【问题标题】:vb.net show tooltiptext for every item in datagridviewcomboboxvb.net 为 datagridviewcombobox 中的每个项目显示工具提示文本
【发布时间】:2016-01-28 02:15:17
【问题描述】:

我已经搜索了显示 datagridview 的工具提示文本,但我只获得了 datagridviewcell 的工具提示文本。我想要的是从datagridviewcomboboxcolumn的下拉列表中突出显示项目(鼠标悬停)时显示的工具提示文本。 我在组合框列的数据绑定中设置了工具提示文本,但它在运行时不显示任何内容。

'assuming dtExpense is the datatable used as datasource
With CType(DataGridView3.Columns(2), DataGridViewComboBoxColumn)
    .AutoComplete = True
    .DataSource = dtExpense
    .DisplayMember = "acct_title"
    .ValueMember = "acct_id"
    .DataPropertyName = "acct_id"
    .ToolTipText = "description"
End With

谁能告诉我怎么做。在 datagriviewcell.tooltiptext 中,它必须在某个点绘制。我在想如何使用 datagridviewcomboboxcolumn 来做到这一点,它必须为组合框中的每个项目显示。

【问题讨论】:

    标签: vb.net datagridview tooltip datagridviewcombobox


    【解决方案1】:

    假设您有一个具有 string 属性的对象类,名为 acct_title(显示为下拉项)和 description(显示为这些下拉项的工具提示),您需要:

    1. 将 ToolTip 控件添加到您的表单。
    2. 为 DataGridView 处理 EditingControlShowing 事件以将事件处理程序添加到底层 ComboBox。

      Me.DataGridView3.EditingControlShowing += DataGridView3_EditingControlShowing
      
      Private Sub DataGridView3_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
      If TypeOf e.Control Is ComboBox Then
              Dim cb As ComboBox = TryCast(e.Control, ComboBox)
              cb.DrawMode = DrawMode.OwnerDrawFixed
              cb.DrawItem -= Cb_DrawItem
              cb.DrawItem += Cb_DrawItem
              cb.DropDownClosed -= Cb_DropDownClosed
              cb.DropDownClosed += Cb_DropDownClosed
          End If
      End Sub
      
    3. 处理 ComboBox DrawItem 事件以设置 ToolTip 值并显示它。我们将使用反射从下拉项中获取description 属性并将其字符串值设置为工具提示。

      Private Sub Cb_DrawItem(sender As Object, e As DrawItemEventArgs)
          Dim cb As ComboBox = TryCast(sender, ComboBox)
          Dim item = cb.Items(e.Index)
          Dim display As String = cb.GetItemText(item)
          Dim toolText As String = item.[GetType]().GetProperty("description").GetValue(item, Nothing).ToString()
      
          e.DrawBackground()
      
          Using br As New SolidBrush(e.ForeColor)
              e.Graphics.DrawString(display, e.Font, br, e.Bounds)
          End Using
      
          If (e.State And DrawItemState.Focus) = DrawItemState.Focus AndAlso cb.DroppedDown Then 
              Me.toolTip1.Show(toolText, cb, e.Bounds.Right, e.Bounds.Bottom, 2000)
          End If
      
          e.DrawFocusRectangle()
      End Sub
      
    4. 处理 ComboBox DropDownClosed 事件以隐藏工具提示。

      Private Sub Cb_DropDownClosed(sender As Object, e As EventArgs)
          Dim cb As ComboBox = TryCast(sender, ComboBox)
          Me.toolTip1.Hide(cb)
      End Sub
      

    【讨论】:

    • 感谢您的回答!我需要更改一些代码行,因为我的 datagridview 中有很多其他组合框。我认为它做得很好。但是,我在Dim toolText As String = item.[GetType]().GetProperty("description").GetValue(item, Nothing).ToString() 这一行周围得到了一个 NullReferenceException 我什至检查了cb isNot Nothing (尽管我认为它没有必要,因为工具提示应该在项目悬停而不是在选择时显示)但这个错误仍然存​​在。你认为我需要在这里添加一个数据类型Dim item = cb.Items(e.Index)吗?
    • 我可以复制该错误的唯一方法是如果我执行.GetProperty("PropertyThatIsNotOnTheObject")。您可以尝试将数据类型放在Dim item 上,但如果我是正确的,那只会引发同样的错误。确保使用一个字符串调用 GetProperty,该字符串是您要在工具提示中显示的属性的名称。
    • 我确实在我的 datagridviewcomboboxcolumn 属性中设置了.ToolTipText = "description"。那应该没问题吧?但我真的不明白为什么我仍然得到 NullReference。所以,我做了一个解决方法,我有点从Datasource 本身搜索description。我在这里效率很低,但它确实有效。感谢您的帮助!!但是有一点问题,你还能帮我吗?工具提示显示在我屏幕的最底部,因为我有一个很长的帐户名称列表。如何让它显示在组合框项目旁边?
    • 好吧,我调查了为什么会发生这种情况,我认为这是因为如果组合框本身下方没有足够的空间,下拉菜单会转移到顶部。虽然发生这种情况,e.Bounds.Bottom 仍然认为第一项仍然显示在组合框的正下方。我所做的只是从中减去一些量来修复它的 Y 坐标。我只是不确定这是否会支持不同的屏幕分辨率,因为它的动态性不足以自行调整。
    • 对不起,工具提示的位置让你很伤心。在我的测试中,它显示为对齐并位于悬停下拉项的右侧,但我很高兴听到您找到了解决方法。抱歉回复慢;我周末搬家了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    相关资源
    最近更新 更多