验证是否在面板中单击了按钮

时间:2016-07-27 23:20:53

标签: c# validation button

所以我试图验证是否在面板中单击了一个按钮。如果其上没有其他人的名字,那么就在该按钮上预留一个座位。我一直收到错误' panel'不包含etc..etc。

的定义
    public void DisplayReservations(String[] seating)
    {
        pnlBox.Controls.Clear();

        int startTop = 10;
        int startLeft = 10;

        for (int seat = 0; seat <= seating.GetUpperBound(0); seat++)
        {
            Button newButton = new Button();
            this.Controls.Add(newButton);
            newButton.Text = ("Seat: " + (seat + 1) + " " + seating[seat]);
            newButton.Location = new System.Drawing.Point(startTop, startLeft + (seat * 30));
            newButton.Click += AButton_Click;
            pnlBox.Controls.Add(newButton);
        }
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }

    private void radDenver_CheckedChanged(object sender, EventArgs e)
    {


        piclogo.Load("Cessna.jpg");
        lblFlightInfoOut.Text = "";
        lblStatusOut.Text = "";
        //show the flight info

        lblFlightInfoOut.Text = flight1.FlightInfo();


        string[] seatChart1 = flight1.GetReservationList();

        if (flight1.IsFull() == false)
        {

            DisplayReservations(seatChart1);
        }
        else
        {
            DisplayReservations(seatChart1);
            lblStatusOut.Text = "Flight " + flight2.Plane + " to " + flight1.Destination + " Is Full";
        }
    }

    private void radKC_CheckedChanged(object sender, EventArgs e)
    {
        piclogo.Load("Piper.jpg");
        lblStatusOut.Text = "";
        lblFlightInfoOut.Text = "";

        //show the flight info

        lblFlightInfoOut.Text = flight2.FlightInfo();

        string[] seatChart2 = flight2.GetReservationList();

        if (flight2.IsFull() == false)
        {

            DisplayReservations(seatChart2);
        }
        else
        {
            DisplayReservations(seatChart2);
            lblStatusOut.Text = "Flight " + flight2.Plane + " to " + flight2.Destination + " Is Full";
        }


    }

    private void btnNameSubmit_Click(object sender, EventArgs e)
    {
        string name = "";
        int seatNum;
        if (CheckText(txtName.Text) == true)
        {
            name = txtName.Text;


            if (radDenver.Checked == true)
            {
                if (ValidSelect(pnlBox.SelectedIndex) == true)
                {
                    seatNum = pnlBox.SelectedIndex;
                    if (flight1.MakeReservation(name, seatNum) == true)
                    {
                        lblStatusOut.Text = "Seat " + (seatNum + 1) + " is now Reserved for " + name;
                        DisplayReservations(flight1.GetReservationList());
                    }
                    else
                    {
                        lblStatusOut.Text = "Sorry " + name + " Seat " + (seatNum + 1) + " is Taken";
                    }
                }

            }
            else if (radKC.Checked == true)
            {
                if (ValidSelect(pnlBox.SelectedIndex) == true)
                {
                    seatNum = pnlBox.SelectedIndex;
                    if (flight2.MakeReservation(name, seatNum) == true)
                    {
                        lblStatusOut.Text = "Seat " + (seatNum + 1) + " is now Reserved for " + name;
                        DisplayReservations(flight2.GetReservationList());
                    }
                    else
                    {
                        lblStatusOut.Text = "Sorry " + name + " Seat " + (seatNum + 1) + " is Taken";
                    }
                }
            }
            else
            {
                lblStatusOut.Text = "Please Select a destination at Step 1";
            }
        }
    }


    private bool ValidSelect(int SelectedIndex)
    {
        if (pnlBox.AButton_Click == false)
        {
            lblStatusOut.Text = "Please Choose a seat from the List";
            return false;
        }
        return true;
    }

1 个答案:

答案 0 :(得分:0)

在名为DisplayReservations的函数的代码顶部,我注意到单击面板内的按钮时,函数AButton_Click被设置为触发事件:

newButton.Click += AButton_Click;

您在函数ValidSelect中收到错误的原因是由于以下行:

if (pnlBox.AButton_Click == false)

当我们使用'dot'时,我们想要访问对象内部的东西。正如我们在代码顶部看到的那样,AButton_Click只是代码中的另一个功能,而不是面板的一部分(pnlBox)。应该像这样触发函数AButton_Click

if (AButton_Click(null, EventArgs.Empty) == false)

通常情况下,按钮的事件功能如下所示:

private void AButton_Click(object sender, EventArgs e)

然后,在if语句中调用函数时,您必须输入sender和EventArgs。

BUT!即便如此,我相信你误解了一些东西。您是否尝试检测当前是否按下了具有AButton_Click功能的按钮作为其点击事件?

如果是这样,这不是方法。你无法以这种方式检测到它。在btnNameSubmit中按下按钮btnNameSubmit_Click时,您正试图检测面板上的按钮是否被按下。如果你问我,这没什么意义:D。此外,AButton_Click的返回类型可能是void。因此,如果调用它,您甚至无法从函数中获取false。因此在if语句中使用它是无效的。

如果你告诉我一些我不确定的事情,我可以提供更好的解决方案:

  1. 按下时按钮的作用是什么? AButton_Click背后的代码是什么?

  2. 在您的函数ValidSelect中调用btnNameSubmit_Click的目的是什么?

  3. 如果你在ValidSelect检查是否按下了pnlBox按钮。你怎么能按下按钮btnNameSubmit

  4. 我希望到目前为止我的解释是可以理解的。当我知道更多时,我会编辑我的答案并添加更多信息。