NotifyIcon, Label1.Text, and auto-update

glj12

Member
Joined
Feb 5, 2008
Messages
8
Programming Experience
3-5
I have two questions about NotifyIcon, one of which being:

How would I have the hover over text appear as what has been typed into my Label6.Text?

It does not display anything when I apply:

Me.notifyText.Text = Label6.Text

but, it displays this:

Me.notifyText.Text = "°F"

How can I get it to display whatever is located within Label6?

Second question:

Now that I have it figured out where I have my icon be the text from a given label as the icon itself (note that this is different from the above question, mouse hover over text) how do I make it continuously update? I.E., I have created a weather application that updates the given temperature every 10 minutes, and when minimized to the systray, it displays, for example 68 as the icon. When 10 minutes have passed, say the temperature has changed - how do I make it auto-refresh down in the systray to the new temperature?

Many thanks in advance!
-glj12
 
That code will do exactly what it's supposed to do. If you don't see any text in your icon's tool tip then your Label doesn't contain any text either.

The Icon property controls what gets display in the tray. If you want to change what's displayed you have to set the Icon property to a new Icon object.
 
That code will do exactly what it's supposed to do. If you don't see any text in your icon's tool tip then your Label doesn't contain any text either.

The Icon property controls what gets display in the tray. If you want to change what's displayed you have to set the Icon property to a new Icon object.

Actually, there is text in Label6.Text.

Secondly, I finished the refresh problem by adding a timer, that reccured the text being applied as the icon, function.

So, ideas why, possibly? I find that to be odd. Maybe something is countering it?
 
Have you stepped through that section of code?

Well, here's the code:

VB.NET:
Expand Collapse Copy
        'Required by the Windows Form Designer
        'notifyText
        '
        Me.notifyText.Text = Label6.Text
        Me.notifyText.Visible = True

    Public Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Int32) As Int32

    Dim fontToUse As Font = New Font("Microsoft Sans Serif", 14, FontStyle.Regular, _
        GraphicsUnit.Pixel)

    Dim brushToUse As Brush = New SolidBrush(Color.DarkBlue)
    Dim bitmapText As Bitmap = New Bitmap(16, 16)
    Dim g As Graphics = Drawing.Graphics.FromImage(bitmapText)

    Dim hIcon As IntPtr

    Sub createTextIcon()

        Try

            g.Clear(Color.Transparent)

            g.DrawString(Label1.Text, fontToUse, brushToUse, -2, 0)

            hIcon = (bitmapText.GetHicon)

            notifyText.Icon = Drawing.Icon.FromHandle(hIcon)

            DestroyIcon(hIcon.ToInt32)

        Catch exc As Exception

            MessageBox.Show(exc.InnerException.ToString, "Failure!", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub
 
Have you opened the designer.vb file and edited that code? If so then that's is a very bad idea. Firstly, if you change the wrong thing you could render your form unusable. Secondly, I'm guessing that you're accessing the Label's Text property before it's been set, so it will, of course, return an empty string. If you want to set the NotifyIcon's Text but you can't do it at design time then you should be doing it either in the constructor, AFTER the call to InitializeComponent, or else in the Load event handler.

That said, if you're setting the Text of the Label at design time then why would you set the NotifyIcon's Text at run time? Just set them both at design time.
 
No go chief. I tried that, same thing, does not produce anything. It still only produces "text" when in design time, no matter what order I put anything in (and Label does not work). I tried every order I could concoct. Would you like me to just place all the code in here?

(Yes, I initialized it solely after the call to InitializeComponent, and also tried it in the Load event handler).

Other ideas?

Edit: Haha, I just realized that I fixed it... by keeping it only in the design code first, not changing anything else. What was happening was that it kept displaying behind my systray, odd. Anyway, I saw the tip of the label being displayed when hovered over it. I guess my tray is having a bad day. :p

I'll try it out on a different box.
 
Last edited:
Back
Top