1

我有一个表单,用户输入我保存在数组中的值,但是当用户想要取消时,我希望最后一次询问用户是否要继续并取消预订。如果用户最后一次拒绝,我希望程序返回 GUI 并专注于包含所有保留行的文本框而不是取消,但我向你展示我编写的代码并询问用户他们是否是当然,如果不是,它仍然会删除预订,然后专注于文本框。我的代码有什么问题?

public void Cancelreservation()
{
    int index = lstReservations.SelectedIndex;
    bool checkedindex = m_seatMngr.CheckIndex(index);
    if (checkedindex)
    {

        if (!m_seatMngr.CancelSeat(index))
        {
            if (lstReservations.SelectedIndex == -1)
            {
                MessageBox.Show("You need to select a row.", "Error!",
                MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button1);
                lstReservations.Focus();
            }
            else
            {
                MessageBox.Show("The seat is not reserved! No need to cancel 
                                  reservation.", "Important Query", 
                                  MessageBoxButtons.OK);
                lstReservations.Focus();
            }
        }
        else
        {
            if (MessageBox.Show("Continue to cancel the reservation?", 
                                "Important Query", MessageBoxButtons.YesNo) 
                                == DialogResult.No)
            {
                lstReservations.Focus();                                
            }
            else
            {
                m_seatMngr.CancelSeat(index);
            }
        }
    }

m_seatMngr

         public bool CancelSeat(int index)
    {

        if (m_vacantList[index] == "Reserved") 
        {
            m_nameList[index] = " - ";
            m_priceList[index] = 0;
            m_vacantList[index] = "Vacant";
            return true;
        }
        else
        {
            return false;              
        }

    }         
4

2 回答 2

3

假设这m_seatMngr.CancelSeat(index)是实际取消席位的方法,您将调用该方法两次。第二个if语句,在你的代码中有六行,是这样的:

if (!m_seatMngr.CancelSeat(index))

...而且(鉴于上述假设)这行似乎很可能会在您显示 MessageBox 之前取消座位。

于 2012-08-01T12:22:12.673 回答
2
if (!m_seatMngr.CancelSeat(index))
{
    // the rest of your code, which displays the messageboxes
}

这总是在您显示任何消息框之前调用 m_seatMngr.CancelSeat。

于 2012-08-01T12:23:25.420 回答