Returning values entered on a windows form to the main program

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I apologize in advance for asking what should be obvious. Im not a c# programmer. Im 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 Im expected to write my entire program in the button_click object of the form. That dosnt seem right. Ive 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 cant seem to figure out how to do this...
Ive written a few c# programs, just never one with a "windows form." Heres my code. Can anyone give me some ideas as to where to go from here? Thanks!
Program.cs

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Windows.Forms;

<span style="color:Blue; struct MyStruct
{
<span style="color:Blue; public <span style="color:Blue; string Box1;
<span style="color:Blue; public <span style="color:Blue; string Box2;
<span style="color:Blue; public <span style="color:Blue; string Box3;
}

<span style="color:Blue; namespace testForm
{
<span style="color:Blue; static <span style="color:Blue; class Program
{
<span style="color:Gray; /// <span style="color:Gray; <summary>
<span style="color:Gray; ///<span style="color:Green; The main entry point for the application.
<span style="color:Gray; /// <span style="color:Gray; </summary>
[STAThread]
<span style="color:Blue; static <span style="color:Blue; void Main()
{

<span style="color:Blue; var newstr = <span style="color:Blue; new MyStruct();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(<span style="color:Blue; false);
Application.Run(<span style="color:Blue; new Form1());
<span style="color:Green; //newstr = new Form1();
MessageBox.Show(<span style="color:#A31515; "Made it back",<span style="color:#A31515; "Program.cs");
MessageBox.Show(newstr.Box1, <span style="color:#A31515; "Program.cs");
MessageBox.Show(newstr.Box2, <span style="color:#A31515; "Program.cs");
MessageBox.Show(newstr.Box3, <span style="color:#A31515; "Program.cs");
MessageBox.Show(newstr.Box1 + <span style="color:#A31515; " " + newstr.Box2 + <span style="color:#A31515; " " + newstr.Box3, <span style="color:#A31515; "Program.cs");
}
}
}

[/code]
Form1.cs<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;

<span style="color:Blue; namespace testForm
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; public Form1()
{
InitializeComponent();
}

<span style="color:Blue; private <span style="color:Blue; void textBox1_TextChanged(<span style="color:Blue; object sender, EventArgs e)
{

}

<span style="color:Blue; private <span style="color:Blue; void textBox2_TextChanged(<span style="color:Blue; object sender, EventArgs e)
{

}

<span style="color:Blue; private <span style="color:Blue; void textBox3_TextChanged(<span style="color:Blue; object sender, EventArgs e)
{

}

<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{

<span style="color:Blue; var newstr = <span style="color:Blue; new MyStruct();
newstr.Box1 = textBox1.Text;
newstr.Box2 = textBox2.Text;
newstr.Box3 = textBox3.Text;
MessageBox.Show(newstr.Box1,<span style="color:#A31515; "Form1.cs");
MessageBox.Show(newstr.Box2, <span style="color:#A31515; "Form1.cs");
MessageBox.Show(newstr.Box3, <span style="color:#A31515; "Form1.cs");
MessageBox.Show(newstr.Box1 + <span style="color:#A31515; " " + newstr.Box2 + <span style="color:#A31515; " " + newstr.Box3, <span style="color:#A31515; "Form1.cs");
<span style="color:Green; // return (newstr);
<span style="color:Blue; this.Close();
}
}
}

[/code]

<br/>
Form1.Designer.cs

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; namespace testForm
{
<span style="color:Blue; partial <span style="color:Blue; class Form1
{
<span style="color:Gray; /// <span style="color:Gray; <summary>
<span style="color:Gray; ///<span style="color:Green; Required designer variable.
<span style="color:Gray; /// <span style="color:Gray; </summary>
<span style="color:Blue; private System.ComponentModel.IContainer components = <span style="color:Blue; null;

<span style="color:Gray; /// <span style="color:Gray; <summary>
<span style="color:Gray; ///<span style="color:Green; Clean up any resources being used.
<span style="color:Gray; /// <span style="color:Gray; </summary>
<span style="color:Gray; /// <span style="color:Gray; <param name="disposing <span style="color:Green; true if managed resources should be disposed; otherwise, false.</param>
<span style="color:Blue; protected <span style="color:Blue; override <span style="color:Blue; void Dispose(<span style="color:Blue; bool disposing)
{
<span style="color:Blue; if (disposing && (components != <span style="color:Blue; null))
{
components.Dispose();
}
<span style="color:Blue; base.Dispose(disposing);
}

<span style="color:Blue; #region Windows Form Designer generated code

<span style="color:Gray; /// <span style="color:Gray; <summary>
<span style="color:Gray; ///<span style="color:Green; Required method for Designer support - do not modify
<span style="color:Gray; ///<span style="color:Green; the contents of this method with the code editor.
<span style="color:Gray; /// <span style="color:Gray; </summary>
<span style="color:Blue; private <span style="color:Blue; void InitializeComponent()
{
<span style="color:Blue; this.textBox1 = <span style="color:Blue; new System.Windows.Forms.TextBox();
<span style="color:Blue; this.textBox2 = <span style="color:Blue; new System.Windows.Forms.TextBox();
<span style="color:Blue; this.textBox3 = <span style="color:Blue; new System.Windows.Forms.TextBox();
<span style="color:Blue; this.button1 = <span style="color:Blue; new System.Windows.Forms.Button();
<span style="color:Blue; this.SuspendLayout();
<span style="color:Green; //
<span style="color:Green; // textBox1
<span style="color:Green; //
<span style="color:Blue; this.textBox1.Location = <span style="color:Blue; new System.Drawing.Point(40, 12);
<span style="color:Blue; this.textBox1.Name = <span style="color:#A31515; "textBox1";
<span style="color:Blue; this.textBox1.Size = <span style="color:Blue; new System.Drawing.Size(300, 20);
<span style="color:Blue; this.textBox1.TabIndex = 0;
<span style="color:Blue; this.textBox1.Text = <span style="color:#A31515; "This is the text in Box1";
<span style="color:Blue; this.textBox1.TextChanged += <span style="color:Blue; new System.EventHandler(<span style="color:Blue; this.textBox1_TextChanged);
<span style="color:Green; //
<span style="color:Green; // textBox2
<span style="color:Green; //
<span style="color:Blue; this.textBox2.Location = <span style="color:Blue; new System.Drawing.Point(40, 68);
<span style="color:Blue; this.textBox2.Name = <span style="color:#A31515; "textBox2";
<span style="color:Blue; this.textBox2.Size = <span style="color:Blue; new System.Drawing.Size(300, 20);
<span style="color:Blue; this.textBox2.TabIndex = 2;
<span style="color:Blue; this.textBox2.Text = <span style="color:#A31515; "This is the text in Box2";
<span style="color:Blue; this.textBox2.TextChanged += <span style="color:Blue; new System.EventHandler(<span style="color:Blue; this.textBox2_TextChanged);
<span style="color:Green; //
<span style="color:Green; // textBox3
<span style="color:Green; //
<span style="color:Blue; this.textBox3.Location = <span style="color:Blue; new System.Drawing.Point(40, 124);
<span style="color:Blue; this.textBox3.Name = <span style="color:#A31515; "textBox3";
<span style="color:Blue; this.textBox3.Size = <span style="color:Blue; new System.Drawing.Size(300, 20);
<span style="color:Blue; this.textBox3.TabIndex = 4;
<span style="color:Blue; this.textBox3.Text = <span style="color:#A31515; "This is the text in Box3";
<span style="color:Blue; this.textBox3.TextChanged += <span style="color:Blue; new System.EventHandler(<span style="color:Blue; this.textBox3_TextChanged);
<span style="color:Green; //
<span style="color:Green; // button1
<span style="color:Green; //
<span style="color:Blue; this.button1.Font = <span style="color:Blue; new System.Drawing.Font(<span style="color:#A31515; "Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((<span style="color:Blue; byte)(0)));
<span style="color:Blue; this.button1.Location = <span style="color:Blue; new System.Drawing.Point(163, 193);
<span style="color:Blue; this.button1.Name = <span style="color:#A31515; "button1";
<span style="color:Blue; this.button1.Size = <span style="color:Blue; new System.Drawing.Size(50, 25);
<span style="color:Blue; this.button1.TabIndex = 5;
<span style="color:Blue; this.button1.Text = <span style="color:#A31515; "OK";
<span style="color:Blue; this.button1.UseVisualStyleBackColor = <span style="color:Blue; true;
<span style="color:Blue; this.button1.Click += <span style="color:Blue; new System.EventHandler(<span style="color:Blue; this.button1_Click);
<span style="color:Green; //
<span style="color:Green; // Form1
<span style="color:Green; //
<span style="color:Blue; this.AutoScaleDimensions = <span style="color:Blue; new System.Drawing.SizeF(6F, 13F);
<span style="color:Blue; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
<span style="color:Blue; this.ClientSize = <span style="color:Blue; new System.Drawing.Size(384, 262);
<span style="color:Blue; this.Controls.Add(<span style="color:Blue; this.button1);
<span style="color:Blue; this.Controls.Add(<span style="color:Blue; this.textBox3);
<span style="color:Blue; this.Controls.Add(<span style="color:Blue; this.textBox2);
<span style="color:Blue; this.Controls.Add(<span style="color:Blue; this.textBox1);
<span style="color:Blue; this.Name = <span style="color:#A31515; "Form1";
<span style="color:Blue; this.Text = <span style="color:#A31515; "Form1";
<span style="color:Blue; this.ResumeLayout(<span style="color:Blue; false);
<span style="color:Blue; this.PerformLayout();

}

<span style="color:Blue; #endregion

<span style="color:Blue; private System.Windows.Forms.TextBox textBox1;
<span style="color:Blue; private System.Windows.Forms.TextBox textBox2;
<span style="color:Blue; private System.Windows.Forms.TextBox textBox3;
<span style="color:Blue; private System.Windows.Forms.Button button1;
}
}


[/code]

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

View the full article
 
Back
Top