Reset 2 Textbox Out Of 4 At Random

timosilver

New member
Joined
Feb 28, 2012
Messages
2
Programming Experience
1-3
Hello

How do i reset 2 textbox text of out four textbox at random avoiding a textbox whose text = 'A'


for clarification.

i have

Textbox1.text = "B"
Textbox2.text = "C"
Textbox3.text = "A"
Textbox4.text = "D"

how do i set an empty value "" to any two textbox at random avoiding the textbox whose text is "A"

Thanks in advance.
 
There are various ways this could be done but the simplest is to use LINQ. Put all the TextBoxes in an array, filter that to exclude those containing "A", randomise those results and then take the first two:
Dim textBoxes = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4}
Dim rng As New Random

textBoxes = (From tb In textBoxes
             Where tb.Text <> "A"
             Order By rng.NextDouble()).Take(2).ToArray()

For Each tb In textBoxes
    tb.ResetText()
Next
 
thanks for you reply.
Am trying to apply this to a button text as well a custom button control.

VB.NET:
Dim Answerbtns = New Answerbtn.Answerbtn() {Main1.Answerbtn1, Main1.Answerbtn2, Main1.Answerbtn3, Main1.Answerbtn4}        Dim rng As New Random


        Answerbtns = (From button In Answerbtns Where (Answerbtns.text <> Main1.TextBox1.Text) Order By rng.NextDouble()).Take(2).ToArray()


        For Each button In Answerbtns
            button.ResetText()
        Next

But i have 1 error at 'Answerbtns.text <> Main.textbox1.text' Errors says .text is not a member of system.array
 
I think that this:
VB.NET:
Answerbtns = (From button In Answerbtns Where ([B][U]Answerbtns[/U][/B].text <> Main1.TextBox1.Text) Order By rng.NextDouble()).Take(2).ToArray()
should be this:
VB.NET:
Answerbtns = (From button In Answerbtns Where ([B][U]button[/U][/B].text <> Main1.TextBox1.Text) Order By rng.NextDouble()).Take(2).ToArray()
 
Back
Top