How to get combo box selected index changed event in another page from the existing page in winforms

N

Naresh

Hi,

I am working on windows forms project. I have two winforms (window1 and
window2).
I have a combobox called cmbEmail in window1 and Textbox called txtName in
window2.
I want to retrieve some information into the txtName in window2 when I
selected something from cmbEmail in window1.
If I write code to retrieve something into the txtName (which is in window2)
in cmbEmail SelectedIndexchanged event in window1, I will get an error of
txtName doesn't exist in this page.

Can anybody help me to solve this problem..


Thanks and Regards
Naresh
 
D

David McCallum

Naresh said:
Hi,

I am working on windows forms project. I have two winforms (window1 and
window2).
I have a combobox called cmbEmail in window1 and Textbox called txtName in
window2.
I want to retrieve some information into the txtName in window2 when I
selected something from cmbEmail in window1.
If I write code to retrieve something into the txtName (which is in
window2) in cmbEmail SelectedIndexchanged event in window1, I will get an
error of txtName doesn't exist in this page.

Can anybody help me to solve this problem..


Thanks and Regards
Naresh

Add a property to windows 1:

public string Name
{
get { return txtName.Text; }
}

David.
 
P

Peter Duniho

David said:
I am working on windows forms project. I have two winforms (window1
and window2).
I have a combobox called cmbEmail in window1 and Textbox called
txtName in window2.
I want to retrieve some information into the txtName in window2 when I
selected something from cmbEmail in window1. [...]

Add a property to windows 1:

public string Name
{
get { return txtName.Text; }
}

Well, assuming these are in fact Form sub-classes as the OP says, the
above won't work, because Form already has a Name property.

I also read his statement that he wants "to retrieve some information
into the txtName" to mean he wants to _set_ the value, not get it. The
word "retrieve" is used in a misleading way, but I'm guessing English is
not his native language.

All that said, I do agree that a property is the best way to approach
the problem. It just needs to be something more like this:

class Windows2 : Form
{
public string CurrentName
{
get { return txtName.Text; }
set { txtName.Text = value; }
}
}

He may or may not need the getter. Strictly speaking, based on the
problem statement I don't think it's required. But it would probably be
useful to include it anyway.

Pete
 
C

coder316

David said:
I am working on windows forms project. I have two winforms (window1
and window2).
I have a combobox called cmbEmail in window1 and Textbox called
txtName in window2.
I want to retrieve some information into the txtName in window2 when I
selected something from cmbEmail in window1. [...]
Add a property to windows 1:
       public string Name
       {
           get { return txtName.Text; }
       }

Well, assuming these are in fact Form sub-classes as the OP says, the
above won't work, because Form already has a Name property.

I also read his statement that he wants "to retrieve some information
into the txtName" to mean he wants to _set_ the value, not get it.  The
word "retrieve" is used in a misleading way, but I'm guessing English is
not his native language.

All that said, I do agree that a property is the best way to approach
the problem.  It just needs to be something more like this:

   class Windows2 : Form
   {
     public string CurrentName
     {
       get { return txtName.Text; }
       set { txtName.Text = value; }
     }
   }

He may or may not need the getter.  Strictly speaking, based on the
problem statement I don't think it's required.  But it would probably be
useful to include it anyway.

Pete

This worked for me:
Textbox1(on Form frm2) must be public.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 frm2;
public Form1()
{
InitializeComponent();
frm2 = new Form2();
frm2.Show();

}

private void comboBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
frm2.textBox1.Text = this.comboBox1.SelectedItem.ToString
();
}
}
}
 
P

Peter Duniho

coder316 said:
This worked for me:

What you did will work, but it's wrong, because:
Textbox1(on Form frm2) must be public.

Making the field public is one of the worst ways to solve the problem.
It breaks encapsulation, showing implementation detail to the client
code unnecessarily.

That's what makes the property approach so useful. The only thing that
is exposed is the class interface of the single string value that can be
set and/or retrieved. That's the whole reason that properties are even
in C#; to enable that kind of scenario.

Pete
 
C

coder316

What you did will work, but it's wrong, because:


Making the field public is one of the worst ways to solve the problem.
It breaks encapsulation, showing implementation detail to the client
code unnecessarily.

That's what makes the property approach so useful.  The only thing that
is exposed is the class interface of the single string value that can be
set and/or retrieved.  That's the whole reason that properties are even
in C#; to enable that kind of scenario.

Pete

Thanks Peter,
Your absolutly right. I was trying to point out that the OP "Probably"
was missing a reference to Form2 , thats why it was unavailable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top