Modal Windows & Parents

  • Thread starter Thread starter cj_junktrap
  • Start date Start date
C

cj_junktrap

Is it possible to have form.ShowDialog() be modal only to its
parent/owner form, and allow the user to still use any other forms in
the app? I've tried passing the parent form in as the parameter to
form.ShowDialog, but that doesn't seem to make any difference.
 
Hi,

I'm not sure if that's possible using a built-in solution but you could just create a tight coupling between the two forms and when
the parent form is activated, check a variable to see if the child form is visible and if so activate the child form.

.... Out of curiosity I just tested my suggestion and it worked as expected, however the ParentForm appears and behaves a bit strange
while the DialogForm is shown. For one thing the XP hover styles of buttons, for instance, are active. Another thing is that the
border chrome is always in the inactive state but you can move the Form. Might need some elbow grease for that one but I can't
imagine that being a big issue.

The minimize, maximize, close and system buttons are all unclickable, which is desired. Also, you can't activate any controls or
click buttons while the DialogForm is visible, which is the point, of course. Here's the partial code:

class ParentForm : Form
{
private readonly DialogForm dialogForm;

public ParentForm()
{
InitializeComponent();

dialogForm = new DialogForm();
dialogForm.Owner = this;
}

private void btnShowDialog_Click(object sender, EventArgs e)
{
dialogForm.Show();
}

protected override void OnActivated(EventArgs e)
{
if (dialogForm.Visible)
{
SystemSounds.Beep.Play(); // lol
dialogForm.Activate();
}

base.OnActivated(e);
}

protected override void OnClosed(EventArgs e)
{
dialogForm.Dispose();
base.OnClose(e);
}
}
 
Unfortunately I can't have such tight coupling between the forms...
what I'm currently doing is setting the parent form's enabled property
to false, spinning off a new thread, calling form.ShowDialog on that
thread, and then waiting in the original thread using a sleep/DoEvents
loop until the new thread finishes (i.e. the dialog is closed) and
setting the parent's enabled property back to true. It's messy, and I
don't like the DoEvents loop at all, but I don't really see much
alternative.
 
Hi,

Try creating an object to handle the 'coupling' of two forms if you can't modify the forms themselves. Maybe create a class named,
"FormDialogRelationManager", which knows how to display a given form as a modal dialog for another given form. You could then use
the code I supplied to manage the relation in a method named, for instance, "ShowDialog". If that fits into your architecture it
might be easier to manage, test and document.

HTH
 
Back
Top