Reputation: 37
What I am trying to do is to pick a string from my array that I have set up at random and then display it int a text box named txtResults. I am not sure how to achieve this as I am fairly new to C# as well as creating GUIs. Any advice or help is greatly appreciated.
private void btnMexican_Click(object sender, EventArgs e)
{
string[] mexicanRestaurants =
{
"Jose Locos/n853 N Glenstone Ave, Springfield, MO 65802/n(417) 831-1300",
"Tortilleria Perches\nElfindale Center, 1601 W Sunshine St, Springfield, MO 65807\n(417) 864-8195",
"Purple Burrito\n5360 S Campbell Ave Springfield, MO 65810\n(417) 883-5305",
"Amigos Mexican Restaurant\n2118 S Campbell Ave, Springfield, MO 65807\n(417) 887-1401",
"Cantina Laredo\nAddress: 4109 S National Ave, Springfield, MO 65807\n(417) 881-7200"
};
Random rand = new Random();
// Now what??
}
Upvotes: 2
Views: 3787
Reputation: 376
Here are the docs on Random. Like jdphenix said in comments, rand.Next(mexicanRestaurants.Length)
should give you an appropriate index.
Upvotes: -1
Reputation: 1976
To get the string:
String result = mexicanRestaurants[rand.Next(mexicanRestaurants.Length)];
Then to set it to the textbox:
txtResults.text = result;
Upvotes: 2