开发者

C# ShowDialog Parent Form is null

开发者 https://www.devze.com 2023-02-27 16:50 出处:网络
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.

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 fol开发者_StackOverflow社区lowing 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.


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.


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


Since you are passing DisplayDataForm as parent in lw.ShowDialog(this); DisplayDataForm is the parent of LoadWorkstationFile form when calling it second time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号