Results 1 to 19 of 19

Thread: [RESOLVED] Changing the option of a variable

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Resolved [RESOLVED] Changing the option of a variable

    Hi all,

    I'm making a program, and it has 8 buttons. Since each of the buttons needs to have a very similar set of code attatched to it (differing only by the button number), I thought I might save some code-space and shorten it by having each of the buttons simply set a global variable with the number, and call a public function. The button code(s) are:

    Code:
    Private Sub Slot1_Click()
    DrinkRef = 1
    Call CallDrink
    End Sub
    The function "CallDrink" then has to make a comparison to see if the picture in a certain picturebox matches another, standard picture. Each button has it's own picture. I tried to do this by concatenating the name of the image by taking a variable and adding the beginning part of the string, the number, and the last part (the button's name):

    Code:
    Public Function CallDrink()
    Dim WordCombine As String
    
    WordCombine = "Slot" & DrinkRef & "LED"
    
    If WordCombine.Picture = LightOn.Picture Then
        MsgBox "Hi"
    End If
    
    End Function
    For example, if the button clicked was "1", the DrinkRef would be "1", and therefore WordCombine would be: "Slot" & 1 & "LED", or "Slot1LED", which is the button's name.

    However, I cannot do "WordCombine.Picture", as WordCombine is just a basic string. Is there a way I can get around the phrasing of this so I can make this work? If not, no worries, I'll just put the code into each seperate button.

    Thanks!
    Last edited by BubbleLife; Oct 26th, 2006 at 09:19 AM.

  2. #2
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Talking Re: Changing the option of a variable

    Public Function CallDrink()
    Dim WordCombine As String

    WordCombine = "Slot" & DrinkRef & "LED"

    If WordCombine.Picture = LightOn.Picture Then
    MsgBox "Hi"
    End If

    End Function
    comment: you can also named the button with array... sample slot(0), slot(1), slot(2)... then add one parameter on your CallDrink function, that looks like CallDrink(slot as integer)... just an idea to shorten your code... hehe, just came out in my mind. hope it helps...

    but under CallDrink function i didn't understand what your goal here...

    wordcombine is a picturebox or a button? i think this line
    VB Code:
    1. WordCombine = "Slot" & DrinkRef & "LED"
    is not a valid code.
    you should specify what property do you assigned that value.

    noielen
    Last edited by noielen; Oct 25th, 2006 at 08:30 PM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Changing the option of a variable

    WordCombine is a string
    Basically, when the user clicks on a button, it sets a global variable, "DrinkRef", with a number 1-8 corrosponding to the button number. It then calls CallDrink.

    There are 8 buttons (named Slot1, Slot2, etc. But that isn't important here), and there are also 8 pictureboxes, named "Slot1LED", "Slot2LED", and etc, up to "Slot8LED". Each box corrosponds to it's own button.

    Therefore, what I am trying to do is, when CallDrink is called, it first makes a string that should corrospond to a button name. It does this by physically adding the text to the button number, which is saved in "DrinkRef". It then tries to do a comparison to see if the picture in the picturebox that corrosponds to the pressed button matches one static image, called "LightOn".

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Changing the option of a variable

    Quote Originally Posted by Arby
    However, I cannot do "WordCombine.Picture", as WordCombine is just a basic string. Is there a way I can get around the phrasing of this so I can make this work?
    Why you use a Function? it must return something? And why Public, It's called from another Form?
    If WordCombine is a String that contain a valid Control name, then you can use Me.Controls(WordCombine) to reference that control..
    In your Form:
    VB Code:
    1. Private Sub CallDrink()
    2. Dim WordCombine As String
    3.  
    4. WordCombine = "Slot" & DrinkRef & "LED"
    5.  
    6. If Me.Controls(WordCombine).Picture = LightOn.Picture Then
    7.     MsgBox "Hi"
    8. End If
    9. End Function
    One more thing: 2 pictures cannot be compared like that, you can just compare the path where they are stored in the HD or else, you'll have to loop inside pixels to compare pixel by pixel.

  5. #5
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: Changing the option of a variable

    i think one of the things that can help you this by naming your controls into an array.
    like for example:
    VB Code:
    1. '' YOUR SLOT BUTTON
    2. Private Sub Slot_Click(Index As Integer)
    3.  
    4. Call CallDrink(Index)
    5.  
    6. End Sub
    7.  
    8. '' YOUR FUNCTION
    9. Public Function CallDrink(intslot As Integer)
    10.  
    11. If SlotLED(intslot).Picture  = LightOn.Picture Then
    12.     MsgBox "Hi"
    13. End If
    14.  
    15. End Function
    OR
    VB Code:
    1. Private Sub Slot_Click(Index As Integer)
    2.  
    3. If SlotLED(Index).Picture  = LightOn.Picture Then
    4.     MsgBox "Hi"
    5. End If
    6.  
    7. End Sub
    Simple... hehe lol
    Last edited by noielen; Oct 25th, 2006 at 09:44 PM.
    noister
    <advertising link removed by moderator>

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Changing the option of a variable

    you should use a control array, first of all, for both your buttons and pictureboxes. you can do this first simply by copying and pasting your picturebox or button and pasting them, since they will have the same name you will get a prompt asking you if you would like to create a control array. say yes, and there you go. if all goes well, you should have your 8 buttons and 8 picture boxes, numbered 0 through 7. then, you can do something like this, assuming your button control array is named drinkButton() and your pictureboxes are SlotLED().
    VB Code:
    1. Private Sub drinkButton_Click(Index as Integer)
    2.   'set the drink number
    3.   drinkRef = Index + 1 '(since the array starts at 0, you just add 1 to make the right drink number)
    4.   Call CallDrink
    5. End Sub
    6. 'use a sub for CallDrink because I assume from what you've given me
    7. 'that you're not outputting anything. Subs should be used when nothing
    8. 'is being output to the user, functions should be used when something
    9. 'is being output
    10. Private Sub CallDrink()
    11.   'we just reference the slot using DrinkRef - 1, which will recreate the original array numbers
    12.   If SlotLED(DrinkRef - 1).Picture = LightOn.Picture Then
    13.     'do something
    14.   Else
    15.     'do something else?
    16.   End If
    17. End Sub
    Normally, DrinkRef would be useless if you were just using these two subs. However, if you are doing multiple things that you can't get input from a user as to which drink it is, using a global variable like DrinkRef is a good idea. I left it in because of this, otherwise just making the CallDrink sub have an input of which drink it was would be more efficient, so that you could just call it using: Call CallDrink(Index) in the drinkButton_Click event.

    hope that helps you out a little. feel free to ask questions if you don't understand arrays.
    Last edited by kows; Oct 25th, 2006 at 09:46 PM.
    Like Archer? Check out some Sterling Archer quotes.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Changing the option of a variable

    Ah, jcis - that me.controls thing worked perfectly, thank you!

    Thank you also to everyone else who replied . I know that Arrays are a good way of doing things, but whenever I try and use them I always seem to screw it up.. Plus I have to do other things with these buttons in another form, so it's probably best if I stick to what I know ^_^;.

    But thank you anyway!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Changing the option of a variable

    Ah, this doesn't work for all types of commands, such as:

    Code:
    Me.Controls(WordCombine) = Me.Controls(WordCombine) - 1
    MsgBox Me.Controls(WordCombine)
    How would I do it for other commands?
    Thanks ^_^

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Changing the option of a variable

    You can't MsgBox an object, it has to be an string or number (properties), like:
    VB Code:
    1. MsgBox Me.Controls(WordCombine).Name
    2. MsgBox Me.Controls(WordCombine).Left
    3. MsgBox Me.Controls(WordCombine).Backcolor
    If you want to select another control then create the String WordCombine again.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Changing the option of a variable

    How about making it wordcombine = wordcombine -1 (as in the first line)?

    Is there another way to do it rather than me.controls?

  11. #11
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Changing the option of a variable

    wordcombine has name(string), you can't make mathematic operations with it.
    What are you trying to accomplish with that -1?

  12. #12
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Changing the option of a variable

    What are you trying to subtract 1 from?

    Are you decrementing a numeric property? Then you will have to specify the property on both sides of the equation.

    Are you decrementing the reference or trying to assign previous wordcombine control to wordcombine control specified by "Slot1LED"?

    So what are you try to subtract from?

    EDIT: Hehehe same question

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Changing the option of a variable

    I'm trying to -1 from itself.
    Wordcombine is a string which has the text (in this instance),
    Slot" & DrinkRef & "Qty"

    Where DrinkRef is the number of whichever button they clicked. Slot3Qty (for example) is a numeric (integer) value that will contain some number. When the button is clicked, one of the things it will need to do is decrease this number.

    If I put the code in each button, it would say (for button 5):
    Slot5Qty = Slot5Qty - 1
    But because I'm trying to do it all in one function, the function needs to know which button to refer to. That's why I'm trying to "make" the variable names by declaring a string (WordCombine), and filling it with the "word" parts of the variable, which are the same in each case ("Slot" and "Qty" in this case), with the unique number in the middle

    Maybe it would be easier to just put individual code in each button...

  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Changing the option of a variable

    That still doesn't make sense... your subtracting 1 from the number in the caption property of the button? Subtracting 1 from another property of the button such as Tag property?

    Or does your "subtraction" mean something else.... like subtracting 1 from the DrinkRef portion of your key?

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Changing the option of a variable

    Uhh... *scratches head*

    WordCombine may equal (for example):
    Slot6Qty
    Slot6Qty is the name of a declared variable (an Integer).
    The value of the variable (Slot6Qty, in this case) needs to be reduced by 1.

    If I was coding it individually in each button, in button 1 I would put
    Slot1Qty = Slot1Qty - 1
    In button 2 I would put
    Slot2Qty = Slot3Qty - 1

    And so on up until button 8.
    Because I'm trying to make one piece of code to do all 8 though, I thought I would make is simpler by making a string variable (WordCombine), making the value of that variable equal the name of a real variable (Slot, 3, Qty (Slot3Qty), and then perform operations on it.

    When I got the first reply for finding the picture box, I used the suggestion:
    Msgbox Me.controls(WordCombine).picture

    Since .picture is a property of an imagebox, I assumed that you could just take anything from a variable or object. I see what you're saying though, "Slot4LED" was the name of an object, not a variable, and it uses a property (.picture).

    What I want to do is use the string WordCombine to perform an operation on a STRING, I'm not sure if strings have properties (E.g. "String1.value"), but nothing shows up when I try typing "WordCombine." (Waiting for properties to appear).

    Maybe "Me.controls" is not the best thing to use. Is there a way I can do this to a string? What I want to acchieve is something like:

    Code:
    Dim Value as Integer
    Dim WordCombine as string
    Dim But1 as integer
    Dim But2 as integer
    
    But1 = 20
    But2 = 20
    
    Private Sub Button1_Click()
    Value = 1
    Call TestFunc
    End Sub
    
    Private Sub Button2_Click()
    Value = 2
    Call TestFunc
    End Sub
    
    Public Function TestFunc
    
    WordCombine = "But" & Value
    ' If "Button2" was clicked, WordCombine would now equal "But2" - the name of a variable
    
    WordCombine = WordCombine - 1
    ' If I was doing this in each button, I would put "But1 = But1 - 1" or "But2 = But2 - 1", but I'm trying to shorten it :)
    
    End Function

  16. #16
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Melbourne, Australia
    Posts
    415

    Re: Changing the option of a variable

    ooooooooooh I get it now, you're trying to change the variables But1 and But2 now, by using the string.. I'm almost positive you can't do that. You should really use an array of integers. If the 0-baseing is throwing you off, you can declare it from 1-8

    VB Code:
    1. Dim But(1 to 8) as Integer
    2. But(1) = 20
    3. But(2) = 20
    4.  
    5.  
    6. Public Function TestFunc
    7. But(Value)=But(Value)-1
    8. End Function

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Changing the option of a variable

    Aiya, Integers.. I can track them 0-7, it's just that manipulating Integers has never been my strong point, especially since I'll have to do a lot more stuff with the buttons in another form ^_^;;;;.

    Ah well, at least I have a definite reply. Thanks for the advice!

  18. #18
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Melbourne, Australia
    Posts
    415

    Re: [RESOLVED] Changing the option of a variable

    Actually, I believe you can access the variable with a string using a Script function, which I haven't looked at too much, and is probably more trouble than its worth

  19. #19
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Changing the option of a variable

    No you can't... the script can't do the assignment operation in the string, it will be evalutaed as an 'is equal' comparison. Arby, your treating a structured language (VB) as a declarative language (eg. SQL)... the compiler will never agree with whatever code you throw at it cause your not speaking its language.

    You could just call this in the button click event.
    VB Code:
    1. Private Sub Button1_Click()
    2.    Call Decrement(But1)
    3. End Sub
    4.  
    5. Public Sub Decrement(ByRef X as Integer)
    6.    x = x - 1
    7. End Sub
    No need to maintain extra variables:
    Dim Value as Integer
    Dim WordCombine as string
    Last edited by leinad31; Oct 27th, 2006 at 12:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width