?
Solved

Passing Variables

Posted on 2005-03-30
17
Medium Priority
?
166 Views
Last Modified: 2010-04-16
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)
0
Comment
Question by:vcgDevelopers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13663339
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
0
 

Author Comment

by:vcgDevelopers
ID: 13668189
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!
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13669559
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:vcgDevelopers
ID: 13672020
here is the error I am getting on form1

'System.Windows.Forms.Form' does not contain a definition for 'ComboBoxValue'
0
 

Author Comment

by:vcgDevelopers
ID: 13706664
any ideas on this error and how I can resolve it?
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13706699
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 ;
     }
}
0
 

Author Comment

by:vcgDevelopers
ID: 13707072
is this VB code or C# code?
0
 
LVL 8

Accepted Solution

by:
rajaloysious earned 2000 total points
ID: 13707153
Oops...it is corrected now...
please correct any systax errors as i havent compiled...

Form2 code
private string _ComboBoxValue ;

public  string ComboBoxValue
{
     get
     {
          return _ComboBoxValue;
     }
     set
     {
          _ComboBoxValue=value ;
     }
}
0
 

Author Comment

by:vcgDevelopers
ID: 13707241
there error remains.  The fix is as follows

Form2 form2 = new Form2();

not

Form FormTwo = new Form2();
0
 

Author Comment

by:vcgDevelopers
ID: 13707595
how can I get this value in form2?
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13707666
say this.ComboBoxValue in Form2
0
 

Author Comment

by:vcgDevelopers
ID: 13708031
can I display this in a message box somewhere?
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13708079
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 );
0
 

Author Comment

by:vcgDevelopers
ID: 13708490
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();
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13709282
should work...-)
0
 

Author Comment

by:vcgDevelopers
ID: 13709359
hmm I'm getting a SQL Error
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13709374
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question