Windows Form launches another form

I am new to Visual Studio (Express) and C #. I made a window form that accepts some user input, then displays that input in a message box (which automatically comes with an OK button that closes the message box when clicked).

Instead, I would like the user input collected by the first form to appear in a new form, which displays a message (label), shows the input and offers a choice of two buttons: one to accept and one to return and change the input.

I have no idea how to do this, and any advice is appreciated.

+4
source share
4 answers

It looks like you need to confirm a message or something like that. The MessageBox class offers this feature.

DialogResult btn = MessageBox.Show("your message", "your title", MessageBoxButtons.OKCancel, MessageBoxIcons.Question); if(btn == DialogResult.Cancel) // User canceled, return to the string editor else // User confirmed, do you work 

If you prefer, there is also an enumeration for MessageBoxButtons.YesNo with the corresponding DialogResult.Yes and DialogResult.No

See here link for MessageBoxButtons
See here the link for MessageBoxIcons

+3
source

You need to somehow transfer the information from the first form to the second. This can be done by setting the properties of the child form either through individual properties (strings, int, etc.), or using the full data structure (object). The input form collects information, creates a child form, sets properties on it, then displays the form. Many other ways to do this, but start with simple ones and then build up to the complex.

+1
source

If you need to pass some data to the second form, create a property / properties in this form or provide data through constructor parameters. Also set the DialogResult property for the two buttons in the second form. Set DialogResult.OK to the button that will accept the input. Check the value returned by the second form when it displays it as a dialog and follow the appropriate steps:

 using(SecondForm secondForm = new SecondForm() { secondForm.Data = yourData; if (secondForm.ShowDialog() != DialogResult.OK) { // go back and change input return } // accept input } 
+1
source

So your Form2 must have some value provided by what creates it in order for it to exist. There should never be an instance of Form2 without this information. This tells you that it should be in the constructor of the form (as opposed to the property in this form).

This means that in Form1 you will have something like this:

 string someData; //populate based off of user input Form2 childForm = new Form2(someData); //then hide current form and show child form 

In Form2, you probably already have a constructor, you just need to change it to something like:

 public Form2(string someData) //TODO give better parameter name { someLabel.Text = someData; } 

Next, we need to deal with the child form that returns to the parent form. I believe the preferred way to handle this is to use events. The form has a FormClosing event, which you can connect to; this will allow your parent form to run some code when the child form is closed.

string someData; // populated based on user input Form2 childForm = new Form2 (someData);

 childForm.FormClosing += (sendingForm, args) => { this.Show(); bool result = childForm.DidUserAccept; } 

Here, I used the property in the DidUserAccept child form, as the user accepted or rejected the value. We need to determine what in Form2:

 public bool DidUserAccept {get; private set;} 

In the button handlers for accepting / canceling, you can set the result accordingly, and then close the form (closing will cause a closed event and run the corresponding code in Form1 .

+1
source

Source: https://habr.com/ru/post/1415596/


All Articles