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 values between forms

Status
Not open for further replies.

mbutch

Programmer
Nov 17, 2000
100
US
I've created a windows app in C# and want to be able to pass an array and its values from the form it's created in to another form. The values would be used to populate a combobox and its values on the second form. Or, could I pass a string variable that is set when a user clicks a button, and just create the array on my second form based on what that variables values are?

Thanks
 
You can fill the combobox after creating the form object, but before calling the .Show method. This works, but I suspect it may change in the future (feels like a bug to me).

The object-way to do this is to create a public property or method that exposes the parts of the combobox that you need for parties external to the form to use to load it (controls are private to their form object).

Chip H.
 
This is the code that I'm using and keep getting a "An object reference is required for the nonstatic field, method, or property 'TheSolustion.Form1.aryReportList'":

In Form1 under
public class Form1 : System.Windows.Forms.Form{
public System.Array aryReportList;

public void BuildArray(string dept)
{
switch (dept)
{
case "accounting":
Array aryReportList=Array.CreateInstance( typeof(Object), 3, 2 );
aryReportList.SetValue("value",0,0);
aryReportList.SetValue("value",0,1);
aryReportList.SetValue("value",1,0);
aryReportList.SetValue("value",1,1);
aryReportList.SetValue("value",2,0);
aryReportList.SetValue("value",2,1);
break;
}
}

In the on click event
public void btnLogin_Click(object sender, System.EventArgs e)
BuildArray("accounting");

In Form2 I try to set the combobox DataSource like this:
this.comboBox1.DataSource = TheSolution.Form1.aryReportList;

What am I doing wrong?

Thanks
 
In Form2 you need to instantiate an instance of Form1. "TheSolution" is a null reference until then.

Do this:
Code:
Form1 TheSolution = new Form1;
this.comboBox1.DataSource = TheSolution.aryReportList;

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top