WinForms工具提示中的文本长度

WinForms工具提示中的文本长度,winforms,tooltip,Winforms,Tooltip,如何在WinForms工具提示中设置文本的最大长度? 我有一个大约有300个字符的字符串,但我的工具提示只显示其中的264个字符 问候 Jürgen您可以像这样在工具提示字符串中添加几次新行,这样它就不会在屏幕上出现 此代码中的字符串长度为434个字符 :-) 请运行此代码来尝试它:>> Imports System.Environment Public Class Form1 Friend WithEvents myToolTip As New ToolTip Priva

如何在WinForms工具提示中设置文本的最大长度? 我有一个大约有300个字符的字符串,但我的工具提示只显示其中的264个字符

问候

Jürgen

您可以像这样在工具提示字符串中添加几次新行,这样它就不会在屏幕上出现

此代码中的字符串长度为434个字符

:-)

请运行此代码来尝试它:>>

Imports System.Environment

Public Class Form1

    Friend WithEvents myToolTip As New ToolTip

    Private Sub Form1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseHover

        Dim someText As String = _
        "Hello World!! Hi everyone!! Good day, good evening or goodnight, wherever you are in the world!!  :-)   :-D" & NewLine & _
        "Hello World!! Hi everyone!! Good day, good evening or goodnight, wherever you are in the world!!  :-)   :-D" & NewLine & _
        "Hello World!! Hi everyone!! Good day, good evening or goodnight, wherever you are in the world!!  :-)   :-D" & NewLine & _
        "Hello World!! Hi everyone!! Good day, good evening or goodnight, wherever you are in the world!!  :-)   :-D"

        Me.Text = someText.Length.ToString
        myToolTip.Show(someText, Me, 5000)

    End Sub

End Class
您可以像这样在工具提示字符串中添加几次新行,这样它就不会一直出现在屏幕上

此代码中的字符串长度为434个字符

:-)

请运行此代码来尝试它:>>

Imports System.Environment

Public Class Form1

    Friend WithEvents myToolTip As New ToolTip

    Private Sub Form1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseHover

        Dim someText As String = _
        "Hello World!! Hi everyone!! Good day, good evening or goodnight, wherever you are in the world!!  :-)   :-D" & NewLine & _
        "Hello World!! Hi everyone!! Good day, good evening or goodnight, wherever you are in the world!!  :-)   :-D" & NewLine & _
        "Hello World!! Hi everyone!! Good day, good evening or goodnight, wherever you are in the world!!  :-)   :-D" & NewLine & _
        "Hello World!! Hi everyone!! Good day, good evening or goodnight, wherever you are in the world!!  :-)   :-D"

        Me.Text = someText.Length.ToString
        myToolTip.Show(someText, Me, 5000)

    End Sub

End Class
我也遇到了同样的问题(碰巧是DataGridView单元格),默认的工具提示文本(即单元格的文本内容)确实被截断了

对我来说,当我明确设置工具提示文本时,它开始正常工作(我看到的所有答案都是这样做的)。我认为很微妙的是,默认的工具提示文本使用相同的单元格内容,只有默认的处理程序会像原始问题中提到的那样截断它。通过覆盖事件并设置工具提示文本(即使它与单元格文本完全相同!),现在默认的长度限制似乎消失了

protected override void OnCellToolTipTextNeeded(DataGridViewCellToolTipTextNeededEventArgs e)
{
    if((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
    {
        // By setting this explicitly we can make the ToolTip length
        // longer even though the content is exactly the same.
        e.ToolTipText = this[e.ColumnIndex, e.RowIndex].Value.ToString();
    }
    base.OnCellToolTipTextNeeded(e);
}
其他控件当然会触发不同的事件,但如果您自己将文本放在工具提示上,则可以避免任何默认工具提示出现的截断。

我也遇到了同样的问题(碰巧是DataGridView单元格)和默认工具提示文本(即单元格的文本内容)确实被截断了

对我来说,当我明确设置工具提示文本时,它开始正常工作(我看到的所有答案都是这样做的)。我认为很微妙的是,默认的工具提示文本使用相同的单元格内容,只有默认的处理程序会像原始问题中提到的那样截断它。通过覆盖事件并设置工具提示文本(即使它与单元格文本完全相同!),现在默认的长度限制似乎消失了

protected override void OnCellToolTipTextNeeded(DataGridViewCellToolTipTextNeededEventArgs e)
{
    if((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
    {
        // By setting this explicitly we can make the ToolTip length
        // longer even though the content is exactly the same.
        e.ToolTipText = this[e.ColumnIndex, e.RowIndex].Value.ToString();
    }
    base.OnCellToolTipTextNeeded(e);
}

其他控件当然会触发不同的事件,但如果您自己将文本放在工具提示上,则可以避免任何默认工具提示出现的截断。

我知道这是一个老问题,我不确定当时是否存在以下功能,但对于搜索此功能的用户,我注意到,如果工具提示文本很长,它可能根本无法显示,经过一些尝试后,我发现这有助于:

// 999 = just an arbitrary number to test for possible very long text, may have to fiddle with that (maybe screen width) !
// 456 = also arbitrary, change to your liking !
// tooltip.GetToolTip((sender as ToolTip).Tag as Control) is because I have multiple controls using the same Tooltip, so I set the Tooltip.Tag to the control that will call Tooltip.Show(...). If you have 1 tooltip per control than just replace it with the control in question.
tooltip.Popup += (sender, e) =>
{
    if (e.ToolTipSize.Width > 999)
    {
        Size s = TextRenderer.MeasureText(tooltip.GetToolTip((sender as ToolTip).Tag as Control), SystemFonts.SmallCaptionFont);
        e.ToolTipSize = new Size(456, s.Height * 3); // * 3 turned out to work for SystemFonts.SmallCaptionFont
    }
};

我知道这是一个老问题,我不确定当时是否存在以下功能,但对于那些搜索此功能的人,我注意到,如果工具提示文本很长,它可能根本无法显示,经过一些尝试后,发现这很有帮助:

// 999 = just an arbitrary number to test for possible very long text, may have to fiddle with that (maybe screen width) !
// 456 = also arbitrary, change to your liking !
// tooltip.GetToolTip((sender as ToolTip).Tag as Control) is because I have multiple controls using the same Tooltip, so I set the Tooltip.Tag to the control that will call Tooltip.Show(...). If you have 1 tooltip per control than just replace it with the control in question.
tooltip.Popup += (sender, e) =>
{
    if (e.ToolTipSize.Width > 999)
    {
        Size s = TextRenderer.MeasureText(tooltip.GetToolTip((sender as ToolTip).Tag as Control), SystemFonts.SmallCaptionFont);
        e.ToolTipSize = new Size(456, s.Height * 3); // * 3 turned out to work for SystemFonts.SmallCaptionFont
    }
};

如何设置工具提示?我复制并粘贴了你的信息3次,这使它有424字节长(3行),所有3行都显示出来了。我所做的只是在表单上抛出一个文本框和一个工具提示,然后设置文本框的工具提示文本。你是如何设置工具提示的?我复制并粘贴了你的信息3次,这使它有424字节长(3行),所有3行都显示出来了。我所做的只是在表单上抛出一个文本框和一个tooltiop,并为文本框设置工具提示文本。上面的代码显示了继承
DataGridView
时的示例。还有一个事件
CellToolTipTextRequired
,可以对其进行处理以在case中实现相同的效果,当开发人员不需要覆盖控件时。上面的代码显示了继承
DataGridView
时的示例。还有事件
CellToolTipTextRequired
,当开发人员不需要覆盖控件时,可以对其进行处理以达到相同的效果。