本文介绍了如何从数组中选择随机字符串并将其显示在文本框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要尝试的是从我随机设置的数组中选择一个字符串,然后将其显示在名为txtResults的文本框中。我不确定如何实现此目标,因为我对C#和创建GUI还是很陌生。

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??
}


推荐答案

获取字符串:

String result = mexicanRestaurants[rand.Next(mexicanRestaurants.Length)];

然后将其设置为文本框:

Then to set it to the textbox:

txtResults.text = result;

这篇关于如何从数组中选择随机字符串并将其显示在文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:46