tooltip no longer working
Hi All.. I am creating buttons dynamically from a delimited text file and assigning text to a tooltip. This was always working just fine, but for some reason all of a sudden they stopped working. I have not done anything strange with the tooltip so I cannot explain how it happened. What seems so basic has totally stopped on me. Here is how I am creating the tooltips:
Code:
Dim myDefectFile As String
myDefectFile = Environment.CurrentDirectory & "\Defect_Files\"
Dim FILE_NAME As String = myDefectFile & "Defects.txt"
Dim data() As String
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
rawData = objReader.ReadLine()
data = Split(rawData, ",")
If data(0) = ToggleFile Then
Dim dynamicButton As New Button With {
.Location = New Point(CInt(data(2)), CInt(data(3))),
.Height = 5,
.Width = 5,
.FlatStyle = FlatStyle.Flat,
.BackColor = Color.Magenta,
.ForeColor = Color.Magenta
}
If PictureBox1.Visible = True Then
PictureBox1.Select()
Me.PictureBox1.Controls.Add(dynamicButton)
ElseIf PictureBox2.Visible = True Then
PictureBox2.Select()
Me.PictureBox2.Controls.Add(dynamicButton)
End If
Dim tip As New ToolTip
Dim myToolTipText = "SN:" & data(1) & " '" & data(6) & "'"
tip.SetToolTip(dynamicButton, myToolTipText)
TextBox1.Text = "DEFECT MAP"
End If
Loop
Re: tooltip no longer working
There are a couple of things that come to mind:
- The data has changed. Perhaps there is an unescaped comma (which aren't being handled) or less columns and it is causing some sort of indexing error when you get the value of data by its index.
- The button is never being added to PictureBox1 or PictureBox2 because the If/Then statements are being reached
- I know that PictureBox inherits Control and Control has a Controls property, but I didn't think you could add controls to a PictureBox (though I may be wrong)
You should set a breakpoint up where you check the visibility of PictureBox1, then step through your code to make sure that the right workflow is occurring.
By the way, I'm not sure what version of Visual Studio you're using, but you could modify your code to:
Code:
Dim filename = IO.Path.Combine(Environment.CurrentDirectory, "Defect_Files", "Defects.txt")
If (IO.File.Exists(filename) Then
Dim lines = IO.File.ReadAllLines(filename)
For Each line In lines
Dim cells = line.Split(","c)
If (cells.Length > 0 AndAlso cells(0) = ToggleFile) Then
TextBox1.Text = "DEFECT MAP"
Dim dynamicButton As New Button With {
.Location = New Point(CInt(data(2)), CInt(data(3))),
.Height = 5,
.Width = 5,
.FlatStyle = FlatStyle.Flat,
.BackColor = Color.Magenta,
.ForeColor = Color.Magenta
}
If (data.Length > 6) Then
Dim tip = New ToolTip()
tip.SetToolTip(dynamicButton, $"SN: {data(0)} '{data(6)}'")
End If
If PictureBox1.Visible = True Then
PictureBox1.Select()
Me.PictureBox1.Controls.Add(dynamicButton)
ElseIf PictureBox2.Visible = True Then
PictureBox2.Select()
Me.PictureBox2.Controls.Add(dynamicButton)
End If
End If
Next
End If
Re: tooltip no longer working
Thanks for this.. I did update my code to your suggestion. So I am getting my dynamic buttons added to the picturebox when expected, so the data points from the file seem to be ok there. I am returning a msgbox with my tooltip text that should be showing at the time of adding the button and that seems to work ok. But no matter what I do, that tooltip doesn't want to actually show when I hover over any of the buttons. The only thing left is the breakpoint you mentioned, which I am not certain how to setup.
Re: tooltip no longer working
If you place your cursor on the line you want to setup the breakpoint and hit F9, it will add it for you (the line will be highlighted red). F9 is how you toggle breakpoints.
Once you're running the code, the code will "break" (aka - pause, stop, etc.) at your breakpoint. From here you can open the watch window, add variables to the watch list, and inspect what they are.
If you hit F10 while in break mode, the code will step over 1 line. In other words, it will execute the line without going into any methods (if applicable).
If you hit F11 while in break mode, the code will step into line. in other words, if the current line is a method, then it will bring you inside of the method.
None of your lines are referencing methods, so you should be good with doing F10, one line at a time, inspecting the variables as you go.
Re: tooltip no longer working
Thanks dday9, but I am totally lost with this. I ran the breakpoint and followed a couple of variables that it would allow me to, but nothing really seems to point out the problem with the tooltip. I can return the tooltip text through a msgbox, button shows up just as it should but no tooltip popup will exist. ugh..
Re: tooltip no longer working
Don't be creating a new ToolTip component for every Button. You don't need any more than one TooTip on the whole form. Add one ToolTip to the form in the designer, then call SetToolTip on that for each Button.
Re: tooltip no longer working
ok... so this has been quite a learning experience for me. I just realized that I had my picturebox disabled, which is what my buttons are being created on. I commented this out to try it and magic, the tooltip shows. However, I found on the Microsoft site that if you have ShowAlways set to true, the tooltip will show over a disabled control. This does not seem to be the case, unless there is another piece I am missing.
Re: tooltip no longer working
If the BUTTON had been disabled, it may have worked... but because the picture box, which is the container, was disabled, the message never gets through, so the ShowAlways doesn't happen.
By disabling the container, you effectively pulled the wool over the eyes of Windows to allow it to see that there were buttons on it that had tooltips.
-tg
Re: tooltip no longer working
Thanks techgnome.. I have not yet found a way around this, so in the meantime I have made a small workaround not related to this method at all.