Return result between Windows Forms in C #

I have two forms of Windows (MyApp, Generator) and I need to call Generator from MyApp

Form gen = new Generator(); gen.Show(); string result = gen.IDontKnowWhatToDoHere(); 

My Generator.cs Form has three TextBox and an Ok button, so when a user types in three text blocks, click OK. I want to get the text entered in these three text fields.

Do you have any idea how I can achieve this.

Thanks.

+7
source share
6 answers
 class Generator : Form { public string Text1 { get; private set; } void ok_Click (object sender, EventArgs) { this.Text1 = txt1.Text; ... this.Close(); } } Form gen = new Generator(); gen.ShowDialog(); string text1 = gen.Text1; ... 

 class TextInfo { public string Text1 { get; set; } ... } class Generator : Form { public TextInfo Textinfo { get; private set; } public Generator(TextInfo info) { this.TextInfo = info; } void ok_Click (object sender, EventArgs) { this.TextInfo.Text1 = txt1.Text; ... this.Close(); } } TextInfo info = new TextInfo(); Form gen = new Generator(info); gen.ShowDialog(); string text1 = info.Text1; 
+6
source

I usually use this template:

 TResult result = Generator.AskResult(); class Generator : Form { // set this result appropriately private TResult Result { get; set; } public static TResult AskResult() { var form = new Generator(); form.ShowDialog(); return form.Result; // or fetch a control value directly } } 

This makes it work in a single expression, similar to MessageBox, and does not require public access to any controls. You can also include any additional data as parameters for the static method and pass them to the form constructor or set properties accordingly.

Other benefits include the ability, if necessary, to reuse instances of the form.

+8
source

If you need data from a modal form, you should use events or another notification template.

 class OkPressedEventArgs : EventArgs { public OkPressedEventArgs(string text1, string text2, string text3) { Text1 = text1; Text2 = text2; Text3 = text3; } public string Text1 {get;private set;} public string Text2 {get;private set;} public string Text3 {get;private set;} } class SourceForm : Form { public event EventHandler<OkPressedEventArgs> OkPressed; private void OnOkPressed() { if(OkPressed != null) { OkPressed(this, new OkPressedEventArgs(textBox1.Text, textBox2.Text, textBox2.Text); } } private void okButton_Click(object source, EventArgs e) { OnOkPressed(); } } class TargetClass { void ShowFormMethod() { var form = new SourceForm(); form.OkPressed += OkPressedHandler; form.Show(); } private void OkPressedHandler(object source, OkPressedEventArgs e) { // process form data here! } } 
+2
source
 Form gen = new Generator(); gen.ShowDialog(); string result = gen.IDontKnowWhatToDoHere(); 

But I think that creating a delegate was more efficient.

+1
source

An IF generator is a form that inherits from a form, changes the string to:

 Generator gen = new Generator(); gen.Show(); string result = gen.SomePublicVariableOrProperty; 
0
source

I think you can try by passing an instance of the current form as a parameter to the constructor of the Generator form.

Once you populate the Textbox when you click an event, Assign it in the Property in Main Form using the instance that was passed in the constructor.

 Form gen = new Generator(this); gen.Show(); public Generator(Form F1) { InitializeComponent(); form1 = F1 as ParentForm; // now you have parent reference } 

Click Generator Form Button

form1.Somproperty assign values

Hope this helps

0
source

All Articles