vcgDevelopers
asked on
Passing Variables
I have a C# program with 5 forms being used. On one form I have the user select data from a listbox and then click the next button to bring them to form 2. How do I get the data they chose from the listbox in form 1 to form 2?
I'm sorry I'm very new to this (was thrown on this project at the last minute and I have no C# experience)
I'm sorry I'm very new to this (was thrown on this project at the last minute and I have no C# experience)
ASKER
that worked great! One last thing - how would I do this same thing for a combobox? I've doubled the points to thank you for your help with this answer in advance!
Form2 code
private _ComboBoxValue as string
public string ComboBoxValue
{
get
{
return _ComboBoxValue;
}
set
{
_ComboBoxValue=value ;
}
}
Form1 Code
Before you call up second form
Form FormTwo = new Form2();
FormTwo.ComboBoxValue= this.Combobox1.SelectedVal ue.ToStrin g() // Please check for exact syntax
// also the following will work
//FormTwo.ComboBoxValue = this.Combobox1.List[this.C ombobox1.S electedInd ex];
Form2.Show()
In Form2 Again:
Refer to the listbox value as this.ComboBoxValue
cheers
private _ComboBoxValue as string
public string ComboBoxValue
{
get
{
return _ComboBoxValue;
}
set
{
_ComboBoxValue=value ;
}
}
Form1 Code
Before you call up second form
Form FormTwo = new Form2();
FormTwo.ComboBoxValue= this.Combobox1.SelectedVal
// also the following will work
//FormTwo.ComboBoxValue = this.Combobox1.List[this.C
Form2.Show()
In Form2 Again:
Refer to the listbox value as this.ComboBoxValue
cheers
ASKER
here is the error I am getting on form1
'System.Windows.Forms.Form ' does not contain a definition for 'ComboBoxValue'
'System.Windows.Forms.Form
ASKER
any ideas on this error and how I can resolve it?
check whether you have changed this as in the previous post...
Form2 code
private _ComboBoxValue as string
public string ComboBoxValue
{
get
{
return _ComboBoxValue;
}
set
{
_ComboBoxValue=value ;
}
}
Form2 code
private _ComboBoxValue as string
public string ComboBoxValue
{
get
{
return _ComboBoxValue;
}
set
{
_ComboBoxValue=value ;
}
}
ASKER
is this VB code or C# code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
there error remains. The fix is as follows
Form2 form2 = new Form2();
not
Form FormTwo = new Form2();
Form2 form2 = new Form2();
not
Form FormTwo = new Form2();
ASKER
how can I get this value in form2?
say this.ComboBoxValue in Form2
ASKER
can I display this in a message box somewhere?
Form1:
=======
Form2 FormTwo = new Form2();
FormTwo.ComboBoxValue= this.Combobox1.SelectedVal ue.ToStrin g() // Please check for exact syntax
// also the following will work
//FormTwo.ComboBoxValue = this.Combobox1.List[this.C ombobox1.S electedInd ex];
MessageBox.Show(FormTwo.Co mboBoxValu e );
Form2.Show()
Form2
=====
In Form_Load event
MessageBox.Show(this.Combo BoxValue );
=======
Form2 FormTwo = new Form2();
FormTwo.ComboBoxValue= this.Combobox1.SelectedVal
// also the following will work
//FormTwo.ComboBoxValue = this.Combobox1.List[this.C
MessageBox.Show(FormTwo.Co
Form2.Show()
Form2
=====
In Form_Load event
MessageBox.Show(this.Combo
ASKER
last question (at least on this post ;-) )
will this work to access the database using my passed value?
cn.Open();
//ADD WHERE
cmd = new SqlCommand("SELECT SiteID, SiteName, NumFloors FROM Site WHERE SiteName = " + this.ComboBoxValue);
SqlDataReader reader = cmd.ExecuteReader();
will this work to access the database using my passed value?
cn.Open();
//ADD WHERE
cmd = new SqlCommand("SELECT SiteID, SiteName, NumFloors FROM Site WHERE SiteName = " + this.ComboBoxValue);
SqlDataReader reader = cmd.ExecuteReader();
should work...-)
ASKER
hmm I'm getting a SQL Error
cmd = new SqlCommand("SELECT SiteID, SiteName, NumFloors FROM Site WHERE SiteName = '" + this.ComboBoxValue + "'");
i have added the single quotes before and after this.Comboboxvalue as this is a string...
cheers
i have added the single quotes before and after this.Comboboxvalue as this is a string...
cheers
Form2 code
private _ListBoxValue as string
public string ListBoxValue
{
get
{
return _ListBoxValue;
}
set
{
_ListBoxValue=value ;
}
}
Form1 Code
Before you call up second form
Form FormTwo = new Form2();
FormTwo.ListBoxValue = this.Listbox1.SelectedValu
Form2.Show()
In Form2 Again:
Refer to the listbox value as this.ListBoxValue
If you want to transer multiple values declare ListBoxValue as ArrayList instead of string
Hope this helps...
Cheers