locked
Returning values entered on a windows form to the main program RRS feed

  • Question

  • I apologize in advance for asking what should be obvious.  I'm not a c# programmer.  I'm trying to learn.  I used Microsoft visual c# 2010 express to create a "Windows Forms Application."  I then created my form.  4 fields, 3 textboxes and 1 button.  I desire to enter text in the three boxes, and then when I click the button, return to the main program along with the data entered in the three boxes.  Eventually, there will be more data (text strings, date/time fields, numbers, and file system references.

    Nothing I do seems to help.  It looks like I'm expected to write my entire program in the button_click object of the form.  That dosn't seem right. I've researched this all over the web.  The best idea I found was to create a custom structure to hold my data and then either return this from the call to the form, or pass it to the form as a "var" so it can be modified.  Again, I can't seem to figure out how to do this...

    I've written a few c# programs, just never one with a "windows form."  Here's my code.  Can anyone give me some ideas as to where to go from here?  Thanks!

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    struct MyStruct
    {
        public string Box1;
        public string Box2;
        public string Box3;
    }
    
    namespace testForm
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                
                var newstr = new MyStruct();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                //newstr = new Form1();
                MessageBox.Show("Made it back","Program.cs");
                MessageBox.Show(newstr.Box1, "Program.cs");
                MessageBox.Show(newstr.Box2, "Program.cs");
                MessageBox.Show(newstr.Box3, "Program.cs");
                MessageBox.Show(newstr.Box1 + " " + newstr.Box2 + " " + newstr.Box3, "Program.cs");
            }
        }
    }
    
    
    Form1.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace testForm
    {
            public partial class Form1 : Form
        {
             public  Form1()      
            {
                InitializeComponent();
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void textBox3_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                var newstr = new MyStruct();
                newstr.Box1 = textBox1.Text;
                newstr.Box2 = textBox2.Text;
                newstr.Box3 = textBox3.Text;
                MessageBox.Show(newstr.Box1,"Form1.cs");
                MessageBox.Show(newstr.Box2, "Form1.cs");
                MessageBox.Show(newstr.Box3, "Form1.cs");
                MessageBox.Show(newstr.Box1 + " " + newstr.Box2 + " " + newstr.Box3, "Form1.cs");
             //   return (newstr);
                this.Close();
            } 
        }
    }
    
    


    Form1.Designer.cs

    namespace testForm
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.textBox3 = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(40, 12);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(300, 20);
                this.textBox1.TabIndex = 0;
                this.textBox1.Text = "This is the text in Box1";
                this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(40, 68);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(300, 20);
                this.textBox2.TabIndex = 2;
                this.textBox2.Text = "This is the text in Box2";
                this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
                // 
                // textBox3
                // 
                this.textBox3.Location = new System.Drawing.Point(40, 124);
                this.textBox3.Name = "textBox3";
                this.textBox3.Size = new System.Drawing.Size(300, 20);
                this.textBox3.TabIndex = 4;
                this.textBox3.Text = "This is the text in Box3";
                this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
                // 
                // button1
                // 
                this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.button1.Location = new System.Drawing.Point(163, 193);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(50, 25);
                this.button1.TabIndex = 5;
                this.button1.Text = "OK";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(384, 262);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.textBox3);
                this.Controls.Add(this.textBox2);
                this.Controls.Add(this.textBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.TextBox textBox3;
            private System.Windows.Forms.Button button1;
        }
    }
    
    
    


    Once again, I apologize if this is a FAQ, or if I have asked it in the wrong place.  I've spent the better part of the week searching the web for answers, and this one has avoided me.

    Harold

    Thursday, September 15, 2011 4:30 PM

Answers

  • Hi Friend,

      There are lot of ways to do this. Let me explain a simple one.

    The goal is, we need to pas value from one form to another. Say Form1 opens Form2 and we enters some value in form 2 and closes it. The entered values should be accessible fro form1.
    First create a class with the properties you need to transfer. Create public property in Form2 of type this class which you created. You will open the form2 like below.

    Form2 frm2=new Form2();

    frm2.ShowDialog();

    now you will get the form2 window as a dialog. you will be entering some values and clicks the button. Once you click the button, update the values in the property you expose in form2 and close the form.

    now you can access those values in form1 like below.

    eg: you set the value something like TexBox1Value.

    string value= frm2.ExposedProperty.TexBox1Value;//where ExposedProperty is the property you exposed and TexBox1Value is a property in it.

    Once again, this is a simple way of transfering data and is synchrounous. you can use delegates to make it asynchrounous. means the dialog no need to be modal. Hope this help...

     


    -- Thanks Ajith R Nair
    • Marked as answer by Jackie-Sun Monday, September 26, 2011 6:21 AM
    Thursday, September 15, 2011 4:53 PM
  • If you don't need the value until after the form is closed:

    In your form:
    
    public MyStruct MyValue { get; private set; }
    
    private void btnMyButton_Click(object sender, EventArgs e)
    {
        if (allGood)
        {
            this.MyValue = new MyStruct(textBox1.Text, textBox2.Text);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        else
            MessageBox.Show("Not all good.");
    }
    
    In main:
    
    MyForm myForm = new MyForm();
    Application.Run(myForm);
    if(myForm.DialogResult == DialogResult.OK)
        DoSomethingWithString(myForm.MyValue);
    else
        Complain();
    

     

    • Proposed as answer by servy42 Thursday, September 15, 2011 5:17 PM
    • Marked as answer by Jackie-Sun Monday, September 26, 2011 6:21 AM
    Thursday, September 15, 2011 5:00 PM

All replies

  • Can I ask you, what is the point of returning into the Main method?

    Do you want to return in the main method, just under the code from where you ran From1?

    If you want to come back, you have to create a NEW instance of the class where the Main method is in (by default this is Program class, but I even can be done line this:

     

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
            }
        }
    

     

    .. but anyway, the Main method goes through when you call "Application.Run(new Form())". So there is actually no way to come back - it is, but it will execute AGAIN the whole code in the main method.

    PS: in my opinion, I dont see any point is this doing, so I suggest you not to do it so.

    Question: what is the purpose of doing so (to go back to Main method, after click on Button on Form1)?


    Mitja
    • Edited by Mitja Bonca Thursday, September 15, 2011 5:03 PM
    Thursday, September 15, 2011 4:51 PM
  • Hi Friend,

      There are lot of ways to do this. Let me explain a simple one.

    The goal is, we need to pas value from one form to another. Say Form1 opens Form2 and we enters some value in form 2 and closes it. The entered values should be accessible fro form1.
    First create a class with the properties you need to transfer. Create public property in Form2 of type this class which you created. You will open the form2 like below.

    Form2 frm2=new Form2();

    frm2.ShowDialog();

    now you will get the form2 window as a dialog. you will be entering some values and clicks the button. Once you click the button, update the values in the property you expose in form2 and close the form.

    now you can access those values in form1 like below.

    eg: you set the value something like TexBox1Value.

    string value= frm2.ExposedProperty.TexBox1Value;//where ExposedProperty is the property you exposed and TexBox1Value is a property in it.

    Once again, this is a simple way of transfering data and is synchrounous. you can use delegates to make it asynchrounous. means the dialog no need to be modal. Hope this help...

     


    -- Thanks Ajith R Nair
    • Marked as answer by Jackie-Sun Monday, September 26, 2011 6:21 AM
    Thursday, September 15, 2011 4:53 PM
  • If you don't need the value until after the form is closed:

    In your form:
    
    public MyStruct MyValue { get; private set; }
    
    private void btnMyButton_Click(object sender, EventArgs e)
    {
        if (allGood)
        {
            this.MyValue = new MyStruct(textBox1.Text, textBox2.Text);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        else
            MessageBox.Show("Not all good.");
    }
    
    In main:
    
    MyForm myForm = new MyForm();
    Application.Run(myForm);
    if(myForm.DialogResult == DialogResult.OK)
        DoSomethingWithString(myForm.MyValue);
    else
        Complain();
    

     

    • Proposed as answer by servy42 Thursday, September 15, 2011 5:17 PM
    • Marked as answer by Jackie-Sun Monday, September 26, 2011 6:21 AM
    Thursday, September 15, 2011 5:00 PM