c# - 在 C# 中列出数组的值

标签 c# .net arrays winforms

我有一个带有两个文本框和两个按钮的 Windows 窗体。文本框被命名为 txtVisited 和 txtAnswer。按钮是 Visited?Exit。我想在第一个文本框中输入一个城市并让它检查数组是否匹配。如果匹配,我想显示已访问过的文本及其在数组中的位置。如果没有找到匹配项,我希望它显示文本“未访问过”。到目前为止,我已经复制了所有代码。任何帮助将非常感激。我对 c# 还是很陌生,所以我可能理解也可能不理解你的回答。所以请耐心等待。提前抱歉。

    namespace Array
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnVisited_Click(object sender, EventArgs e)
        {
            string[] CityName = {"Columbus", "Bloomington", "Indianapolis",
                "Fort Wayne", "Greensburg", "Gary", "Chicago", "Atlanta", "Las Vegas"};
            bool visitedbool;
            int Subscript;
            string QueryCity;
            QueryCity = txtState.Text.ToUpper();
            int Subscript =0;
            visitedbool = false;

            while (visitedbool = true)

            if (CityName(intsubscript).ToUpper= QueryCity)
            {
                visitedbool = true
            }

            else
            { 
                Subscript =  Subscript +1
            }
        }
    }
}

最佳答案

var idx = Array.IndexOf(CityName, QueryCity);
if (idx  != -1)
{
    // display QueryCity and idx
}
else
{
    // display "not visited"
}

关于c# - 在 C# 中列出数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433280/

相关文章:

android - 将字符串动态插入到Android中的字符串数组中

c# - 如何读取屏幕像素的颜色

c# - MVC asp.net 序列化如何在 Controller 操作上为 Json 对象工作?

c# - 误报 : precondition is redundant

c# - c++ 和 c# sin 函数大值的不同结果

.net - 如何在 .NET/C#(或 p/invoke)中检测 Windows 关机与重启

java - 拆分没有分隔符的字符串

ruby - 将数组拆分为一些子数组

C# 在 INSERT 查询中调用 ST_GeomfromText 函数

c# - 如何在代码中动态地将新的视觉状态添加到自定义控件模板?