TrayIcon picture not updating

Post your Gambas programming questions here.
User avatar
cogier
Site Admin
Posts: 637
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: TrayIcon picture not updating

Post by cogier » Thursday 28th January 2021 5:04pm

ForeverNoob wrote:
Thursday 28th January 2021 3:55pm

Hi cogier,

Thanks for answering. I've copied the above code to a new form and edited the relevant part to look like this:
  If bSwitch = False Then
    TrayIcon1.Tooltip = "Connected"
    TrayIcon1.Picture = Picture["icon1.png"]
    Label1.Text = "Connected"
  Else
    TrayIcon1.Tooltip = "Not Connected"
    TrayIcon1.Picture = Picture["icon2.png"]
    Label1.Text = "Not connected"
  End If
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'
TrayIcon1.Hide                                    'This is an important part
TrayIcon1.Show                                  'This is an important part

User avatar
cogier
Site Admin
Posts: 637
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: TrayIcon picture not updating

Post by cogier » Thursday 28th January 2021 5:15pm

ForeverNoob wrote:
Thursday 28th January 2021 3:58pm
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.
' Gambas class file

bSwitch As Boolean
Timer1 As Timer
TrayIcon1 As Trayicon
TrayIcon2 As Trayicon
Label1 As Label

Public Sub Form_Open()

  With Me
    .Height = 300
    .Width = 650
    .Arrangement = Arrange.Vertical
    .Padding = 5
  End With

  With Label1 = New Label(Me) As "Label1"
    .Expand = True
    .Height = 28
    .Font.Size = 60
    .Font.Bold = True
    .Alignment = Align.Center
  End With

  With TrayIcon1 = New TrayIcon As "TrayIcon1"
    .Tooltip = "Connected"
    .Picture = Picture["icon:/22/connect"]
  End With

  With TrayIcon2 = New TrayIcon As "TrayIcon2"
    .Tooltip = "Not Connected"
    .Picture = Picture["icon:/22/close"]
  End With

  With Timer1 = New Timer As "Timer1"
    .Delay = 3000
    .Trigger
    .Start
  End With

End

Public Sub Timer1_Timer()

  If bSwitch = False Then
    TrayIcon1.Show
    TrayIcon2.Hide
    TrayIcon1.Tooltip = "Connected"
    Label1.Text = "Connected"
  Else
    TrayIcon1.Hide
    TrayIcon2.Show
    TrayIcon1.Tooltip = "Not connected"
    Label1.Text = "Not connected"
  End If

  bSwitch = Not bSwitch

End

Public Sub Form_Close()

  TrayIcon1.Delete                                  'This stops the program from continuing to run

End

ForeverNoob
Posts: 6
Joined: Thursday 01st October 2020 10:11am

Re: TrayIcon picture not updating

Post by ForeverNoob » Thursday 28th January 2021 5:29pm

cogier wrote:
Thursday 28th January 2021 5:15pm
ForeverNoob wrote:
Thursday 28th January 2021 3:58pm
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.
' Gambas class file

bSwitch As Boolean
Timer1 As Timer
TrayIcon1 As Trayicon
TrayIcon2 As Trayicon
Label1 As Label

Public Sub Form_Open()

  With Me
    .Height = 300
    .Width = 650
    .Arrangement = Arrange.Vertical
    .Padding = 5
  End With

  With Label1 = New Label(Me) As "Label1"
    .Expand = True
    .Height = 28
    .Font.Size = 60
    .Font.Bold = True
    .Alignment = Align.Center
  End With

  With TrayIcon1 = New TrayIcon As "TrayIcon1"
    .Tooltip = "Connected"
    .Picture = Picture["icon:/22/connect"]
  End With

  With TrayIcon2 = New TrayIcon As "TrayIcon2"
    .Tooltip = "Not Connected"
    .Picture = Picture["icon:/22/close"]
  End With

  With Timer1 = New Timer As "Timer1"
    .Delay = 3000
    .Trigger
    .Start
  End With

End

Public Sub Timer1_Timer()

  If bSwitch = False Then
    TrayIcon1.Show
    TrayIcon2.Hide
    TrayIcon1.Tooltip = "Connected"
    Label1.Text = "Connected"
  Else
    TrayIcon1.Hide
    TrayIcon2.Show
    TrayIcon1.Tooltip = "Not connected"
    Label1.Text = "Not connected"
  End If

  bSwitch = Not bSwitch

End

Public Sub Form_Close()

  TrayIcon1.Delete                                  'This stops the program from continuing to run

End
:D Thanks a lot!

Post Reply