Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form frmItem. in C# Visual Studio 2008 Windows Application
In which i have a textbox of txtCompany.
On Enter Event of txtCompany I call a another form frmHelp
In frmHelp form a List Box Appears containing Company Names.
I want to send selected value of ListBox to txtCompany control of frmItem.
On Enter Event of List Box I write code as :

C#
frmItem objfrmItem=new frmItem(lstCompany.SelectedItem.ToString());
objfrmItem.Activate();
this.Close();


I received this value in Constructor of frmItem. and updated the txtCompany.Text value
but txtCompany.Text still not updated.

I am waiting for your support.

Thanks a Lot
Posted
Updated 7-Nov-11 3:56am
v2

show the form as a dialog and then after it has been closed (hopefully with DialogResult.OK) store the value in the textbox.
 
Share this answer
 
Comments
Espen Harlinn 7-Nov-11 11:27am    
What I would have done, my 5 ...
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
well, for the solution of this problem i would like to follow some other way.

Step-1: in frmHelp, declare a string variable say named companyName. Now, encapsulate the variable (Right click on the variable, then go to Refactor and then click Encapsulate Field. Click OK.) You'll find that something like below:

C#
public String CompanyName
{
    get { return companyName; }
    set { companyName = value; }
}


Step-2: after encapsulating when you work with ListBox.SelectedIndexChanged event. Code like below:

C#
private void YourListBoxName_SelectedIndexChanged(object sender, EventArgs e)
{
        //do else...
        this.companyName=YourListBoxName.SelectedItem.ToString();
        //do else if you need to...        
}


Step-3: Now, get into frmItem, where you have textbox for Company Name (txtCompany). Here, according to your way, on TextBox.Enter event call frmHelp like below:

C#
public void txtCompany_Enter(object sender, EventArgs e)
{
       frmHelp fh=new frmHelp();
       fh.ShowDialog();
       if(fh.CompanyName!=String.Empty)
          txtCompany.Text=fh.CompanyName;
       else
          txtCompany.Text="";
}


Remember:
**Use Form.ShowDialog() only while calling the frmHelp.
**When user closes the frmHelp after selecting the listbox in frmHelp, you'll have the value in text box of the frmItem.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900