Reputation: 195
I would like to make a form that, when opened, locks the user to only using that form until closed.
I see this done in many programs that have a "settings." You open the new form and if you try to click back on the old one, the new one pops back up and makes a beeping sound.
And just wondering, what exactly is this called, just do I don't have to reference it as a "Form that locks the main form"?
Upvotes: 1
Views: 1732
Reputation: 3258
You need to use the ShowDialog method to do this. This will lock the parent form like your are wanting. Here is some sample code that will show you how to do this (all it does is check a textbox
's content in form2, which is shown as a modal dialog to prevent the parent form from being used:
public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
Sample code copied from here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog(v=vs.71).aspx
Upvotes: 7