Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ShowDialog Parent Form is null

Tags:

c#

forms

I have two forms.

The LoadWorkstationFile prompts the users for the id that they want to load.

The DisplayDataForm displays the id's data that they selected on the previous screen.

In the DisplayDataForm they can click on an option to load new data, which calls the LoadDataForm as a ShowDiaglog:

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
  var answer = MessageBox.Show("Do you wish to save the current work file before continuing?", "Confirmation",
                               MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  if (answer == DialogResult.Cancel)
    return;
  if (answer == DialogResult.Yes)
    SaveWorkFile();
  var prevworkstationid = Configuration.WorkstationId;
  var lw = new LoadWorkstationFile();
  lw.ShowDialog(this);
  if (Configuration.WorkstationId != prevworkstationid)
  {
    LoadData();
  }

}

As you can see, I am prompting them again with the same screen as before.

In the LoadWorkstationFile it has the following code:

  if (this.Parent == null)
  {
    var sc = new ScanCheck();
    sc.Show();
    this.Hide();
  }

Initial load everything is fine. When I want to load data again it it has loaded, I end up with 2 of the LoadWorkstationFile screens cause Parent always equals null.

Do I have the wrong idea? Should the parent be the DisplayDataForm when it is called with .ShowDialog ?

Thanks as usual.

like image 267
ErocM Avatar asked Apr 20 '11 13:04

ErocM


2 Answers

You should check the Owner instead of the Parent

What you're setting in the constructor of ShowDialog is the Owner property. Which tells the form which other form "owns" it. The parent is (as indicated by Mario) described the ownership relationship for a Control.

so you should change your code to:

if (this.Owner == null)
  {
    var sc = new ScanCheck();
    sc.Show();
    this.Hide();
  }

and it should work.

like image 155
NightDweller Avatar answered Sep 22 '22 14:09

NightDweller


Parent is a property inherited from control which is used to describe embedding relationships ( a label has as parent the form).

I do not think it is set when using ShowDialog().

I assume that Owner is the correct property to check.

hth

Mario

like image 31
Mario The Spoon Avatar answered Sep 23 '22 14:09

Mario The Spoon