hexagon logo

Form editor: changing image properties with variables

So I'm pretty new to the VB language and even newer to the PC-DMIS form editor. I don't usually have problems with variables in Visual Studio, but with the Form Editor I can't seem to get past the simple declaration of a variable. (For instance, Dim Variable As Integer is no problem, but Variable = [anything I try] somehow causes some kind of syntax error. The editor will not accept it.)

What I'm trying to do is use a variable (or any other method really) to change the property of a Bitmap control to change the image that is displayed. Think of setup instructions with multiple pictures which can be viewed with NEXT/PREVIOUS buttons. Or a probe calibration program that will show the image of the probe configuration depending on which Radio Button is currently selected.

But no matter what I do, I can't seem to change the .bitmap property with the form running. I've tried using EventClick from within the Bitmap control. I've also tried using the Event[something like "ValueChange" but I can't remember] with the Radio Button control. I can get those events to do other things, like show a MsgBox, but the Bitmap control won't show a different image. Can anyone help me out?
  • Dim strImgPath

    strImgPath = "C:\Temp\pic1.bmp"
    bitmap1.Bitmap = strImgPath
  • Okay, here's what I've got so far:

    In the form's EventInitialize handler I have the following code:
    Dim strImgPath as StringstrImgPath="yaddayadda\master.jpg"
    Bitmap1.Bitmap=strImgPath

    ("yaddayadda" is an actual file path")

    So far so good. The form loads with the correct picture showing.

    For the Bitmap1 EventClick handler:
    strImgPath="yaddayadda\sunset.jpg"
    msgbox("click")


    Form loads displaying master.jpg. I click on the picture, but the image does not change. I do see a message box that says "click."

    What am I missing?
  • All you've done is set the value of the variable, you haven't set the bitmap property to the variable's new value.
  • Huh. Seems strange that I have to set the bitmap property to the variable again, but it does work after I add this line to the click event:
    This.Bitmap=strImgPath


    Thanks for the tip, Badger. Let's see if you can figure this one out, too.

    I have difficulty when I try to change the same image using the radio button event handler, EventChange. I have 6 radio buttons, values from 1 to 6. I've created 6 .jpg files all named "PROBEPIC"+[number bt 1 and 6]. I add this code to the event:
    strImgPath="yaddayadda\probepic" + this.value + ".jpg"
    Bitmap1.Bitmap=strImgPath
    MsgBox("path is"+strImgPath)


    This time when I run the form I click on a radio button. The image does not change. The message box appears with only the text "1" or the value of the selected button. Apparently the value of my string variable is equal to this.value. Why is that?
  • Oy. Right after I posted I tried concatenating with & instead of +. NOW it works!

    Boy, I thought the learning curve would be smaller after learning VB...

    I need to spread some rep around. Consider this an IOU.
  • What flavour of VB have you learnt?

    I always use the ampersand operator (&Wink for concatenating strings whatever VB I use.

    Also you say it seems strange you have to set the property to the variable again, but that's just the way it is. When you set the property (Bitmap1.Bitmap =strImgPath)...

    Think of the = in that line of code as 'becomes' instead of 'equals'; it's not linked to that variable, it becomes the current value of that variable.

    Which is why the statement...

    i=i+1 (i becomes i +1)

    ...is valid in code but not in maths


    (The clue's in the name, it's a variable, not a constant!)
  • As NinjaBadger says - the code is not a collection of facts, but a recipe that is followed top-down: do this, then do that, on click do this followed by that, etc.
  • Yeah, you're right. I guess I was just expecting the VB code to behave like PC-DMIS code. Even though I learned (sorta) VB 2008 --which uses & to concatenate-- I had gotten used to using + in my Edit Window. Also I wasn't used to setting and re-setting control properties with variables; I just expected it to be more dynamic. But now that you guys explain it, this makes sense to me. The variable is passes to the property at the line [[COLOR=#333333]Bitmap1.Bitmap[/COLOR]= strImgPath], but there is no link between them. When the variable value changes, the property must be updated with another line of code [[COLOR=#333333]Bitmap1.Bitmap[/COLOR]= strImgPath].

    Thanks for taking the time to explain this to me.