如何在虚拟模式下格式化DataGridView使用的工具提示?

时间:2022-03-09 17:28:13

I need to apply formatting - specifically, bold text - to the tooltip used by a DataGridView in virtual mode. I can set the text in the CellToolTipTextNeeded event, but it doesn't support HTML tags; is there some other syntax I should be using? I don't want to have to reimplement tooltip support myself.

我需要将格式 - 特别是粗体文本 - 应用于DataGridView在虚拟模式下使用的工具提示。我可以在CellToolTipTextNeeded事件中设置文本,但它不支持HTML标记;我应该使用其他一些语法吗?我不想自己重新实现工具提示支持。

2 个解决方案

#1


You can use the HtmlToolTip from http://www.codeproject.com/KB/GDI-plus/HtmlRenderer.aspx

您可以使用http://www.codeproject.com/KB/GDI-plus/HtmlRenderer.aspx中的HtmlToolTip。

To use it with DataGridView create a ToolTip (HtmlToolTip) and add this after the InitalizeComponent() in your form to replace the default tooltip:

System.Reflection.FieldInfo toolTipControlFieldInfo=
typeof(DataGridView).GetField("toolTipControl", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

System.Reflection.FieldInfo toolTipFieldInfo=
toolTipControlFieldInfo.FieldType.GetField("toolTip", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

object toolTipControlInstance =
toolTipControlFieldInfo.GetValue(myDataGridView);

toolTipFieldInfo.SetValue(toolTipControlInstance, myToolTip);

Work for me with .net 3.5

用.net 3.5为我工作

#2


As the name of the event implies, it simply wants the text to display, which will not have formatting.

正如事件的名称所暗示的那样,它只是希望显示文本,而不会有格式化。

If you want anything like bold, or other kinds of formatting, you are going to have to handle the display and drawing of the tool tip yourself. You can use the ToolTip control to help, setting the OwnerDraw property to true and handling the Draw event, but note, you will most likely have to override the grid in a significant manner to get into that event at the appropriate time.

如果您想要粗体或其他类型的格式,您将不得不自己处理工具提示的显示和绘图。您可以使用ToolTip控件来帮助,将OwnerDraw属性设置为true并处理Draw事件,但请注意,您很可能必须以显着的方式覆盖网格才能在适当的时间进入该事件。

#1


You can use the HtmlToolTip from http://www.codeproject.com/KB/GDI-plus/HtmlRenderer.aspx

您可以使用http://www.codeproject.com/KB/GDI-plus/HtmlRenderer.aspx中的HtmlToolTip。

To use it with DataGridView create a ToolTip (HtmlToolTip) and add this after the InitalizeComponent() in your form to replace the default tooltip:

System.Reflection.FieldInfo toolTipControlFieldInfo=
typeof(DataGridView).GetField("toolTipControl", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

System.Reflection.FieldInfo toolTipFieldInfo=
toolTipControlFieldInfo.FieldType.GetField("toolTip", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

object toolTipControlInstance =
toolTipControlFieldInfo.GetValue(myDataGridView);

toolTipFieldInfo.SetValue(toolTipControlInstance, myToolTip);

Work for me with .net 3.5

用.net 3.5为我工作

#2


As the name of the event implies, it simply wants the text to display, which will not have formatting.

正如事件的名称所暗示的那样,它只是希望显示文本,而不会有格式化。

If you want anything like bold, or other kinds of formatting, you are going to have to handle the display and drawing of the tool tip yourself. You can use the ToolTip control to help, setting the OwnerDraw property to true and handling the Draw event, but note, you will most likely have to override the grid in a significant manner to get into that event at the appropriate time.

如果您想要粗体或其他类型的格式,您将不得不自己处理工具提示的显示和绘图。您可以使用ToolTip控件来帮助,将OwnerDraw属性设置为true并处理Draw事件,但请注意,您很可能必须以显着的方式覆盖网格才能在适当的时间进入该事件。