Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Passing Variables

Posted on 2005-03-30
17
Medium Priority
?
167 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

571 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