I Want to create a form in which the pop-out form should disable the background form just like save as dialog box in notepad. Please ANybody Help me .. I am Creating Application and Need it very urgent .

sorry i have already that answer but it did not match to my query .

I am explaining the whole question:

I will take 2 forms , 1 menustrip ,
sorry i have already seen that code snippet @ddanbe but that is not what i want.

i am Explaining the whole question:

I will take 2 forms, 3 text box and 2 buttons .

In 1st form i have draged 1 button and 2 textbox.
Now when i will enter some value and click on button . then one new 2nd form will appear with one button and 1 textbox . Now i have to enter the value in textbox in the new 2nd form. Now the first should be disable till I click the button on the 2nd form or close the 2nd form. and after the 2nd form is closed or the first form should get the focus. And if clicked on the button at the 2nd form then also the 2nd form should be closed and 1st form should get the focus.

Please help ... I am stucked in between the application..

In my snippet, I made use of ShowDialog() method, this opens a form as a modal dialog. If you want it to be a form use the Show() method. Click Here

Like ddanbe said, ShowDialog() will disable any interaction with the parent form until the child form is closed, which is what you want if I understood you correctly.

Here's a simple set up that will do what you want. You didn't say what you want to do with the data in the textbox on form2, so I copied it to a textbox on form1:

Form1 controls and their relevant properties

            // 
            // textBox1
            Name = "textBox1";
            // 
            // textBox2
            // 
            Name = "textBox2";
            // 
            // button1
            // 
            Name = "button1";
            Text = "button1";
            button1.Click += new System.EventHandler(button1_Click);// to get this just double click the button in the designer.

Code for Form1

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

            private void button1_Click(object sender, EventArgs e)
            {
                //this grays out Form1 while Form2 is showing.
                this.Enabled = false;
                Form2 NewForm = new Form2();
                NewForm.ShowDialog();
                DialogResult Result = NewForm.DialogResult;
                this.Enabled = true;
                DialogResult Result = NewForm.DialogResult;
                if (Result == DialogResult.OK)
                {
                    textBox1.Text = NewForm.textBox1.Text;
                }
            }
        }
    }

Form2 controls and their relevant properties

            // 
            // btnOK
            // 
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Name = "btnOK";
            Text = "Ok";
            // 
            // btnCancel
            // 
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Name = "btnCancel";
            Text = "Cancel";
            // 
            // textBox1
            // 
            Name = "textBox1";
            // 
            // Form2
            // 
            AcceptButton = btnOK;
            CancelButton = btnCancel;
            ControlBox = false;
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            Name = "Form2";
            StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            Text = "";

I included a cancel button. It's usually good practice to include a failsafe

Form2 code

    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
        }

Notice Form2 doesn't actually need any extra coding to work as a modal form

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.