Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello There

I have a C# application in which there are two forms. ParentForm contains several controls like TextBoxes,DataGridView etc. Now I want to fetch the values of these controls on ChildForm.

In ParentForm, I declared this controls as Public and In ChildForm, I create object of ParentForm as ParentForm uxParent = new ParentForm().

There is a DataGridView Control in ParentForm which has 5 rows. Now I want to get the value of these rows in ChildForm but the rowcount display as 0.

Need Suggestion
Posted
Comments
[no name] 2-Feb-13 2:04am    
Pass values using Constructors
Mayank Topiwala 2-Feb-13 2:07am    
For that I have to create multiple constructors. Can I have any other options ?
[no name] 2-Feb-13 2:33am    
Is it form app ?
Mayank Topiwala 2-Feb-13 2:41am    
Windows Application

1 solution

Really, don't do that.
The first problem is that
C#
ParentForm uxParent = new ParentForm();
Does not give you access to the actual parent - the new keyword is a clue here - it generates a new instance of the parent form. If your parent form automatically creates an instance of the child form in it's constructor, your program will immediately crash because it will loop itself. To do anything like that, yoiu need access to teh actual instance of the parent form.

While that is available (as Kishor syas in his solution), it is a very bad idea to use it for something like this, because it ties the design of the two forms together so that neither one can exist without the other, and you cannot make changes to one without considering any possible effects on the other. This is very bad practice, and against the principles of OOP. This is why all form controls are declared private by default!

It's not difficult to do the job properly - all you have to do is create properties in the child form to supply the data to, create an event in the child form which says it needs the data, and handle the event in the parent:
Child form:
C#
public string MyProperty
    {
    get { return myTextBox.Text; }
    set { myTextBox.Text = value; }
    }
/// <summary>
/// Event to indicate DataRequired
/// </summary>
public event EventHandler ReadyForData;
/// <summary>
/// Called to signal to subscribers that DataRequired
/// </summary>
/// <param name="e"></param>
protected virtual void OnReadyForData(EventArgs e)
    {
    EventHandler eh = ReadyForData;
    if (eh != null)
        {
        eh(this, e);
        }
    }
To signal you need the data, just call the method:
C#
OnReadyForData(null);

In the parent form, add a handler when you construct the child instance:
C#
    frmChild myChildForm = new frmChild();
    ... Do your other child initialization
    myChildForm.ReadyForData += new EventHandler(myChildForm_ReadyForData);
    }

void myChildForm_ReadyForData(object sender, EventArgs e)
    {
    frmChild child = sender as frmChild;
    if (child != null)
        {
        child.MyProperty = "Here is the new data";
        }
    }
 
Share this answer
 
Comments
Mayank Topiwala 2-Feb-13 3:40am    
thanx originalgriff and can i have some website links through which I can design main menu of ERP.

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