mp45
asked on
How can I increase the time a tooltip is shown in a DataGridView (VS2005 SP1)
(Visual Studio 2005 SP 1, Visual Basic)
Hi,
In a DataGridView I defined tooltips for the cells in the CellFormatting event. Some of the tooltip texts contain more than only a few words. To make it easier to read the text I would like to increase the time a tooltip is shown.
Is there a chance to set the parameters of the DataGridView tooltip, ... such as the properties "AutoPopDelay" and "AutomaticDelay" of a Tooltip control?
Best wishes
Michael
Hi,
In a DataGridView I defined tooltips for the cells in the CellFormatting event. Some of the tooltip texts contain more than only a few words. To make it easier to read the text I would like to increase the time a tooltip is shown.
Is there a chance to set the parameters of the DataGridView tooltip, ... such as the properties "AutoPopDelay" and "AutomaticDelay" of a Tooltip control?
Best wishes
Michael
ASKER
Hi,
I tried it in tow ways:
1. I added the code in the CellFormatting event
2. in the CellMouseEnter event
but in none of the ways the tooltip was shown. (The code for both ways see below)
You wrote that "the auto tooltip can be disabled from the DataGridView". Perhaps this is a necessary precondition. How can I disable it?
.. or have I implemented something else in a wrong way?
Michael
------ in CellFormatting event ------
Private Sub DataGridView2_CellFormatti ng( _
ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewCel lFormattin gEventArgs ) _
Handles DataGridView2.CellFormatti ng
Dim strValueL As String
If Not (e.Value Is Nothing) Then
With Me.DataGridView2
Select Case e.ColumnIndex
Case .Columns("col2_townM").Ind ex
With .Rows(e.RowIndex).Cells(e. ColumnInde x)
strValueL = CStr(e.Value).Trim
' --- ToolTip of DataGridView
'If strValueL = "" Then
' .ToolTipText = _
' "H I N T: " & vbCrLf _
' & "Town is not specified yet"
'Else
' .ToolTipText = _
' "Town is " & vbCrLf _
' & strValueL
'End If
' --- Separate ToolTip
If strValueL = "" Then
Me.ToolTip.SetToolTip( _
Me.DataGridView2, _
"H I N T: " & vbCrLf _
& "Town is not specified yet")
Else
Me.ToolTip.SetToolTip( _
Me.DataGridView2, _
"Town is " & vbCrLf _
& strValueL)
End If
End With
End Select
End With
End If
End Sub
------ in CellMouseEnter event ------
Private Sub DataGridView2_CellMouseEnt er( _
ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) _
Handles DataGridView2.CellMouseEnt er
Dim strValueL As String
Debug.Print("Cell (col, row): " & e.ColumnIndex & ", " & e.RowIndex)
With Me.DataGridView2
If _
e.ColumnIndex >= 0 AndAlso e.ColumnIndex < .Columns.Count _
AndAlso _
e.RowIndex >= 0 AndAlso e.RowIndex < .Rows.Count _
Then
Select Case e.ColumnIndex
Case .Columns("col2_townM").Ind ex
With .Rows(e.RowIndex).Cells(e. ColumnInde x)
If .Value IsNot Nothing Then
strValueL = CStr(.Value).Trim
If strValueL = "" Then
Me.ToolTip.SetToolTip( _
Me.DataGridView2, _
"H I N T: " & vbCrLf _
& "Town is not specified yet")
Else
Me.ToolTip.SetToolTip( _
Me.DataGridView2, _
"Town is " & vbCrLf _
& strValueL)
End If
End If
End With
End Select
End If
End With
End Sub
I tried it in tow ways:
1. I added the code in the CellFormatting event
2. in the CellMouseEnter event
but in none of the ways the tooltip was shown. (The code for both ways see below)
You wrote that "the auto tooltip can be disabled from the DataGridView". Perhaps this is a necessary precondition. How can I disable it?
.. or have I implemented something else in a wrong way?
Michael
------ in CellFormatting event ------
Private Sub DataGridView2_CellFormatti
ByVal sender As Object, ByVal e As System.Windows.Forms.DataG
Handles DataGridView2.CellFormatti
Dim strValueL As String
If Not (e.Value Is Nothing) Then
With Me.DataGridView2
Select Case e.ColumnIndex
Case .Columns("col2_townM").Ind
With .Rows(e.RowIndex).Cells(e.
strValueL = CStr(e.Value).Trim
' --- ToolTip of DataGridView
'If strValueL = "" Then
' .ToolTipText = _
' "H I N T: " & vbCrLf _
' & "Town is not specified yet"
'Else
' .ToolTipText = _
' "Town is " & vbCrLf _
' & strValueL
'End If
' --- Separate ToolTip
If strValueL = "" Then
Me.ToolTip.SetToolTip( _
Me.DataGridView2, _
"H I N T: " & vbCrLf _
& "Town is not specified yet")
Else
Me.ToolTip.SetToolTip( _
Me.DataGridView2, _
"Town is " & vbCrLf _
& strValueL)
End If
End With
End Select
End With
End If
End Sub
------ in CellMouseEnter event ------
Private Sub DataGridView2_CellMouseEnt
ByVal sender As Object, ByVal e As System.Windows.Forms.DataG
Handles DataGridView2.CellMouseEnt
Dim strValueL As String
Debug.Print("Cell (col, row): " & e.ColumnIndex & ", " & e.RowIndex)
With Me.DataGridView2
If _
e.ColumnIndex >= 0 AndAlso e.ColumnIndex < .Columns.Count _
AndAlso _
e.RowIndex >= 0 AndAlso e.RowIndex < .Rows.Count _
Then
Select Case e.ColumnIndex
Case .Columns("col2_townM").Ind
With .Rows(e.RowIndex).Cells(e.
If .Value IsNot Nothing Then
strValueL = CStr(.Value).Trim
If strValueL = "" Then
Me.ToolTip.SetToolTip( _
Me.DataGridView2, _
"H I N T: " & vbCrLf _
& "Town is not specified yet")
Else
Me.ToolTip.SetToolTip( _
Me.DataGridView2, _
"Town is " & vbCrLf _
& strValueL)
End If
End If
End With
End Select
End If
End With
End Sub
Hi,
First in the datagridview you have to set the ShowCellToolTips = False. Then you have to do this (just the basics):
Private tt As Tooltip
Private Sub DataGridView2_CellMouseEnt er(..) ...
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
tt.RemoveAll()
tt = New ToolTip
tt.InitialDelay = 4000
tt.AutoPopDelay = 4000
tt.SetToolTip(Me.DataGridV iew2. Me.DataGridView2(e.ColumnI ndex, e.RowIndex).Value.ToString )
Else
tt.RemoveAll()
End If
End Sum
First in the datagridview you have to set the ShowCellToolTips = False. Then you have to do this (just the basics):
Private tt As Tooltip
Private Sub DataGridView2_CellMouseEnt
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
tt.RemoveAll()
tt = New ToolTip
tt.InitialDelay = 4000
tt.AutoPopDelay = 4000
tt.SetToolTip(Me.DataGridV
Else
tt.RemoveAll()
End If
End Sum
ASKER
I just increased the "point value" for this question. :-)
Looking forward to a solution.
Michael
Looking forward to a solution.
Michael
But what the problem with the code I have showed you ?
ASKER
Thanks for the reply. (I haven't realised that there was a new answer when I increased the point value.)
I deactivated the ToolTip of the DataGridView by "ShowCellToolTips = False".
Then, the tooltip created by "my" CellMouseEnter event showed up.
Your example contains "tt.RemoveAll() : tt = New ToolTip". Why is it necessary? The MSDN lib tells: If a message for a control is set by ToolTip.SetToolTip, this method does not add a new entry in the ToolTip if there already exists an entry for the control. It rather replaces the message in the already existing entry.
I wonder why is it necessary to call RemoveAll and even use a new ToolTip instance before SetToolTip is called?
Michael
I deactivated the ToolTip of the DataGridView by "ShowCellToolTips = False".
Then, the tooltip created by "my" CellMouseEnter event showed up.
Your example contains "tt.RemoveAll() : tt = New ToolTip". Why is it necessary? The MSDN lib tells: If a message for a control is set by ToolTip.SetToolTip, this method does not add a new entry in the ToolTip if there already exists an entry for the control. It rather replaces the message in the already existing entry.
I wonder why is it necessary to call RemoveAll and even use a new ToolTip instance before SetToolTip is called?
Michael
When you move fom a cell to another and if you dont remove the other tooltips you will have 2,3,4, ... tootips over the datagridview. Also the columnheaders and rowsheaders bellong to the datagridview. This way you must clean it when the mouse are over them.
This code works fine for and if you can I can upload a sample.
This code works fine for and if you can I can upload a sample.
ASKER
If I add a ToolTip in the designer and then always use this one I do not understand why there should be 2, 3, etc. ToolTips over the DataGridView.
So I do neither see why myToolTip = New ToolTip is needed.
And as far as I understand the hint in the MSDN lib, there should only be one entry for the DataGridView in the ToolTip instance, independend how often myToolTip.SetToolTip(Me.Da taGridView 2, <new message text>) is called.
So I do neither see why myToolTip.RemoveAll is needed.
I just like to understand the details. -- Thanks in advance.
Michael
So I do neither see why myToolTip = New ToolTip is needed.
And as far as I understand the hint in the MSDN lib, there should only be one entry for the DataGridView in the ToolTip instance, independend how often myToolTip.SetToolTip(Me.Da
So I do neither see why myToolTip.RemoveAll is needed.
I just like to understand the details. -- Thanks in advance.
Michael
Sorry I have made some wrong copy/pastes. Actualy you don't need to create a new one everytime. In my example I had that on the form load event and the copy wrong.
But you need the RemoveAll () because not all cells are the ones you want, like the column headers.
Please check the snippet that I have attached.
But you need the RemoveAll () because not all cells are the ones you want, like the column headers.
Please check the snippet that I have attached.
Public Class Form1
Private tt As ToolTip
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Creates a DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("id", GetType(Integer)))
dt.Columns.Add(New DataColumn("description", GetType(String)))
Dim dr As DataRow
For x As Byte = 0 To 50
dr = dt.NewRow()
dr.Item("id") = x.ToString
dr.Item("description") = "Item " + x.ToString
dt.Rows.Add(dr)
Next
' Bound the DataTable to the DataGridView
Me.DataGridView1.DataSource = dt
Me.DataGridView1.ShowCellToolTips = False
' Creates the tooltip and definitions
tt = New ToolTip
With tt
.ReshowDelay = 2000
.InitialDelay = 1000
.AutoPopDelay = 50000
End With
End Sub
Private Sub DataGridView1_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseEnter
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
tt.SetToolTip(Me.DataGridView1, Me.DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString)
Else
tt.RemoveAll()
End If
End Sub
End Class
ASKER
Thanks for the code snippet.
I have two improvements:
1. If the ToolTIp is used by other controls in the form, e.g. a tooltip message is added for a button in the designer, this tooltip message will not be shown any more, after the mouse has entered a DataGridView cell, since "ToolTip.RemoveAll" does remove *all* entries in the ToolTip. -- Solution: To use >>Me.ToolTip.SetToolTip(Me .DataGridV iew1, "")<< instead of >>ToolTip.RemoveAll<<.
2. If a cell contains Nothing (instead of an empty string), the event causes an exception. -- Solution: Addition of an extra if condition.
Michael
I have two improvements:
1. If the ToolTIp is used by other controls in the form, e.g. a tooltip message is added for a button in the designer, this tooltip message will not be shown any more, after the mouse has entered a DataGridView cell, since "ToolTip.RemoveAll" does remove *all* entries in the ToolTip. -- Solution: To use >>Me.ToolTip.SetToolTip(Me
2. If a cell contains Nothing (instead of an empty string), the event causes an exception. -- Solution: Addition of an extra if condition.
Michael
Private Sub DataGridView1_CellMouseEnter( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellMouseEnter
Dim toolTipText As String
If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 Then
toolTipText = ""
If Me.DataGridView1(e.ColumnIndex, e.RowIndex).Value _
IsNot Nothing Then
toolTipText = Me.DataGridView1( _
e.ColumnIndex, e.RowIndex).Value.ToString
End If
Me.ToolTip.SetToolTip(Me.DataGridView1, toolTipText)
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works fine. -- Thanks.
Michael
Michael
ASKER
Futher way to use an external tooltip:
In the above mentioned way to use an external tooltip the cell is formatted in the CellFormatting event and the tooltips texts as specified in the CellMouseEnter event. Thus the implementation is separated in two different event methods. However, the format and the tooltip message often belongs together such as a neg. value in a cell should be highlighted be a red cell background and the tooltip message should tell something about the neg. sign.
There is a chance to specify the cell format and its tooltip text together in only one methode and yet show the tooltip message by an external tooltip control. You just have to set the format as well as the tooltip text in the CellFormat event as you would do if you planed to use the internal tooltip of the DataGridView (i.e. set the tooltip message by: Me.DataGridView1.Rows(e.Ro wIndex).Ce lls(e.Colu mnIndex).T oolTipText = "..."). Disable the internal ToolTip (in the designer or in the load event of the DataGridView: "Me.DataGridView1.ShowCell ToolTips = False"). And add the CellMouseEnter event and just get the tooltip message specified for the cell and show it in the external tooltip (see below).
Michael
Private Sub DataGridView1_CellMouseEnt er( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) _
Handles DataGridView1.CellMouseEnt er
Dim toolTipTextL As String
If Not Me.DataGridView1.ShowCellT oolTips Then
If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 Then
toolTipTextL = ""
With Me.DataGridView1.Rows(e.Ro wIndex).Ce lls(e.Colu mnIndex)
' Take tooltip message which has been specified by the
' event 'DataGridView1.CellFormatt ing'
toolTipTextL = .ToolTipText.Trim
' If no tooltip message is given, take the cell contents
' (to make sure that cell contents which could not be shown
' in the cell completely is shown in the tooltip. If the
' contents is completely visible in the cell the contents
' is shown in the tooltip as well)
If toolTipTextL = "" AndAlso .Value IsNot Nothing Then
toolTipTextL = .Value.ToString
End If
End With
' Show the tooltip message in the external ToolTip control
Me.ToolTip.SetToolTip( _
Me.DataGridView1, _
toolTipTextL)
End If
End If
End Sub
In the above mentioned way to use an external tooltip the cell is formatted in the CellFormatting event and the tooltips texts as specified in the CellMouseEnter event. Thus the implementation is separated in two different event methods. However, the format and the tooltip message often belongs together such as a neg. value in a cell should be highlighted be a red cell background and the tooltip message should tell something about the neg. sign.
There is a chance to specify the cell format and its tooltip text together in only one methode and yet show the tooltip message by an external tooltip control. You just have to set the format as well as the tooltip text in the CellFormat event as you would do if you planed to use the internal tooltip of the DataGridView (i.e. set the tooltip message by: Me.DataGridView1.Rows(e.Ro
Michael
Private Sub DataGridView1_CellMouseEnt
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataG
Handles DataGridView1.CellMouseEnt
Dim toolTipTextL As String
If Not Me.DataGridView1.ShowCellT
If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 Then
toolTipTextL = ""
With Me.DataGridView1.Rows(e.Ro
' Take tooltip message which has been specified by the
' event 'DataGridView1.CellFormatt
toolTipTextL = .ToolTipText.Trim
' If no tooltip message is given, take the cell contents
' (to make sure that cell contents which could not be shown
' in the cell completely is shown in the tooltip. If the
' contents is completely visible in the cell the contents
' is shown in the tooltip as well)
If toolTipTextL = "" AndAlso .Value IsNot Nothing Then
toolTipTextL = .Value.ToString
End If
End With
' Show the tooltip message in the external ToolTip control
Me.ToolTip.SetToolTip( _
Me.DataGridView1, _
toolTipTextL)
End If
End If
End Sub
Thanks Michael for your contribute.
I never add implemented the tootip for a datagridview (I did it for you only) and that way I was unable to test it right. Ti describe your experience is always good for other people that read this topic.
jpaulino
I never add implemented the tootip for a datagridview (I did it for you only) and that way I was unable to test it right. Ti describe your experience is always good for other people that read this topic.
jpaulino
I don't think that is possible but why don't you disable the auto tooltips from the datagridview and add a new tooltip handled by you. That way you can define the time that is showed.