WinForms 工具提示中的文本长度

发布于 2024-11-30 07:33:00 字数 103 浏览 1 评论 0原文

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

问候,

Jürgen

how to set the max length of a text in a WinForms ToolTip?
I have a String with about 300 chars, but my ToolTip displays only 264 of them...

Greets,

Jürgen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦魇绽荼蘼 2024-12-07 07:33:00

您可以像这样在工具提示字符串中添加几次 NewLine ,这样它就不会一直穿过屏幕。

此代码中的字符串长度为 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

You could add NewLine a few times in your ToolTip string like this so that it does not go all the way across the screen.

The string in this code is 434 characters long.

:-)

Just run this code to try it please:>>

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
浅语花开 2024-12-07 07:33:00

我遇到了同样的问题(它恰好与 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);
}

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

I had this same issue (it happened to be with a DataGridView cell) and the default ToolTip text (i.e. the text content of the cell) was indeed being truncated.

For me, it started working correctly when I set the ToolTip text explicitly (all of the answers I see do this). What I think is subtle is that the default ToolTip text uses the same cell content, only the default handler does truncate it as noted in the original question. By overriding the event and setting the ToolTip text (even though it's exactly the same cell text!) now the default length restriction seems to go away.

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);
}

Other controls will fire different events of course, but it should hold that if you put the text onto the tool tip yourself then you can circumvent the truncation that occurs with any default ToolTip.

原谅过去的我 2024-12-07 07:33:00

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

// 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
    }
};

I know this is an old question and I'm not sure if the following functionality existed at that time, but for those searching for this, I noticed that if the Tooltip text is very long, it may not get displayed at all, and after some trying, found that this helps:

// 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
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文