TrayIcon picture not updating
Posted
#1
(In Topic #564)
Trainee
I'm writing a form with a TrayIcon that should change according periodically using a timer:
Code
Public Sub Form_Open()
StatusTimer.Delay = 1
TrayIcon1.Visible = True
End
Public Sub StatusTimer_Timer()
UpdateTrayIcon
StatusTimer.Delay = Globals.StatusTimerDelay
End
Private Sub UpdateTrayIcon()
Dim CnxStatus As String
CnxStatus = GetCnxStatus()
If InStr(CnxStatus, "Connected") > 0 Then
TrayIcon1.Tooltip = "Connected"
TrayIcon1.Picture = Picture["icon1.png"]
Else If InStr(CnxStatus, "Not connected") > 0 Then
TrayIcon1.Tooltip = "Not connected"
TrayIcon1.Picture = Picture["icon2.png"]
Endif
Print TrayIcon1.Tooltip
End
The first time the timer is invoked the icon is displayed correctly, but after that the icon picture remain the same, even when the returned status changed (the tooltip is updated correctly). Please advise.
TIA
Posted
Regular

ForeverNoob said
…even when the returned status changed (the tooltip is updated correctly). Please advise.
TIA
Hi TIA and welcome to Gambas One.
I can't immediately see a problem with your code, but I suggest you put a break point at the line starting: "If "
Then run the code and when it stops use F8 to step it on one line at a time while checking variables.
If I get a chance later today I may try to reproduce your problem.
A couple of other tips; paste code on these pages using the gb button, as this preserves Gambas formatting…
Code (gambas)
- [code]
- StatusTimer.Delay = 1
- UpdateTrayIcon
- StatusTimer.Delay = Globals.StatusTimerDelay
- CnxStatus = GetCnxStatus()
- TrayIcon1.Tooltip = "Connected"
- TrayIcon1.Tooltip = "Not connected"
- Print TrayIcon1.Tooltip
- [/code]
…I also suggest that you enable line numbers in the editor, as they can be very handy!
I hope this helps.
Steve
Posted
Regular

The only thing I notice is that while the TrayIcon object is updated as expected, the tray icon only gets updated on the screen when the code exits my timer, which calls the Sub, which changes the TrayIcon icon/picture. Adding Wait statements didn't help (no real reason why they should, its not the form that needs to update its the system panel...but worth a try).
Can you humour me and just temporarily increase your timer delay from 1 second to 10 seconds and retest?
Posted
Guru

Run this code in a Graphical program with 'gb.gui.trayicon' selected.
Code (gambas)
- .Height = 300
- .Width = 650
- .Arrangement = Arrange.Vertical
- .Padding = 5
- .Height = 28
- .Alignment = Align.Center
- TrayIcon1.Tooltip = "Connected"
- .Delay = 3000
- .Trigger
- .Start
- TrayIcon1.Hide 'This is an important part
- TrayIcon1.Tooltip = "Connected"
- Label1.Text = "Connected"
- TrayIcon1.Tooltip = "Not Connected"
- Label1.Text = "Not connected"
- TrayIcon1.Show 'This is an important part
- bSwitch = Not bSwitch
- TrayIcon1.Delete 'This stops the program from continuing to run
Posted
Regular

cogier said
I have run into one part of this problem which is the 'Comment' not changing. This can be solved by 'Hiding' then 'Showing' the icon which will change the 'Comment'. However, I can't get this to work with the 'Picture'….
By 'comment' do you mean ToolTip Charlie?
If I remove the breakpoints and just run my code with TimerDelay of 5 seconds its works fine, including the ToolTips.
(Actually, If I run with TimerDelay set to 1ms is still runs fine, but no tool tips.)
I didn't find .Show/.Hide/.Delete of any help with Icon/Picture.
I wonder if this problem is related to the Linux desktop in use, because there is some old chat in TrayIcon Help about DBus and Ubuntu implementations…
…or there could still be a problem with TIA's code. Maybe you could upload the project so we can take a look?
Posted
Trainee
stevedee said
OK, I've knocked up a simple program and single-stepped my way through it.
TrayIcon.png
The only thing I notice is that while the TrayIcon object is updated as expected, the tray icon only gets updated on the screen when the code exits my timer, which calls the Sub, which changes the TrayIcon icon/picture. Adding Wait statements didn't help (no real reason why they should, its not the form that needs to update its the system panel...but worth a try).
Can you humour me and just temporarily increase your timer delay from 1 second to 10 seconds and retest?
Hi stevedee,
Thanks for answering. I tried increasing the initial timer delay, but that didn't work. In fact, over the value of 50(ms) or so the TrayIcon didn't updated even on the first iteration and just displayed the default gambas icon.
Posted
Trainee
cogier said
I have run into one part of this problem which is the 'Comment' not changing. This can be solved by 'Hiding' then 'Showing' the icon which will change the 'Comment'. However, I can't get this to work with the 'Picture'.
Run this code in a Graphical program with 'gb.gui.trayicon' selected.Code (gambas)
.Height = 300 .Width = 650 .Arrangement = Arrange.Vertical .Padding = 5 .Height = 28 .Alignment = Align.Center TrayIcon1.Tooltip = "Connected" .Delay = 3000 .Trigger .Start TrayIcon1.Hide 'This is an important part TrayIcon1.Tooltip = "Connected" Label1.Text = "Connected" TrayIcon1.Tooltip = "Not Connected" Label1.Text = "Not connected" TrayIcon1.Show 'This is an important part bSwitch = Not bSwitch TrayIcon1.Delete 'This stops the program from continuing to run
Hi cogier,
Thanks for answering. I've copied the above code to a new form and eidted the relevant part to look like this:
The label is periodically updated, but not the icon.
Posted
Trainee
Thank you all for the time and effort.
Posted
Regular

ForeverNoob said
…The label is periodically updated, but not the icon.
Could you post the details of your system?
including:-
- Linux Distro
- Linux kernel
- Gambas version
…and maybe go to menu Project > Properties and post a screen-shot like this;
Posted
Trainee
stevedee said
ForeverNoob said
…The label is periodically updated, but not the icon.
Could you post the details of your system?
including:-
- Linux Distro
- Linux kernel
- Gambas version
…and maybe go to menu Project > Properties and post a screen-shot like this;
TrayIconProperties.png
Mint 20.1 cinnamon
Kernel 5.4.0-62-generic
Gambas 3.14.3
Posted
Guru

ForeverNoob said
Hi cogier,
Thanks for answering. I've copied the above code to a new form and edited the relevant part to look like this:
The label is periodically updated, but not the icon.
You need the Hide and Show in your code, or it won't work properly. I did mark it as 'Important'
Code (gambas)
- TrayIcon1.Hide 'This is an important part
Code (gambas)
- TrayIcon1.Show 'This is an important part
Posted
Guru

ForeverNoob said
OK, this is what I'm going to do: I'll create 2 TrayIcons, each with it's own icon, and just hide or show the relevant one as needed. No use fighting what looks like a bug somewhere.
Thank you all for the time and effort.
You're a genius, and it works. Here my modified code.
Code (gambas)
- ' Gambas class file
- .Height = 300
- .Width = 650
- .Arrangement = Arrange.Vertical
- .Padding = 5
- .Height = 28
- .Alignment = Align.Center
- .Tooltip = "Connected"
- .Tooltip = "Not Connected"
- .Delay = 3000
- .Trigger
- .Start
- TrayIcon1.Show
- TrayIcon2.Hide
- TrayIcon1.Tooltip = "Connected"
- Label1.Text = "Connected"
- TrayIcon1.Hide
- TrayIcon2.Show
- TrayIcon1.Tooltip = "Not connected"
- Label1.Text = "Not connected"
- bSwitch = Not bSwitch
- TrayIcon1.Delete 'This stops the program from continuing to run
Posted
Trainee
cogier said
ForeverNoob said
OK, this is what I'm going to do: I'll create 2 TrayIcons, each with it's own icon, and just hide or show the relevant one as needed. No use fighting what looks like a bug somewhere.
Thank you all for the time and effort.
You're a genius, and it works. Here's my modified code.Code (gambas)
' Gambas class file .Height = 300 .Width = 650 .Arrangement = Arrange.Vertical .Padding = 5 .Height = 28 .Alignment = Align.Center .Tooltip = "Connected" .Tooltip = "Not Connected" .Delay = 3000 .Trigger .Start TrayIcon1.Show TrayIcon2.Hide TrayIcon1.Tooltip = "Connected" Label1.Text = "Connected" TrayIcon1.Hide TrayIcon2.Show TrayIcon1.Tooltip = "Not connected" Label1.Text = "Not connected" bSwitch = Not bSwitch TrayIcon1.Delete 'This stops the program from continuing to run
1 guest and 0 members have just viewed this.






