Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variable from one form to another

Status
Not open for further replies.

THURELL

Programmer
Jun 12, 2003
12
US
I have been a Mainframe programmer for 23 years & this is my first attempt at a C# app. I have a multiple form app that I need to pass a variable created in the first form to all other forms. The variable is populated by what is entered in a textbox on the first form
(iRinc = txtRincode.Text) and I need to be able to access that variable on all other forms. Help???
 
Easiest way I found was just creating a new object origForm p, then access members of it using p.textBox1.Text, p.variable.toString() and any other variation.

Code:
 //in origForm

loadForm newForm = new loadForm(this);
newForm.ShowDialog(this);

//In newForm

private origForm origFormInstance;
public newForm(origForm p)
{
    origFormInstance = p;
    InitializeComponent();
    //Overload here and create
}

public newForm()
{
        InitializeComponent();
}
 
John,
I greatly appreciate your response, however, I am really a beginner and in your example code you said 'overload here' and I am sorry to say I have no idea what you mean. Sorry!!
 
By that I mean

Code:
public newForm(origForm p)
{//            ^^^^^^^^^^-overloading
    origFormInstance = p;
    InitializeComponent();
    
}

public newForm()
{
        InitializeComponent();
}
You have 2, one of which has 1 argument. The "public newForm(origForm p)" accepts this passed argument "this" in this case.
You would use this in origForm by:
Code:
private void showForm(object sender, System.EventArgs e)
{
     newForm newForm_show = newForm(this);
     newForm_show.ShowDialog(this);//or .Show()
     /*
       This shows the newForm 
       with the arugment "this", 
       which is origForm, in this example.
     */
}
 
Change this:
Code:
newForm newForm_show = newForm(this);
To:
Code:
newForm newForm_show = new newForm(this);
 
John,
Can you show me some sample code that works that does what I am trying to do? I tried with the info you gave me yesterday, but I can't get it to work.
 
Alrighty, heres a quick example, mind you its long since it includes all of microsofts fluff.

Form1.cs
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace FormPassingDemo
{
	public class Form1 : System.Windows.Forms.Form
	{
		public System.Windows.Forms.TextBox textBox1;
		private System.ComponentModel.Container components = null;
		public Form1()
		{
			InitializeComponent();
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			this.textBox1.Location = new System.Drawing.Point(96, 120);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "Hello";
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			Form2 f_new = new Form2(this);
			f_new.Show();
		}
	}
}

Form2.cs
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace FormPassingDemo
{
	public class Form2 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button button1;
		private Form1 Form1_passed;
		public Form2()
		{
			InitializeComponent();
		}
		public Form2(Form1 p)
		{
			Form1_passed = p;
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			this.textBox1.Location = new System.Drawing.Point(88, 112);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			this.button1.Location = new System.Drawing.Point(104, 144);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "Get Form1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox1);
			this.Name = "Form2";
			this.Text = "Form2";
			this.Load += new System.EventHandler(this.Form2_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void Form2_Load(object sender, System.EventArgs e)
		{
		
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			textBox1.Text = Form1_passed.textBox1.Text;
		}
	}
}
 
John,
I guess I will never make it as a C# programmer. I think I have my code just like the example you gave me and I get the following error:

test.Form1.textbox1 is inaccessible due to its protection level on the following line:
textBox1.Text = Form1_passed.textBox1.Text;
 
John,
I got past the previous error I sent you about & now when I try to run it, I enter text in form1 and press enter and form2 displays with "textBox1" highlighted in form2 textbox not what I entered "1234". When I click on the button I get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred in test.exe

Additional information: Object reference not set to an instance of an object.
 
Are you sure you defined textBox1 as public in form1?
You were never supposed to press enter on the form1 text box. As it is designed now you should enter a value in form1, go to form2 and press "Get Form1" button, which should retrieve the value from Form1.textBox1.
Just in case reverify that you have:
Code:
public System.Windows.Forms.TextBox textBox1;
correctly written in your program.

Regards,
John
 
John,
Yes, that was the first thing I had wrong. I changed that.
Unless I have something wrong both forms display at the same time, and when I enter somthing in Form1 and go to Form2 & click, it works. However, what I want to do is have the user enter data on form1, press enter, and then on form2 display some more data based on what they entered in form1. Can I get it to work that way??
 
John,
After almost a week, I finally got it to work. You started me on the right track and I greatly appreciate your help. Now my next task will be to populate a list box from a database. I don't even want to think about it.
Thanks again!

 
Glad to hear it :), sorry for my rather slow replies, and populating a listbox from a database shouldn't be "too" hard :), enjoy yourself.

Regards,
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top