-2

How can I lock (and make it look faded) a parent form while the child form is active? I tried to make the child form topmost but that just made it always visible and I can still edit the parent form. I want to be unable to operate on the main form while the child form is running in VS2012, C#. This is the code I used to call the second form...

private void checkButton_Click(object sender, EventArgs e)
{
    Form2 newForm = new Form2(this);
    newForm.Show();
}
B.K.
  • 9,982
  • 10
  • 73
  • 105
  • How is this question related to visual studio? Would it be different in another editor? Why is it not tagged with Windowm, XAML or any other relevant tag? – TomTom Feb 06 '16 at 07:57
  • It was simply for the purpose of clarity, that's the IDE I'm using, – Bafana Gama Feb 06 '16 at 08:24
  • 1
    @TomTom Let's not get crazy here. You could just edit the question, seeing how he's a new user. – B.K. Feb 06 '16 at 19:54

4 Answers4

1

One very simple way is to use ShowDialog() instead of Show() on the child form. This will prevent any interaction with the main form. This is also a neat way of passing back results. There are many other ways, of course.

Example:

private void checkButton_Click(object sender, EventArgs e)
{
    Form2 newForm = new Form2(this);
    newForm.ShowDialog();
}

See MSDN for further details: https://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.110).aspx

B.K.
  • 9,982
  • 10
  • 73
  • 105
  • That works just the way I want it to B.K. I would also like to know if the data I work with can be saved/passed back to the original form, or even to a textfile/database? – Bafana Gama Feb 06 '16 at 08:20
  • @Sensei Yes, but all those are different things and can be approached in different ways. If, for example, you wan to return results other than `DialogResult` enumeration returned by the ShowDialog() method, you could create a public method in your pop-up form, say `ShowDialogWithResults()` which uses `ShowDialog()` within it and then returns the results to the main form. You can see possible implementation of that here: http://stackoverflow.com/questions/3764736/is-it-possible-to-overload-the-showdialog-method-for-forms-and-return-a-differen – B.K. Feb 06 '16 at 19:51
0

Just add Hide() for Current Running Form,

 private void checkButton_Click(object sender, EventArgs e)
            {
                Form2 newForm = new Form2(this);
                this.Hide();//hide old form
                newForm.Show();
            }
  • I suppose this would work since, the only problem that may have arose was that each time I terminated the child form, a new instance of the parent form was created so I ended up with duplicates of the same form. – Bafana Gama Feb 06 '16 at 08:23
0

You can use Form.ShowDialog to create dialog which will open on top of parent form and will not allow to edit parent until you close child

private void checkButton_Click(object sender, EventArgs e)
        {
            Form2 newForm = new Form2(this);
            newForm.ShowDialog(this);
        }
Viru
  • 2,228
  • 2
  • 17
  • 28
0

You might wanna run the form2 in separate thread and set topmost = true, the form1 will function unblocked, but the form2 will run whatever you want unblocked too. Is this what you want?

namespace TestModal
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thrd = new Thread(newwindow);
            thrd.IsBackground = true;
            thrd.Start();
        }

        private void newwindow()
        {
            Form2 frm2 = new Form2();
            frm2.TopMost = true;
            frm2.ShowDialog();
        }
    }
}
WindyHen
  • 318
  • 2
  • 6