简体   繁体   English

每次加载表单时保持禁用按钮

[英]Keep a button disabled every time the form loads

I have two forms ( Form1 and Form2 ). 我有两种形式( Form1Form2 )。

Form1 has some other controls which are saved to the database after all the fields are filled. Form1还有一些其他控件,这些控件在填写完所有字段后保存到数据库中。 It also has a DELETE button where you can delete the searched information from the database and it has the "Available Rooms" button, which when clicked will load Form2 . 它还具有一个DELETE按钮,您可以在其中从数据库中删除搜索到的信息,并且具有“可用房间”按钮,单击该按钮将加载Form2

Form2 has a number of buttons (unoccupied rooms) which I can select from. Form2有许多按钮(无人居住的房间)可供选择。 Now, my problem is how to keep that selected button (eg.: 101) on Form2 disabled when I click on "Available rooms" for a second time, or any other day and also keep any other selected button (eg.: 102) disabled when Form2 loads the next time, etc. 现在,我的问题是,当我第二次或在任何其他日子单击“可用房间”时,如何保持Form2上的该选定按钮(例如:101)处于禁用状态,并且还保留任何其他选定按钮(例如:102)下次加载Form2时禁用,等等。

To make it clear, the only time I want the disabled button on Form2 be active, is when I click on the DELETE button on Form1. 为了明确起见,我唯一希望激活Form2上的禁用按钮的时间是当我单击Form1上的DELETE按钮时。 But, this I cad do it. 但是,这我cad做。

When you are starting the new Form2, save the form variable and use it to access its values. 当启动新的Form2时,保存form变量并使用它访问其值。 You should be able to access the button.Enabled property of your button on Form2. 您应该能够访问Form2上按钮的button.Enabled属性。

This is what your code on Form1 这是您在Form1上的代码

Form2 secondForm;

private void button_Clicked (object sender, EventArgs e)
{
    if (secondForm != null)
    {
        secondForm.yourButton.Enabled = true;
    }
    // Any other code you want to handle
}
private void Form2_Closing (object sender, EventArgs e)
{
    secondForm = null;
}

And this is what you should do everytime you open/show Form2: 这是每次打开/显示Form2时应该做的事情:

secondForm = new Form2();
secondForm.Show();
secondForm.Closing += new System.EventHandler(Form2_Closing);

EDIT: Thanks Hans Passant! 编辑:谢谢汉斯·帕森特! Forgot that. 算了 You should also set the value of secondForm to null if the form is closed. 如果关闭窗体,还应该将secondForm的值设置为null。 Edited the code above. 编辑了上面的代码。

Use Form load event - https://msdn.microsoft.com/en-us/library/system.windows.forms.form.load(v=vs.110).aspx 使用表单加载事件-https://msdn.microsoft.com/zh-cn/library/system.windows.forms.form.load(v=vs.110).aspx

When you load Form 2, read the database. 当您加载窗体2时,读取数据库。 You already know the condition when the button for the room needs to be disabled. 您已经知道需要禁用会议室按钮的情况。 For example, for simplicity let's assume that you can read a column of the database into ac# boolean(say unAvailable) and based on that you can make a decision whether the room is available. 例如,为简单起见,我们假设您可以将数据库的一列读入ac#boolean(例如unAvailable),并据此来确定房间是否可用。 So on your form 2 load (handle Form load event) there needs to be a condition like - 因此,在您的Form 2加载(处理Form加载事件)上,需要满足以下条件-

Form_Load(System.Object sender, 
System.EventArgs e)
{
    roomButton.Enabled = unAvailable;
}

So based on the value of field unAvailabled the roomButton is enabled or disabled. 因此,根据字段unAvailabled的值,启用或禁用roomButton。

I must say thank you for all your comments and answers above. 我必须对您上面的所有评论和回答表示感谢。 Going through them, my mind was opened to think differently particularly using the Form_Load event on Form2 and when did, it worked like a charm!!!!. 通过它们,我的思想变得开放,特别是使用Form2上的Form_Load事件来思考不同的事物,当这样做时,它就像一个魅力!!!

All I did on the Form2_Load event was to read the Apartment_No column(as per one contributor suggested) in the database, and use each of the values to disable the corresponding button using the switch... case. 我对Form2_Load事件所做的只是读取数据库中的Apartment_No列(根据一位贡献者的建议),并使用switch ...大小写使用每个值禁用相应的按钮。

Here is the code below: 这是下面的代码:

     private void Form2_Load(object sender, EventArgs e)
    {
        string constr = @"Data Source=DESKTOP-J6KRI77\SQLEXPRESS; Initial Catalog = SELLnBUY; Integrated Security = true";
        string cmdstr = "SELECT Apartment_No FROM Apartment";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand com = new SqlCommand(cmdstr, con);

        con.Open();
        SqlDataReader r = com.ExecuteReader();


        while (r.Read())
        {
          int room = int.Parse(r["Apartment_No"].ToString());

            switch(room)
            {
                case 101:               
                        button1.Enabled = false;
                        break;                       
                case 102:                       
                        button2.Enabled = false;
                        break;
                    case 103:
                        button3.Enabled = false;
                        break;
                case 104:
                        button4.Enabled = false;
                        break;

            }
        }
        con.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM