Author Topic: Displaying more than one picture  (Read 1287 times)

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Displaying more than one picture
« on: March 10, 2017, 11:27:29 AM »
I currently have a DataSubscriber with DataChanged event. Depending on the value , it will display the selected picture.
Code: [Select]
Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged

        Select Case e.Values(0)
            Case "0"
                PictureBox2.BackgroundImage = My.Resources.AdvancedHMILogoBR
            Case "1"
                PictureBox2.BackgroundImage = My.Resources.AdvancedHMILogoBR
            Case "2"
                PictureBox2.BackgroundImage = My.Resources.AdvancedHMILogoBR
            Case "3"

Now, let's say when it's case "1", I'd like to display more than one picture, that is pic1 for 2 seconds, pic2 for 2 seconds, then repeat the sequence as long as the value is still at "1"
so I modified case "1" like below:
Code: [Select]
Case "1"
        While e.Values(0) = 1
            PictureBox2.BackgroundImage = My.Resources.Pic1
            Application.DoEvents()
            Sleep(2000)
            PictureBox2.BackgroundImage = My.Resources.Pic2
            Application.DoEvents()
            Sleep(2000)
        End While
      
This works as intended, but as my e.values changes, different pictures are not displaying. Do you see anything wrong with the code? TIA
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Displaying more than one picture
« Reply #1 on: March 10, 2017, 11:53:14 AM »
In your first case, the ImageDisplayByValue would do this without writing the code.

In your second case, you are doing 2 things are highly recommended to avoid. The DoEvents and the Sleep. The Sleep will execute on your UI thread and freeze it.

A better option is to create a Blink timer that will toggle between the pictures. Store the pictures in Private variables outside of the subroutines for both the DataChanged and the blink timer to use.

Code: [Select]
    Private Image1 As Bitmap
    Private Image2 As Bitmap
    Private LastState As Integer
    Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
        If LastState > 0 Then
            PictureBox1.Image = Image2
            PictureBox1.Invalidate()
            LastState = 0
        Else
            PictureBox1.Image = Image1
            PictureBox1.Invalidate()
            LastState = 1
        End If
    End Sub

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Displaying more than one picture
« Reply #2 on: March 10, 2017, 12:06:15 PM »
 ImageDisplayByValue use ImageList, I think it can only display small size pic 256x256, which is why I use picturebox.

I thought about using timer and tick , but not sure how I can do it within the DataChanged event?
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Displaying more than one picture
« Reply #3 on: March 10, 2017, 12:59:09 PM »
In your DataChanged event handler you would point the private variables to the images you want to show:

Code: [Select]
    Private Sub DataSubscriber1_DataChanged_1(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
        If e.ErrorId = 0 Then
            Select Case e.Values(0)
                Case 1
                    Image1 = My.Resources.Pic1
                    Image2 = My.Resources.Pic2
                Case 2
                    Image1 = My.Resources.Pic3
                    Image2 = My.Resources.Pic4
            End Select
        End If
    End Sub

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Displaying more than one picture
« Reply #4 on: March 10, 2017, 01:31:03 PM »
Super! Thanks, Archie.

P.S.   Picturebox1 is reserved for the super logo. Cant touch that !
« Last Edit: March 10, 2017, 01:35:32 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================