Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一个控件上有多个带下划线的工具提示-WinForms_C#_Winforms_Tooltip - Fatal编程技术网

C# 一个控件上有多个带下划线的工具提示-WinForms

C# 一个控件上有多个带下划线的工具提示-WinForms,c#,winforms,tooltip,C#,Winforms,Tooltip,我想要一个显示的控件 工具提示1非工具提示工具提示2 ^ ^ TOOLTIPA TOOLTIPB 将鼠标悬停在tooltip1上时显示一个工具提示,将鼠标悬停在tooltip2上时显示另一个工具提示。事实上,这些都会被加下划线和蓝色 我可以使用richtextbox并通过GetPositionFromCharIndex和鼠标事件跟踪工具提示的位置来实现这一点。但是,使用richtextbox,如果不禁用控件,我就无法禁用文本选择/位置插入符号,这反过来会

我想要一个显示的控件

工具提示1非工具提示工具提示2

   ^             ^
TOOLTIPA      TOOLTIPB
将鼠标悬停在tooltip1上时显示一个工具提示,将鼠标悬停在tooltip2上时显示另一个工具提示。事实上,这些都会被加下划线和蓝色

我可以使用richtextbox并通过GetPositionFromCharIndex和鼠标事件跟踪工具提示的位置来实现这一点。但是,使用richtextbox,如果不禁用控件,我就无法禁用文本选择/位置插入符号,这反过来会禁用我的所有事件

我不想使用多个标签,因为它需要手动设置标签间距,因为需要处理字符间距

是否有第三方控件(如Dev Express)可以处理此问题?还有别的选择吗


谢谢

这就是你想要的:

ToolTip toolTip = new ToolTip();
toolTip.SetToolTip(control1, "Hello ");
toolTip.SetToolTip(control2, "world!");

……还是我完全错过了这里的船?从您的描述中很难理解问题是什么。

我不能100%确定您在描述中要求什么,但似乎您想要一个具有工具提示的控件,其标题取决于鼠标悬停在控件的哪个部分

我想不出一个“好”的方法来做这件事。下面的代码适用于标签,但很难看。我个人更愿意创建一个带有多个标签的用户控件,并担心手动分隔标签。但我得到的是:

        Label lbl = new Label() { Left = 6, Top = 6, AutoSize = true };
        this.Controls.Add(lbl);

        // Determine the width of each of the "sections" of the Label Text.
        // Use the fact that AutoSize = true will increase the Width of the
        //  Label as you increase the Text Length.
        int width = lbl.Width;
        lbl.Width = 0;
        lbl.Text = string.Empty;
        lbl.Text = "x";
        int delta = lbl.Width;
        lbl.Text += "x";
        delta = lbl.Width - 2 * (lbl.Width - delta);
        lbl.Text = "ToolTip1";
        int txt1Width = lbl.Width - delta;
        delta = lbl.Width;
        lbl.Text += " NoToolTip ";
        int txt2Width = lbl.Width - delta;
        delta = lbl.Width;
        lbl.Text += "ToolTip2";
        int txt3Width = lbl.Width - delta;

        // Use a System.Windows.Forms.ToolTip and set the caption on 
        //  MouseHover, depending on the Position of the Cursor.
        var tip = new ToolTip();
        lbl.MouseHover += delegate(object sender, EventArgs e)
        {
            tip.RemoveAll();

            // Find the Point for the ToolTip (relative to the Label) based
            //  on the Position of the Cursor.
            this.Cursor = new Cursor(Cursor.Current.Handle);
            Point lblScreenPos = lbl.PointToScreen(Point.Empty);
            Point tipPoint = new Point(Cursor.Position.X, Cursor.Position.Y + lbl.Height);
            tipPoint = new Point(tipPoint.X - lblScreenPos.X, tipPoint.Y - lblScreenPos.Y);

            // Determine the location of the "sections" of Label text.
            int x = tipPoint.X;
            int txt1Left = 3;
            int txt1Right = txt1Left + txt1Width;
            int txt3Left = txt1Right + txt2Width;
            int txt3Right = txt3Left + txt3Width;

            // Show the ToolTip with the correct caption.
            if (x >= txt1Left && x <= txt1Right)
            {
                tip.Show("TOOLTIPA", lbl, tipPoint, tip.AutoPopDelay);
            }
            else if (x >= txt3Left && x <= txt3Right)
            {
                tip.Show("TOOLTIPB", lbl, tipPoint, tip.AutoPopDelay);
            }
        };
        lbl.MouseLeave += delegate(object sender, EventArgs e) { tip.RemoveAll(); };
Label lbl=new Label(){Left=6,Top=6,AutoSize=true};
this.Controls.Add(lbl);
//确定标签文本每个“部分”的宽度。
//使用AutoSize=true将增加屏幕宽度这一事实
//增加文本长度时添加标签。
int width=lbl.width;
lbl.宽度=0;
lbl.Text=string.Empty;
lbl.Text=“x”;
int delta=磅宽;
lbl.Text+=“x”;
增量=磅宽-2*(磅宽-增量);
lbl.Text=“工具提示1”;
int txt1Width=lbl.Width-delta;
增量=磅宽;
lbl.Text+=“NOTOLTIP”;
int txt2Width=lbl.Width-delta;
增量=磅宽;
lbl.Text+=“工具提示2”;
int txt3Width=lbl.Width-delta;
//使用System.Windows.Forms.ToolTip并将标题设置为
//鼠标悬停,具体取决于光标的位置。
var tip=新工具提示();
lbl.MouseHover+=委托(对象发送方,事件参数e)
{
tip.RemoveAll();
//根据标签查找工具提示的点(相对于标签)
//在光标的位置上。
this.Cursor=新光标(Cursor.Current.Handle);
点lblScreenPos=lbl.PointToScreen(点为空);
点倾斜点=新点(Cursor.Position.X,Cursor.Position.Y+lbl.Height);
tipPoint=新点(tipPoint.X-lblScreenPos.X,tipPoint.Y-lblScreenPos.Y);
//确定标签文本“部分”的位置。
int x=tipPoint.x;
int txt1Left=3;
int txt1Right=txt1Left+txt1Width;
int txt3Left=txt1Right+txt2Width;
int txt3Right=txt3Left+txt3Width;
//显示带有正确标题的工具提示。

如果(x>=txt1Left&&x=txt3Left&&x这与我使用richtextbox时所做的操作非常接近。但我实际上是在标签文本的下面画下划线,以显示工具的显示位置。有点像链接。这就是为什么我使用richtextbox,以便可以中途更改字体的原因。但是,我仍然坚持使用插入符号位置和可选文本。Use 2个LinkLabels和1个Label。使用代码将它们放置在表单上的正确位置。确定使用类似于我的代码放置控件的位置,该代码获取标签文本“部分”的宽度。找到一个空格的宽度,控件左边缘和绘制文本之间的距离(这是3个像素),以及控件右边缘与绘制文本之间的距离(控件宽度-文本宽度-3)。使用这3个值来确定放置下一个控件的位置,以便绘制的文本正好位于上一个控件的位置后一个空格。我尝试了此操作,或者至少在表单上放置了一个包含所有文本的标签,然后手动排列多个标签,以查看它们的位置相对于上一个标签的位置/宽度。value每次都会因为字符间距逻辑而改变,有时是6像素,有时是4像素。(注意,我关闭了所有标签上的填充。我可以通过使用包含所有文本的richtextbox控件来解决问题。然后,我使用richtextbox中的GetPositionFromCharIndex将标签插入表单中。很抱歉,您是对的。我发现开始时,位置+宽度-7始终是这个值下一个标签(这不是使用修剪,修剪将是4像素)。但是,如果我修剪,字体改变,我不知道空间有多大(默认情况下,我的空间是3像素)。我不知道如何描述它,但基本上是一个显示三个单词的控件,其中鼠标悬停在第一个单词上会显示一个工具提示,鼠标悬停在第三个单词上会显示另一个工具提示。我还希望单词1和3加下划线,但单词2上没有工具提示或下划线…您满意吗?我正在创建所有者绘制调色板的用户控件。最好在每种颜色上显示不同的工具提示。我可以调用tooltip.show(),但一旦它隐藏起来,它将不会再次显示,直到鼠标离开并重新进入我的控件。我正在完成它。我最终使用TextRenderer绘制我想要的字符串并跟踪不同的工具提示位置。修改它供您使用应该不会太坏。共享代码的好方法是什么?