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