1

我试图弄清楚如何检查列表框是否在所选索引处包含特定字符串,但我无法弄清楚。这是一个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);
    }
}
4

3 回答 3

1

编辑:

对于 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
            }
        }               
于 2013-10-14T08:05:32.567 回答
0

你可以用这个。就是这么简单

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
于 2018-09-13T19:23:24.243 回答
0

您还可以使用 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
}
于 2018-09-13T19:32:57.123 回答