random buttons in VB

Status
Not open for further replies.

jesspren6

In Runtime
Messages
266
I'm making a simple program that there are many command buttons with numbers on them. I want the program to choose a random button each time the program is opened. Then when the person guesses the right button, it will take it to another form. I know how to make it go to another form, but i do need help on how it will choose a random number each time the program is opened.
 
I'm not sure what he used to make his, or if *he* was the one who did it, but the man at http://www.ghisler.com used what appears to be this technique in his software "Total Commander" for the unregistered version. Maybe you could get in touch with him?
 
http://msdn2.microsoft.com/en-us/library/f7s023d2.aspx

Its been ages since iv done VB but that link above would be a good starting point and looking on google i found a few sites with examples but i`ll let you do that your self ;). I think basically you would have each button with an index and use that as the rnd instead of numbers so it`ll randomise between say 6 buttons and maybe a variable for when its chosen to be set to 1 or something...and then do an if statement to say if button 1 = true open form etc.

god i need to brush up on my programming me thinks :D
 
If the buttons are always the same, but what changes is which is the correct button to push, then it's very simple (the last post almost gave it away): when the form opens, randomize an integer (from 1 to the number of buttons you want). Then, just assign a number (in your head) to each button and put a check in the button's code: if the random number matches the preset number for that button, then you go to the next form.

On Form load:
Randomize()
RndNum = Int(Rnd() * n + 1) //where "n" is th number of buttons

Buttons:
Button1_Click: "If RndNum=1 Then form2.show"
Button2_Click: "If RndNum=2 Then form2.show"
etc...

Since the correct button is fixed, all he'll have to do is click them all until one works (rather silly). If you want to randomize the buttons again after he makes a try (so he really has to guess), just put the RndNum = Int(Rnd() * n + 1) line in the code of each button before the check.

Hope this helps.

Edit: by the way, are you using VB6 or VB.NET (or even DOS Basic)?
 
good stuff Meithan I just did that in visual studio and it worked like a charm I new i was on the right track somewhere it should help out jesspren6 no problem.

I forgot how nice VB is to code and use although c# is just as nice.
 
Yeah, Visual Basic is a breeze. I know C++ is more powerful, but for small, quick Windows apps, I think there's nothing like Visual Studio 2005. I absolutely love it. I'm a VB6 user migrating from Visual Studio 6, and boy are there nice new features! It makes placing controls and such on your form very quick (and it looks nice, too!), so you can concentrate on the code.

Still waiting for jesspren6 to reply...
 
Status
Not open for further replies.
Back
Top Bottom