Reputation: 1303
So I have 9 buttons on my form , and when I press on a button I want to select one of the 9 buttons and change his text to - "random" . How can I do that in visual basic 2008/2010 ?
I was thinking to something like
For Each buttons In Panel1.Controls
If TypeName(buttons) = "Button" Then
//select a random button and change his text to "random"
End If
Next buttons
Upvotes: 1
Views: 2334
Reputation: 3433
var buttons = from controls in this.Controls.OfType<Button>() select controls;
buttons.ElementAt(new Random().Next(buttons.Count())).Text = "random";
I have not used VB, so just did this in C#. May be same/very similar in VB.
Edit: To answer your comment try something like the following:
var buttons = (from controls in this.Controls.OfType<Button>() where !controls.Text.Equals("random") select controls);
if (buttons.Count() > 0)
{
buttons.ElementAt(new Random().Next(buttons.Count())).Text = "random";
}
Upvotes: 1