2

Sorry, I'm a C# beginner

I am trying to make a button on Form 4 that will make change a property of an object in Form 3.

This case, every time I press button 1 on Form 4, the label on Form 3 will say that "You pressed button 1", Same thing on the button 2.

I added this on Form 4.

  public partial class Form4 : Form
{
    public bool buttonchecked; 

    private void button1_Click_1(object sender, EventArgs e)
    {
        buttonchecked = true;
    }

    private void button2_Click_1(object sender, EventArgs e)
    {
        buttonchecked = false;
    }

And this is what i put on Form 3:

        public void label2_Click(object sender, EventArgs e)
    {
        Form4 form4 = new Form4(); //add

        if (form4.buttonchecked == true)
        {
            label2.Text = "You pressed button 1";
        }
        else
        {
            label2.Text = "You pressed button2";

My label2 text is always set to "You pressed button2" but I didn't

I added a code that closes the current form and Opens the other form, maybe this is causing the problem?

this is from the Form 3

        this.Hide();
        Form4 f4 = new Form4();
        f4.ShowDialog();

and this is from the Form 4

        this.Hide();
        Form3 frm3 = new Form3();
        frm3.ShowDialog();

Is there anything something I'm doing wrong?

  • ok, so you ARE setting a property, but setting the property doesn't change the label, unless you click the label? – MrD at KookerellaLtd Apr 16 '18 at 11:41
  • you are missing a few things. you need Form3 to have some method that Form4 will call, when the button is pressed. You need Form4 to have some sort of reference to a Form3 for, for Form4 to call that method (which you'd would pass through in a constructor)...(there are other ways, but this is the obvious) – MrD at KookerellaLtd Apr 16 '18 at 11:42
  • Have a look at [my answer in this thread](https://stackoverflow.com/a/15605436/106159) and also look at the original question and the other answers. – Matthew Watson Apr 16 '18 at 11:43

1 Answers1

1

There are a few issues with your code:

  1. On Form3, why are you handling the label2_click button? This event is fired when you click on a label. If the Text property of your label is an empty string, you won't even see the label in order to be able to click it.

  2. This code:

    Form4 form4 = new Form4();
    
    if (form4.buttonchecked == true)
    // etc
    

    is not logically correct, because you are creating a Form4 instance and then you're checking the value of it's public field (buttonchecked) without displaying the form. The default value of a boolean variable is false, so the control will always hit the else branch. That's the reason you're always getting the "You pressed button2" message.

One correct way to do this using your code is the following:

On Form3:

var form4 = new Form4();

var result = form4.ShowDialog();

if (result == DialogResult.OK)
{
     label2.Text = "You pressed button 1";
}
else
{
     label2.Text = "You pressed button 2";
}

On Form4:

public partial class Form4 : Form
{
   public bool buttonchecked; 

   private void button1_Click_1(object sender, EventArgs e)
   {
       DialogResult = DialogResult.OK;
   }

   private void button2_Click_1(object sender, EventArgs e)
   {
       DialogResult = DialogResult.Cancel;
   }
}

The ShowDialog() method will display the Form4 and will block the Form3 execution. On Form4 you set a DialogResult based on the button you pressed and you return that result to the calling form (Form3). Based on that result, you can take a decision.

That solution will do the job, but it has one issue: you can't play with both forms in parallel because of the Dialog constraint (when you open the Form4 from Form3, you have to close it in order to reach Form3 again, you can't play with both of them in the same time).

So here's a new (clean) solution that solves this problem:

  • On Form3 in Designer Mode, click on the label2 -> Properties -> Modifiers -> Public. In that way you can access the label2 from other forms.

  • On Form4, place the follwing code:

    public partial class Form4 : Form
    {
       private void button1_Click_1(object sender, EventArgs e)
       {
         var form3 = Application.OpenForms["Form3"];
         form3.label2.Text = "You pressed button 1";
       }
    
       private void button2_Click_1(object sender, EventArgs e)
       {
         var form3 = Application.OpenForms["Form3"];
         form3.label2.Text = "You pressed button 2";
       }
    }
    

Note: on that solution, Form3 needs to be open before Form4, otherwise Application.OpenForms["Form3"] will return null or it will throw an exception.

If you have any further issues, don't hesitate to leave a comment.

Cosmin Ioniță
  • 3,598
  • 4
  • 23
  • 48