Home > Mobile >  c# - How to set all the application as the owner of Form.showDialog()
c# - How to set all the application as the owner of Form.showDialog()

Time:02-17

I have a Windows Desktop App with an auto-update method implemented. I want to show a custom Form (because I need to change the button texts) asking the user if he or she wants to download the new version or not when a new update is detected and I want to "block" all input actions in the Desktop App until the user has made his selection.

After reading Form.ShowDialog() documentation and several topics here saying "ShowDialog() is not making my windows modal" and several answers replying "You need to properly set the owner" I still don't understand how to set this owner. Of course if I make two forms and the first one shows the second, I can "block" the first one doing:

secondForm.ShowDialog(firstForm);

But I don't know how to make that the firstForm blocks all the application to prevent the user using a deprecated version of it.

I tried several approaches like getting the current id process (or trying to get it) and convert it to IWin32Window. But nothing seemed to work.

If you need it, I add here the code I'm using:

 FormAsk  formAsk = new FormAsk (param1, param2);
 formAsk.StartPosition = FormStartPosition.CenterParent;
 formAsk.TopLevel = true;
 formAsk.TopMost = true;
 formAsk.DialogResult = formAsk .ShowDialog();
 if(formAsk.DialogResult.Equals(DialogResult.OK))
 {
      // do stuff
 }
 else
 {
      // do other stuff
 }

I've also seen lots of solution implementing:

myForm.ShowDialog(this);

But VS start's crying because the types are not compatible.

CodePudding user response:

Do your Stuff in your first Form and whatever button you press to create your second Form just go like this. (Pseudo Code)

button1_click()
{
   Form2 = new Form() 
   Form2.Owner = this;
}

and now from your Form2 you can talk to your Owner with this.Owner.Visible = false for example.

Thats how you make the Owner if thats what you asked for.

CodePudding user response:

You need to pass an instance of the IWin32Window interface to the ShowDialog method.

IntPtr myWindowHandle = IntPtr(parent.Handle);
IWin32Window w = Control.FromHandle(myWindowHandle);
  • Related