检查列表框中的所选项是否包含特定字符
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检查列表框中的所选项是否包含特定字符相关的知识,希望对你有一定的参考价值。
我试图弄清楚如何检查列表框是否包含选定索引处的特定字符串,但我无法弄明白。这是一个WAF项目。
private void UpdateGUI(string name, double price)
{
//Check if the seat is already reserved??
//Check if the seat is already canceled??
int index = lstSeats.SelectedIndex;
if (index < 0)
{
MessageBox.Show("Please select an item in the list!");
return;
}
string strOut = string.Empty;
string strReserved = "Vacant";
if (rbtnReserve.Checked)
{
strReserved = "Reserved";
}
else
{
name = string.Empty;
price = 0.0;
}
strOut = string.Format("{0,3} {1,14} {2,11} {3,13:f2}", index + 1, strReserved, name, price);
lstSeats.Items.RemoveAt(index);
lstSeats.Items.Insert(index, strOut);
lblNumOfReservedSeats.Text = numOfReservedSeats.ToString();
lblNumberOfVacantSeats.Text = (totalNumOfSeats - numOfReservedSeats).ToString();
lblTotalNumOfSeats.Text = totalNumOfSeats.ToString();
}
基本上,我不想预订已经预订的座位(在电影院里),或者如果座位已被取消,我不想再取消它。有什么建议?
或者检查下面的代码会更好吗?:
private void btnOK_Click(object sender, EventArgs e)
{
string name = string.Empty;
double price;
bool inputOK = ReadAndValidateInput(out name, out price);
if (inputOK)
{
if (rbtnReserve.Checked)
{
numOfReservedSeats++;
}
else
{
numOfReservedSeats--;
}
UpdateGUI(name, price);
}
}
答案
编辑:
对于WFA并检查项目是否包含给定字符串使用此:
if (listBox1.SelectedIndex != -1)
{
int itemAtPostion = listBox1.SelectedIndex;
string reserved = "Reserved";
if (listBox1.Items[itemAtPostion].ToString().Contains(reserved))
{
MessageBox.Show("We are sorry, but this seat is reserved!");
//your code
}
}
另一答案
你可以用这个。这很简单
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each i In ListBox1.Items
If i.ToString.Contains("your specific characters") Then
//what happen if it true
Else
//what happen if it false
End If
Next
//what happen when its finish work
End Sub
另一答案
您也可以使用C#:
private void Button1_Click(object sender, EventArgs e)
{
foreach (var i in ListBox1.Items)
{
if (i.ToString().Contains("your specific characters"))
{
//what happen if it true
}
else
{
//what happen if it false
}
}
//what happen when its finish work
}
以上是关于检查列表框中的所选项是否包含特定字符的主要内容,如果未能解决你的问题,请参考以下文章