Author Topic: Button back image  (Read 1679 times)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Button back image
« on: January 26, 2016, 11:20:19 PM »
How can I access the Back Ground image of a button for control purposes.  such as

If Button1.BackGround image=xxxx Then

 I want to use the image in the same manner as the text to create a single button for two operations. ie..Start/Stop.  Thanks
 

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Button back image
« Reply #1 on: January 27, 2016, 05:49:23 AM »
Probably the easiest way is to add your images to the resources like this:

- Right click the AdvancedHMI project in Solution Explorer
- Select Properties
- Select the Resources tab
- Add Resource->Add Existing File
- Browse to your images

Now that your images are in the Resources, you can add them to your button either with the Properties Window or via code:
Code: [Select]
BasicButton1.BackgroundImage = My.Resources.AdvancedHMILogoBR

The you can check which image is currently on the button like this:
Code: [Select]
        If BasicButton1.BackgroundImage Is My.Resources.AdvancedHMILogoBR Then

        End If

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Button back image
« Reply #2 on: January 27, 2016, 09:19:25 PM »
This doesn't seem to let me change the Back Ground Image at run time.  My images are in my resources.  I was hoping to change this property on the button click.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Button back image
« Reply #3 on: January 27, 2016, 10:12:43 PM »
It turns out a little more complicated than I thought. I created a simple image comparison function. I made a demo by adding a regular Button to the form and making it switch between 2 images. This is the code:
Code: [Select]
    Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
        If CompareImages(Button1.BackgroundImage, My.Resources.AdvancedHMILogoBR) Then
            Button1.BackgroundImage = My.Resources.RoundMeterNeedle
        Else
            Button1.BackgroundImage = My.Resources.AdvancedHMILogoBR
        End If
    End Sub

    Private Function CompareImages(ByRef Image1 As Bitmap, ByRef image2 As Bitmap) As Boolean
        If Image1 Is Nothing AndAlso image2 IsNot Nothing Then
            Return False
        End If

        If image2 Is Nothing AndAlso Image1 IsNot Nothing Then
            Return False
        End If

        If Image1.Size.Width = image2.Size.Width AndAlso Image1.Size.Height = image2.Size.Height Then
            If Image1.GetPixel(0, 0).A = image2.GetPixel(0, 0).A And Image1.GetPixel(0, 0).R = image2.GetPixel(0, 0).R And
                Image1.GetPixel(0, 0).G = image2.GetPixel(0, 0).G And Image1.GetPixel(0, 0).B = image2.GetPixel(0, 0).B Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Button back image
« Reply #4 on: January 27, 2016, 10:29:01 PM »
Thanks, Maybe I will just use two buttons and alternate visibility :)