繁体   English   中英

使用 Contains 或 IndexOf 方法检查值的状态或索引时如何从文本框中检索值?

[英]How to retrieve value from a textbox when using Contains or IndexOf methods to check the status or index of values?

我只是尝试检查数组是否包含特定值或它的索引是什么,不是通过在括号中显示相应值而是在文本框中键入它们。 怎么办?

private void button3_Click(object sender, EventArgs e)
{
    int[] values = new int[6];
    values[0] = 2;
    values[1] = 9;
    values[2] = 5;
    values[3] = 15;
    values[4] = 8;
    values[5] = 25
    bool status = values.Contains(?);//I want to retrieve it from txtbox  
    label1.Text = $"{status}";
    int indexi = Array.IndexOf(values,?); //same is true for this method aswell.         
    label2.Text = $"{indexi}";


    foreach (int item in values)
    {               
        listBox1.Items.Add(item);
    }
}

如果您只想从文本框中检索一个 int 值并查看该数字是否存在于 values 数组中:

int position = Array.IndexOf(values, Convert.toInt32(Textbox.Text)); 
if (position > -1) //If it finds the index position it will be greater than -1
{
   bool status = true;
}

这只是从文本框中检索字符串值,将其转换为整数并在值数组中查找它的索引。 如果“位置”变量大于 -1,则表示它在数组中找到了有效位置。

检查这个: 检查字符串数组是否包含一个值,如果是,获取它的位置

你可以这样做

int[] values = new int[6];
        values[0] = 2;
        values[1] = 9;
        values[2] = 5;
        values[3] = 15;
        values[4] = 8;
        values[5] = 25;
        bool status = values.Contains(Convert.ToInt16(txtValue.Text));//I want to retrieve it from txtbox  
        lblindex.Text = status.ToString();
        int indexi =Array.IndexOf(values,Convert.ToInt16(txtValue.Text)); //same is true for this method aswell.         
        lblindex.Text = indexi.ToString();
        foreach (int item in values)
        {
            listBox1.Items.Add(item);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM