VB Question

Soldato
Joined
30 Dec 2004
Posts
3,343
Location
London
Hey all,

Just wanted to ask, in VB, I am trying to generate a set of buttons which when clicked in a random order will have the values inside the buttons stored as variables.

How would I go about representing those variables in the order that they were clicked in?

Wondered if anyone could hint me as to how I'd go about doing it.


Thanks
 
Hey all,

Just wanted to ask, in VB, I am trying to generate a set of buttons which when clicked in a random order will have the values inside the buttons stored as variables.

How would I go about representing those variables in the order that they were clicked in?

Wondered if anyone could hint me as to how I'd go about doing it.


Thanks

An array?
A Dictionary Object?

Create an array size to equal the max number of buttons.
In the On_Click button event, add the vaule of that particular button to the next array element. Increase array counter by 1.
 
Last edited:
The queue should be relatively easy:

Code:
Queue q = new Queue()

//Then all the buttons would have the same click handler

q.Enqueue(<yourvalue>)

You could then use a loop to go round the queue, knowing full well that the first value in the queue was selected first etc.

Alternatively it is a fixed amount you might be able to do it with an array? You'd need some sort of mechanism for knowing what order things are going in though.
 
If I click a button, is there anyway to use OR? (not working for me at the moment)

So for example,
Label1 = But1 or But2 or But3

meaning that if I select But3 it will insert it into label1, if I select But1 it will insert it... and so on?
 
If I click a button, is there anyway to use OR? (not working for me at the moment)

So for example,
Label1 = But1 or But2 or But3

meaning that if I select But3 it will insert it into label1, if I select But1 it will insert it... and so on?

You could have a method that all the buttons call to set the value:

Code:
private void SetValue(Label label, string value)
{
label.Text = value;
}

So in each button click event you would have:

Code:
SetValue(label1, <yourvalue>);
 
Not really sure what you want.

You have 3 buttons. They will each output a number to a label.

You want to keep the order of the numbers added to that label?

So for the first button you could use:
Code:
Label1.Text = Label1.Text & But1 & ", "

As I said, I'm not really sure if that's what you want. Please explain more.
 
Sorry, will explain.

3 Buttons have numbers in them.

I want to be able to click button1, button2, and button3 in any order.

The order that I click them in, I want to be displayed in labels.

So if I click button3, I want the value from button3 to go into label1 (first button clicked). If I then click button1 I want the value to go into label2 (Second button clicked), and so on.

Does that make sense??

The value in button1, button2 and button3 are not constants, they are variables so i can't put a set value anywhere.
 
Last edited:
Sorry, will explain.

3 Buttons have numbers in them.

I want to be able to click button1, button2, and button3 in any order.

The order that I click them in, I want to be displayed in labels.

So if I click button3, I want the value from button3 to go into label1 (first button clicked). If I then click button1 I want the value to go into label2 (Second button clicked), and so on.

Does that make sense??


So like this:

Code:
        private void button1_Click(object sender, EventArgs e)
        {
            SetValue(label1, <value>);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            SetValue(label2, <value>);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            SetValue(label3, <value>);
        }

private void SetValue(Label label, string value)
{
label.Text = value;
}
 
dasyad, it doesn't appear to be recognising the Private Void line you have written.

You sure this will work with numbers that are varying?

Not that advanced at VB...


I'm still not sure why it can't work by setting each label equal to all 3 buttons, and they then change depending on what one you click.

So, label1 = But1 OR But2 or But3

Meaning if I click But3, the value for But3, will be inserted into label1...then if I click but1, the value for but1 will be inserted into label1.....??
 
Last edited:
hmm, expanding on dasyad. My syntax may not be exact, its been awhile since i messed in forms.

Code:
Label[] _Labels = new Label[3];
int counter;
private void MyForm_Load(object sender, EventArgs e)
{
    _Labels[0] = this.Label1;
    _Labels[1] = this.Label2;
    _Labels[2] = this.Label3;
    counter = 0;
}

        private void button1_Click(object sender, EventArgs e)
        {
            SetValue(<value>, counter);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            SetValue(<value>, counter);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            SetValue(<value>, counter);
        }

        private void SetValue(string value, int counter)
        {
            _labels[counter].Text = value;

            if(counter <  _labels.length+1)
                counter = counter++;
        }
 
Last edited:
dasyad, it doesn't appear to be recognising the Private Void line you have written.

You sure this will work with numbers that are varying?

Not that advanced at VB...


I'm still not sure why it can't work by setting each label equal to all 3 buttons, and they then change depending on what one you click.

So, label1 = But1 OR But2 or But3

Meaning if I click But3, the value for But3, will be inserted into label1...then if I click but1, the value for but1 will be inserted into label1.....??

Well I don't know what you have called your buttons and labels so they are all just place holders. What I have shown would work for 3 buttons with 3 equivalent labels.

If you have a variable amount of buttons and accompanied labels you would need something that is a little bit smarter. You'd need to dynamically create buttons and labels in pairs, and then keep track of how many pairs you have created.

Also what I wrote was actually C# syntax sorry, I don't know what the VB equivalents are :p. If you know how to pass parameters to methods then you should be good to go.
[EDIT]
So, label1 = But1 OR But2 or But3

Meaning if I click But3, the value for But3, will be inserted into label1...then if I click but1, the value for but1 will be inserted into label1.....??

Unfortunately it cannot do all the hard work for us, this is where as a programmer you need to think of a solution to a problem, it's essentially the aim of the game. If you could change djkav's solution into VB syntax then I think it would do what you need. Good luck!
 
Last edited:
If you have 3 buttons and 3 labels then this could work. Make sure the labels have no text in.

Button1
Code:
If Label1.Text = "" Then
Label1.Text = But1
ElseIf Label2.Text = "" Then
Label2.Text = But1
ElseIf Label3.Text = "" Then
Label3.Text = But1
End If

Replace But1 on other buttons.
 
Thanks for the help guys.

@Craig87; It does... but all it seems to do is put But1 into Label1, But2 into Label2 and so on. It doesn't do it from which I pick first. I.e; if i click on the third button, it fills the Label1 with the value from But1.
 
Thanks for the help guys.

@Craig87; It does... but all it seems to do is put But1 into Label1, But2 into Label2 and so on. It doesn't do it from which I pick first. I.e; if i click on the third button, it fills the Label1 with the value from But1.

Well I didn't have access to VB until now. But it seemed to work for me. Here is the code for all 3 buttons. It is using a set value for But1-3 for testing though.

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim But1 As Integer = 1
        If Label1.Text = "" Then
            Label1.Text = But1
        ElseIf Label2.Text = "" Then
            Label2.Text = But1
        ElseIf Label3.Text = "" Then
            Label3.Text = But1
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim But2 As Integer = 2
        If Label1.Text = "" Then
            Label1.Text = But2
        ElseIf Label2.Text = "" Then
            Label2.Text = But2
        ElseIf Label3.Text = "" Then
            Label3.Text = But2
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim But3 As Integer = 3
        If Label1.Text = "" Then
            Label1.Text = But3
        ElseIf Label2.Text = "" Then
            Label2.Text = But3
        ElseIf Label3.Text = "" Then
            Label3.Text = But3
        End If
    End Sub
 
Last edited:
Back
Top Bottom