Eli Waxman
Eli Waxman

Reputation: 2897

How to create a new form and make all other forms not clickable until that form is disposed?

I once read a tutorial about how to create a new form and make it above all other windows so you can click only it - like in internet explorer for example when you click on browse for a file you can't click the main window until you finish using the browse window.

Also what is the best way to get values from a form back, for exmpale on my second form I have a listbox and when a user clicks on one of the values the first form (mainform) should get the event - is this possible?

Upvotes: 1

Views: 4078

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1502216

It sounds like you want:

using (MyCustomForm form = new MyCustomForm(...))
{
    if (form.ShowDialog() == DialogResult.OK) {
        // Now use the values in form
        // (e.g. through properties of the form)
    }
}

Upvotes: 7

Mike Marshall
Mike Marshall

Reputation: 7850

try this:

Create your form and In your calling code do the following:

MyForm form = new MyForm();
form.ShowDialog();

To get the values back, simply create properties on your form that map to the values of your controls (make sure you don't dispose your form before you access the properties!):

public class MyForm
{
//...

  public string FirstName
  {
     get
     {
         return firstNameTextBox.Text;
     }
  }
}

Then call the properties from your calling code after the dialog is done:

MyForm form = new MyForm();
if(form.ShowDialog() == DialogResult.OK)
{
    string myFirstName = form.FirstName;
    // etc
}

Upvotes: 0

HABJAN
HABJAN

Reputation: 9338

What you are looking for is to show the form as a modal dialog box. Form.ShowDialog() Here you can read more about this topic.

You can access parent form (back form) in couple ways:

  • Make modal form constructor to accept parent from as parameter
  • Put parent form reference into some global variable that can be accessed from modal form
  • ...

To get an event from child form you can do something like this:

form.myListBox.SelectedIndexChanged += new System.EventHandler(this.myListBox_SelectedIndexChanged);
form.ShowDialog();

You need to make myListBox control public in order to access it from parent (caller) form.

Upvotes: 2

Chris Wenham
Chris Wenham

Reputation: 24017

Just use a modal form, which is done by calling .ShowDialog() after you've made an instance of it.

To get the values back, just store them in properties of the form, and then read those properties from the parent window/code before it goes out of scope. You'd handle the SelectionChanged event in the code-behind of your new form and set a property with the value.

Upvotes: 3

Related Questions