-1

I checked and found this article that is referred to MANY times in this type of question, and it is NOT the answer I want...

I have a form, Form_Main frmMainPage, it creates Form_Status frmStatus. When it does so, I disable the controls on frmMainPage so the user can't mess with things while things are processing. When things are done processing they can close the frmStatus window to go back to frmMainPage to continue working. I cannot re-enable the controls from frmStatus. I tried to use the frmMainPage_Enter, but it goes crazy when it first loads, so that isn't really an option.

I have the following in Form_Main.cs

public void EnableForm() {
    this.gridData.Enabled = true;
    this.txtLocation.Enabled = true;
    this.txtSupplier.Enabled = true;
    this.txtItem.Enabled = true;

    this.FillTable("", "", "");
}

When I use this (per article above):

private void btnClose_Click(object sender, EventArgs e) {
    Form_Main f2 = new Form_Main();
    f2.Show();
    f2.EnableForm();

    this.Close();
}

It creates a second Form_Main, which is not what I want. I want to know how to change the controls in the existing form.

Edit: No, it is not this that was also suggested. Most of the answers deal with changing controls on Form 2 from Form 1 when Form 2 is created by Form 1. In my case I need to do the opposite and change controls on Form 1 FROM Form 2, which was created by Form 1. Kind of a circular thing.

Dizzy49
  • 1,360
  • 24
  • 35
  • Where is the code where you show `frmStatus`? You can just show it modally, so that the other form is not accessible until `frmStatus` is closed. Alternatively, you could write an event handler for the `frmStatus.Close` event in `frmMainPage` and re-enable your controls there. – Rufus L May 02 '19 at 21:14
  • What seem to be trying to manually re-create is modal dialogs. Instead of opening the Form_Status with Show, open it with ShowDialog. That will block the main window. Or am I missing something? – Avo Nappo May 02 '19 at 21:17
  • Just open `frmStatus` with `f2.ShowDialog();` and you don't have to disable anything. When `frmstatus` closes , `main` will be again available. – T.S. May 02 '19 at 21:22
  • Possible duplicate of [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – Thomas Weller May 02 '19 at 21:25
  • Nope, not duplicate if you read. In general it is communication between two windows forms. But communicating one was is easy, but the way I'm asking is not as clear cut. – Dizzy49 May 02 '19 at 22:27

2 Answers2

1

I can think of a couple of ways to do this. First (and most common) is to show the second form modally (which means that the first form's code pauses while the second form's code is running):

private void button1_Click(object sender, EventArgs e)
{
    var statusForm = new frmStatus();

    // ShowDialog will prevent frmMainPage from being accessible.
    // This form's code will pause at the next line until the second form is closed
    statusForm.ShowDialog();
}

There are occasions where you want to have both forms accessible at the same time. If this is the case, another method would be to add an event handler for the second form's FormClosed event, where you can re-enable the controls on the first form. This will allow both forms to be accessed at the same time:

private void button1_Click(object sender, EventArgs e)
{
    var statusForm = new frmStatus();

    // Add an event handler for the second form's FormClosed event, and
    // put code in that event handler to re-enable controls on this form
    statusForm.FormClosed += statusForm_FormClosed;

    // Disable our controls on this form and show the second form
    DisableForm();
    statusForm.Show();
}

private void statusForm_FormClosed(object sender, FormClosedEventArgs e)
{
    // When the second form closes, re-enable controls on this form
    EnableForm();
}

private void DisableForm()
{
    this.gridData.Enabled = false;
    this.txtLocation.Enabled = false;
    this.txtSupplier.Enabled = false;
    this.txtItem.Enabled = false;
}

public void EnableForm() 
{
    this.gridData.Enabled = true;
    this.txtLocation.Enabled = true;
    this.txtSupplier.Enabled = true;
    this.txtItem.Enabled = true;

    this.FillTable("", "", "");
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • I think this is what I wanted. Trying to wrap my head around what goes where. I think what I need to use is the ShowDialog(). Then I just need to figure out how to call the FillTable() function in frmMain when frmStatus closes. I think adding the evenhandler as you mentioned will work. – Dizzy49 May 02 '19 at 21:36
  • Yeah you can just call `EnableForm` from the event handler. All the code above would be in `frmMainPage` – Rufus L May 02 '19 at 21:43
  • I modified the code so it mimics yours, except I'm not sure where you instantiate the `frmStatus`, so I stuck it in a button `Click` event. – Rufus L May 02 '19 at 22:06
  • private void btnPurchase_Click(object sender, EventArgs e) { Form_Status frmStatus = new Form_Status(); frmStatus.ShowDialog(); } – Dizzy49 May 02 '19 at 22:16
1

you dont need to do this disable enable. you just need to show your new from with ShowDialog(); like this:

frmStatus.ShowDialog();

instead of just:

frmStatus.Show();
Yair I
  • 1,133
  • 1
  • 6
  • 9
  • You are correct, and I only Marked Rufus above your's because it answered my actual question, and I can use elements of his answer to resolve the other issue the ShowDialog() would bring. Thank you! – Dizzy49 May 02 '19 at 21:37