How to use value of a combo box from one page to another page?

Hi,

I m developing a desktop application using C#. I want to use value, selected at run time of combobox to other page. But i m not able to do that. I have tried by making function also. Can anybody tell me how to use value of one control from one page to another page. Like we used to do in web application by Session variable.
[340 byte] By [khushi] at [2007-11-19 9:00:32]
# 1 Re: How to use value of a combo box from one page to another page?
I assume you are developing a windows form application, and that you by "page" mean "form".

If this is the case, then you just have to make your ComboBox public. For example

public class ComboBoxForm : Form
{
public ComboBox CB;
+ /// <summary> ...
+ private void InitializeComponent() ...
}

I have tried to mimic the outlining in VS here :-) Anyway, with the above declaration you can instansiate and use the ComboBoxForm, like so:

...
// This is another class where we have an instance of the
// ComboBoxForm, created somewhere else.
protected ComboBoxForm CBF;
...
public void UseComboBoxForm()
{
// Show currently selected item.
MessageBox.Show( "selected item: " + CBF.CB.SelectedItem.ToString() );
}

HTH!
Anders at 2007-11-9 1:48:00 >
# 2 Re: How to use value of a combo box from one page to another page?
Don't make controls public ! This breaks all the advantages of encapsulation !

Instead you can add a public property to get the text out of the combo box e.g.

class MyForm : Form
{
private ComboBox m_comboBox;

public string ComboBoxText
{
get
{
return m_comboBox.Text;
}
}
}

Then in the controlling code you'll have something like :

MyForm form1 = new MyForm();

if (form1.ShowDialog() == DialogResult.OK)
{
string sComboBoxText = form1.ComboBoxText;

// the other form
MyForm2 form2 = new MyForm2();

// this has a property to set its combo boxes text
form2.ComboBoxText = sComboBoxText;

if (form2.ShowDialog() == DialogResult.OK)
{
// continue
}
}

I hope this demonstrates what I mean.

But under no circumstances should you have public variables in classes. In fact I never have public variables in structs either.

Darwen.
darwen at 2007-11-9 1:48:54 >