Link to home
Start Free TrialLog in
Avatar of vcgDevelopers
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)
Avatar of rajaloysious
rajaloysious

Have  public  property declared in Form2 which may hold the values in listbox

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.SelectedValue.ToString() // Please check for exact syntax
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
Avatar of vcgDevelopers

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.SelectedValue.ToString() // Please check for exact syntax
// also  the following will work
//FormTwo.ComboBoxValue = this.Combobox1.List[this.Combobox1.SelectedIndex];
Form2.Show()

In Form2 Again:
Refer to the listbox value as this.ComboBoxValue

cheers
here is the error I am getting on form1

'System.Windows.Forms.Form' does not contain a definition for 'ComboBoxValue'
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 ;
     }
}
is this VB code or C# code?
ASKER CERTIFIED SOLUTION
Avatar of rajaloysious
rajaloysious

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
there error remains.  The fix is as follows

Form2 form2 = new Form2();

not

Form FormTwo = new Form2();
how can I get this value in form2?
say this.ComboBoxValue in Form2
can I display this in a message box somewhere?
Form1:
=======
Form2 FormTwo = new Form2();
FormTwo.ComboBoxValue= this.Combobox1.SelectedValue.ToString() // Please check for exact syntax
// also  the following will work
//FormTwo.ComboBoxValue = this.Combobox1.List[this.Combobox1.SelectedIndex];

MessageBox.Show(FormTwo.ComboBoxValue );
Form2.Show()

Form2
=====
In Form_Load event
MessageBox.Show(this.ComboBoxValue );
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();
should work...-)
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