vb.net 下拉列表中每个项的工具提示



我将 DrawMode 属性设置为OwnerDrawFixed,并将以下代码用于 Combobox 中每个项的工具提示。我在工具提示中将System.Data.DataRow作为ComboBox中的项目。如何解决此问题?

Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.Index = -1 Then
Return
End If
e.DrawBackground()
Dim p As New Point
p = New Point(ComboBox1.Location.X + 120, ComboBox1.Location.Y + ComboBox1.Height + (30 + e.Index * 10))
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
ToolTip1.Show(ComboBox1.Items(e.Index).ToString, Me, p)
End If
' e.DrawBackground()
e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString, e.Font, Brushes.Black, New Point(e.Bounds.X, e.Bounds.Y))
End Sub

您的代码ComboBox1.Items(e.Index).ToString正在调用 Items(e.Index( 的基础Type上的.ToString()。看起来您正在将组合框绑定到数据表,因此项目中任何内容的基础Type都是数据行。在数据行上调用ToString()将产生System.Data.DataRow的值。

相反,您必须从 DataRow 的所需列中提取所需的值,并将该值放在工具提示中。

最新更新