For循环,如果语句没有将值发送到文本框。

时间:2022-11-29 20:28:25

I am comparing specific position [0] in array 1 with position[i] in array 2 and when they are equal to each other I want 3rd array with position[i] to be placed in textbox field.

我比较的是数组1中的特定位置[0]和数组2中的位置[I],当它们相等时,我想要在textbox字段中放置位置[I]的第3个数组。

Problem is that value wont be placed in the textbox. I've checked if the values are equal and they are.

问题是,这个值不会放在文本框中。我检查过这些值是否相等。

MessageBox.Show(" id [0] is : " + id[0] + "orderID [0] is : " + orderID[0] + " name 0 is : " + name[0]);

Message box shows:

消息框显示:

For循环,如果语句没有将值发送到文本框。

Here is the for loop.

这是for循环。

for (int i = 0; i < id.Length; i++)
{
    if (orderID[0] == id[i])
    {
        text1.Text = name[i];
    }

}

EDIT : Declaration :

编辑:声明:

string[] orderID = new string[aa.Length];
        string[] id = new string[bb.Length];
        string[] name = new string[bb.Length];

1 个解决方案

#1


0  

It seems like you have new line characters '\n' in id[0] and orderID[0] because I don't see them in your code, but the MessageBox shows them.

在id[0]和orderID[0]中,似乎有新的行字符“\n”,因为我在您的代码中没有看到它们,但是MessageBox显示了它们。

When you fill the arrays, trim the strings or convert them like this :

当你填充数组时,修剪字符串或将它们转换成这样:

orderID = orderID.Select(s => (s ?? "").Trim()).ToArray();
id = id.Select(s => (s ?? "").Trim()).ToArray();

Then you can simplify your code to

然后你可以简化你的代码。

int i = id.IndexOf( orderID[0] );      // this returns -1 if not found
if ( i >= 0 ) text1.Text = name[i];

#1


0  

It seems like you have new line characters '\n' in id[0] and orderID[0] because I don't see them in your code, but the MessageBox shows them.

在id[0]和orderID[0]中,似乎有新的行字符“\n”,因为我在您的代码中没有看到它们,但是MessageBox显示了它们。

When you fill the arrays, trim the strings or convert them like this :

当你填充数组时,修剪字符串或将它们转换成这样:

orderID = orderID.Select(s => (s ?? "").Trim()).ToArray();
id = id.Select(s => (s ?? "").Trim()).ToArray();

Then you can simplify your code to

然后你可以简化你的代码。

int i = id.IndexOf( orderID[0] );      // this returns -1 if not found
if ( i >= 0 ) text1.Text = name[i];