3 Replies - 803 Views - Last Post: 27 December 2012 - 01:49 AM Rate Topic: -----

#1 ImminentFate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-December 12

Changing PictureBox images through the use of "sender" and 

Posted 26 December 2012 - 05:56 AM

I am trying to create a program that has many pictureboxes as enable/disable sliders (think ios settings) and text that shows on/off.
Posted Image

What i'm trying to do (since I have a total of 16 images and text pieces) is manage them using the following code:
The code should check if the picturebox is showing a gray bar, and if it is, make it blue.
It should also modify the label next to it to show on/off. the label's name is the name of the picturebox + "onoff", for example, the picturebox name is "allowip" and the on/off Label is named allowiponoff.


 Private Sub propertiesonoff_Click(sender As System.Object, e As System.EventArgs) Handles allowip.Click,enablequery.Click,dnserrorhandling.Click, .....

        Dim sendertxt As Object
        sendertxt = sender.GetType.Name + "onoff"
        Dim imgg As Bitmap = My.Resources.sliderbar_grey

        If sender.Image.Equals(My.Resources.Resources.sliderbar_grey) Then
            sender.Image = My.Resources.Resources.sliderbar_blue
            sendertxt.text = "On"
        ElseIf sender.Image.Equals(My.Resources.sliderbar_blue) Then
            sender.Image = My.Resources.sliderbar_grey
            sendertxt.text = "Off"
        End If

    End Sub


I've found that I can modify the image if I don't have an "If" statement. as soon as I try to check for the current image in the picturebox, it no longer works. I've tried using "Ctype" and "DirectCast" for the picturbox, but it doesn't work either.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Changing PictureBox images through the use of "sender" and 

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Changing PictureBox images through the use of "sender" and 

Posted 26 December 2012 - 07:52 AM

View PostImminentFate, on 26 December 2012 - 06:56 AM, said:

I've found that I can modify the image if I don't have an "If" statement. as soon as I try to check for the current image in the picturebox, it no longer works. I've tried using "Ctype" and "DirectCast" for the picturbox, but it doesn't work either.

I had a similar propblem, and finally figured out that once the picturebox is loaded with an image via .Image = ..., is is no longer associated with the file (or probably resource), so you have to find some characteristic of the image itself that can be reliably taken as an indication of which bitmap is forming the image.

In my case, I could do it by using the pbox_Foo.Image.Width property. Perhaps you could use the colour of a pixel.

If you can't find anything like that to check, perhaps you could temproraily save the Resource in the Application.Directory, and use .Image.Location to load it. Then, you could test the Location property. You could then delete the saved files in the Form.Closing event (or not, if it doesn't matter).

On the other hand, there may just be some other way I haven't thought of or noticed, that would be better.
Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Changing PictureBox images through the use of "sender" and 

Posted 26 December 2012 - 08:12 AM

To add a note on what lar3ry suggested, every control in vb has a Tag property.
Since your have only two choices, you can use t to store the status of the picture box, either on or of.
Just you can say:
If sender.Tag = "On" Then
    'do off things
    sender.Tag = "Of"
Else
    'do on things
    sender.Tag = "On"
End If

Or something like that...
Was This Post Helpful? 1
  • +
  • -

#4 ImminentFate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-December 12

Re: Changing PictureBox images through the use of "sender" and 

Posted 27 December 2012 - 01:49 AM

Thanks guys, I managed to work with your suggestions by using the backcolor of the picturebox as the variable, and changing it under the if command. I also managed to use a string array to gather the name of the text (called it f())


If sender.BackColor = Color.White Then
            sender.Image = My.Resources.sliderbar_blue
            sender.BackColor = Color.Gainsboro
            f(0).Text = "On"
        ElseIf sender.BackColor = Color.Gainsboro Then
            sender.Image = My.Resources.sliderbar_grey
            sender.BackColor = Color.White
            f(0).Text = "Off"
        End If

New question:
Say I had a Setting under My.Settings that had the same name as the picturebox. How would I go about saving data to that settings string when I click on the picturebox?
So that if I click on the picturebox named "allowip", the setting My.Settings.allowip would get the value of "on"

I tried using the same scheme I used to get the name of the on/off text

Dim f() As Control = Controls.Find(sendertxt, True)


replacing the control with My.settings, but it won't work.

I guess what I need is some way to get a Setting by entering its name, so that the state of the switches can be rememebered when the form is closed.

Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1