|
-
Oct 25th, 2006, 07:46 PM
#1
Thread Starter
Addicted Member
[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.
-
Oct 25th, 2006, 08:25 PM
#2
Fanatic Member
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:
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.
-
Oct 25th, 2006, 09:00 PM
#3
Thread Starter
Addicted Member
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".
-
Oct 25th, 2006, 09:39 PM
#4
Re: Changing the option of a variable
 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:
Private Sub CallDrink()
Dim WordCombine As String
WordCombine = "Slot" & DrinkRef & "LED"
If Me.Controls(WordCombine).Picture = LightOn.Picture Then
MsgBox "Hi"
End If
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.
-
Oct 25th, 2006, 09:41 PM
#5
Fanatic Member
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:
'' YOUR SLOT BUTTON
Private Sub Slot_Click(Index As Integer)
Call CallDrink(Index)
End Sub
'' YOUR FUNCTION
Public Function CallDrink(intslot As Integer)
If SlotLED(intslot).Picture = LightOn.Picture Then
MsgBox "Hi"
End If
End Function
OR
VB Code:
Private Sub Slot_Click(Index As Integer)
If SlotLED(Index).Picture = LightOn.Picture Then
MsgBox "Hi"
End If
End Sub
Simple... hehe lol
Last edited by noielen; Oct 25th, 2006 at 09:44 PM.
noister
<advertising link removed by moderator>
-
Oct 25th, 2006, 09:41 PM
#6
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:
Private Sub drinkButton_Click(Index as Integer)
'set the drink number
drinkRef = Index + 1 '(since the array starts at 0, you just add 1 to make the right drink number)
Call CallDrink
End Sub
'use a sub for CallDrink because I assume from what you've given me
'that you're not outputting anything. Subs should be used when nothing
'is being output to the user, functions should be used when something
'is being output
Private Sub CallDrink()
'we just reference the slot using DrinkRef - 1, which will recreate the original array numbers
If SlotLED(DrinkRef - 1).Picture = LightOn.Picture Then
'do something
Else
'do something else?
End If
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.
-
Oct 26th, 2006, 07:09 AM
#7
Thread Starter
Addicted Member
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!
-
Oct 26th, 2006, 09:19 AM
#8
Thread Starter
Addicted Member
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 ^_^
-
Oct 26th, 2006, 11:13 AM
#9
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:
MsgBox Me.Controls(WordCombine).Name
MsgBox Me.Controls(WordCombine).Left
MsgBox Me.Controls(WordCombine).Backcolor
If you want to select another control then create the String WordCombine again.
-
Oct 26th, 2006, 11:34 AM
#10
Thread Starter
Addicted Member
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?
-
Oct 26th, 2006, 11:39 AM
#11
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?
-
Oct 26th, 2006, 11:42 AM
#12
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
-
Oct 27th, 2006, 08:46 AM
#13
Thread Starter
Addicted Member
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...
-
Oct 27th, 2006, 08:52 AM
#14
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?
-
Oct 27th, 2006, 09:13 AM
#15
Thread Starter
Addicted Member
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
-
Oct 27th, 2006, 11:19 AM
#16
Hyperactive Member
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:
Dim But(1 to 8) as Integer
But(1) = 20
But(2) = 20
Public Function TestFunc
But(Value)=But(Value)-1
End Function
-
Oct 27th, 2006, 11:22 AM
#17
Thread Starter
Addicted Member
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!
-
Oct 27th, 2006, 11:27 AM
#18
Hyperactive Member
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
-
Oct 27th, 2006, 12:10 PM
#19
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:
Private Sub Button1_Click()
Call Decrement(But1)
End Sub
Public Sub Decrement(ByRef X as Integer)
x = x - 1
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|