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)、メソッドを2回呼び出しています。2番目の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 に答える