Any Visual Basic experts out there?

Rajah

DIS Veteran
Joined
Aug 17, 1999
I have 5 buttons. They're labled button1 button2 button3 button4 and button5.

I have some code associated with each button that, other than the number behind the word button, is exactly the same for each button.

Is there a way in VB to do something like take a variable "counter", set it to some number between 1 and 5, and change an attribute based on something like "button + counter".Caption = Hi!

I know how to do it in Cold Fusion, I know in other languages you can do it, but I know in yet others you can't. Anyone know the answer in Visual Basic?
 
I wish I could help, but I'm a little confused by the question. Do you mean that everytime the counter increases, a different caption will be displayed when you click on the button?
 
I want to be able to put:

button + counter.Caption = TheText (or whatever the proper format is)

into a function call that is something like giveCaption(counter, TheText)

So that somewhere in the body of a program I can say
Call giveCaption(3, "Hello!!")

And the result is that button3 will get the caption "Hello!!!"

But then later in the program I can do
Call giveCaption(2, "Goodbye!")
Call giveCaption(5, "I love Mickey!")

And have button2's caption turn to "Goodbye" and button5's caption become "I love Mickey!"
 
VB is not real keen on allowing you to build variable names dynamically like that.

You might be able to pull off something using the CallByName Function, but its not a really good fit. My suggestion would be to just have a big case statement like:

Select Case x
Case 1
Button1.Text = "a"
Case 2
Button1.Text = "b"
Case 3
Button1.Text = "c"
End Select
 


Blech! The DIS editor trashed just my code's indentation. I haven't seen unindented code since I was a freshman.
 
Oh ok, I understand what you're saying. There probably is a way to do it but I've never tried it. I wanted to try to do some codes to see if I could figure out how, but everytime I open Visual Basic is says unexpected error, quitting. I'm sorry, I wish I could help.:( I hope you're able to find out a way to do it.
 
LOL! It messed up my tabbing, too, so no problem ;)

A case statement was the next best option I could think of, but if I could get away with writing the code only *once* instead of 5 times, that would have been even better. 5 is better than 15 or 20, but once is better than 5 times.

Thanks.
 


Cold Fusion is probably an interpretted language, where as VB is compiled (or at least it pretends to be compiled). Compiled languages generally do not allow a variable reference in the form of a string, since there is no way to bind it at compile time.
 
If you have a lot of code tied to the button press, you might try a case statement to select the button, and then pass the button object to a subroutine for the rest of the processing. That way you only have to right most of the code once in the subroutine, rather than once for each button.


BTW, I tried playing with the CallByName function, it doesn't help in this case.
 
Ahh -- is that the difference? I do believe CF is interpreted, and now that I think about it all of those that have allowed dynamic variable names that I can remember (can't remember *when* I've done it besides in CF, just that I have) I think were all interpreted as well.

That makes sense. I'll just do the case statement instead.

Thanks!
 
I believe the key to what you are looking for is called a "control array" in Visual Basic. What you actually need is five buttons all with the same name, but each button has a unique index number from 0 to 4 (since VB starts everything with 0). Basically, it's an array of buttons. Have you ever heard of any of this???? :)

Assume your buttons are all boringly named Command1 (the default), and each has the Index property from 0 to 4. Your GiveCaption procedure would look something like this:

Public Sub GiveCaption(buttonnum, newtext)
Command1(buttonnum).Caption = newtext
End Sub

Your code is exactly right to call this procedure, like

Call GiveCaption(3, "Hello")

It should give button#3 the caption of "Hello".

To create buttons with the same name, simply make a button and put it on your form. Then click on it (still in design mode), and then click Edit-Copy on the top toolbar. Then click Edit-Paste, and Visual basic will ask something like "You already have a button named Command1. Do you want to create a control array?" Then click yes. Then another button will appear on the form. This button will have the same name, but the Index property will be 1. Do this three more times to make five buttons total.

OK!!! I know I completely lost you by now, but I think it will work!!!! I think this control array is the crucial piece of code to make this project work properly. :jester: Of course, the case statement might work, too! Have fun!

:) :) :)

<img src="http://home.att.net/~disneysue/hugsmall3.gif">
 
Actually, you didn't lose me -- I just never knew anything like that existed. I'll have to try that tomorrow!! Thanks HugsForEeyore!
 
Hey Hugs -- I tried what you suggested...

That is EXACTLY what I wanted!!! Thank you SO MUCH! I've been able to clean up the code bigtime now! Thanks!!!

Ain't the DIS great? DH couldn't believe I got the answer to a programming question here ;)
 
While I would also suggest the control array as being the cleanest and best codewise solution to be able to deal with this situation, you CAN dynamically do it with the buttons you have.

Assuming the button name of "Button" plus a number, you can code it this way:

Sub GiveCaption (intButtonNumber, strCaption)
Me("Button" & intButtonNumber).Caption = strCaption
End Sub

Because VB is an object based language, you can reference almost everything with that type of syntax. It's not as nice as

Button1.Caption = "Hello World"

but it still works. The syntax is slightly different if you put this in a global module or class, but very close anyway. It would be that way with the other solution also.

Also, not that it probably compiles any different, but the way I call subs is like this :

GiveCaption 1, "Hello World"

as opposed to

Call GiveCaption(1, "Hello World")

Either works. :D
 
Rajah, glad to be of help!!!! I took a class in Visual Basic, and I am glad to finally use what I learned! :) I am so glad you understood what I wrote, since I wrote it at 1:30 in the morning here!

Jfulcer - I had no idea you could code control names like that! I'll keep that in mind since it could come in handy in future projects! :)

<img src="http://home.att.net/~disneysue/hugsmall3.gif">
 
HugsforEeyore,

With VB being object oriented like this, there's lots of room for controlling and accessing the objects. The coolest application I have created used the dynamic control ability to a huge extent - it was a vehicle tracking system that created about 200 dynamic movable text boxes on the screen that the dispatchers used. What was cool about it is that all of them are created while the program is running. It was sweet. Everything is an object.
 

GET A DISNEY VACATION QUOTE

Dreams Unlimited Travel is committed to providing you with the very best vacation planning experience possible. Our Vacation Planners are experts and will share their honest advice to help you have a magical vacation.

Let us help you with your next Disney Vacation!





Top