Basically the in the code below i am trying to loop through the array and if the current text in the combobox does not match anything in the array it will throw an error. However i cannot get it to fire on the event TextChanged event.
Any help is greatly appreciated
string[] SRtier1 = { "Option1", "Option2", "Option3", "Option4", "Option5" };
private void SRcmb_tier1_TextChanged(object sender, EventArgs e)
{
//Loop SRtier1 Array to ComboBox
for (int i = 0; i < SRtier1.Length; i++)
{
if (SRcmb_tier1.Text != SRtier1[i])
{
MessageBox.Show("Please select one of the options provided.");
}
}
}
如果您在TextChanged中有任何内容,请在事件中检查属性窗口
First, you have wrong loop implementation : in your code you're eager to fire the message SRtier1.Length
times, what you want is, probably, to check the input and fire the message once:
private void SRcmb_tier1_TextChanged(object sender, EventArgs e)
{
Boolean found = false;
for (int i = 0; i < SRtier1.Length; i++)
{
if (SRcmb_tier1.Text == SRtier1[i])
{
found = true;
break;
}
}
if (!found)
MessageBox.Show("Please select one of the options provided.");
}
Better solution is to use Linq :
private void SRcmb_tier1_TextChanged(object sender, EventArgs e) {
if (!SRtier1.Any(item => item == SRcmb_tier1.Text))
MessageBox.Show("Please select one of the options provided.");
}
And finally, check if SRcmb_tier1_TextChanged
is assinged to TextChanged
of SRcmb_tier1
as von v. said in comments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.