开发者

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?


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)
    }
}


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.


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.


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜