How to make Form1 label.text change when checkbox on form2 is checked?

后端 未结 4 1467
醉梦人生
醉梦人生 2020-11-30 14:45

I\'m very new to c# and am trying my first experiments with 2 different forms.

I\'d like to make it so you have a label1 and a button1 on Form1, and a checkbox1 on F

4条回答
  •  青春惊慌失措
    2020-11-30 14:53

    In this scenario you could use the CheckedChanged event:

    public void checkbox2_CheckedChanged(object sender, EventArgs e) {
        if (checkbox2.Checked) 
        {
            Form1.Label1.Text = "Checkbox 2 has been checked";
        } else 
         { 
            Form1.Label1.Text = "";
         }
    }
    

    http://www.c-sharpcorner.com/uploadfile/mahesh/checkbox-in-C-Sharp3/

    Note you will have to change the Access Modifier in Form1 and make Label1 public so Form2 can alter the Text property.

    To do this, goto Form1, select Label1 goto Properties, select Modifiers and change from Private to Public. Form2 will then have access to the Label.

提交回复
热议问题