Click here to Skip to main content
11,634,634 members (63,501 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 1-Feb-13 20:00pm
Comments
Krunal Rohit at 2-Feb-13 2:04am
   
Pass values using Constructors
Mayank Topiwala at 2-Feb-13 2:07am
   
For that I have to create multiple constructors. Can I have any other options ?
Krunal Rohit at 2-Feb-13 2:33am
   
Is it form app ?
Mayank Topiwala at 2-Feb-13 2:41am
   
Windows Application

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Really, don't do that.
The first problem is that
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:
        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:
            OnReadyForData(null);
In the parent form, add a handler when you construct the child instance:
            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";
                }
            }
  Permalink  
Comments
Mayank Topiwala at 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)

  Print Answers RSS
0 OriginalGriff 8,736
1 Sergey Alexandrovich Kryukov 8,359
2 Mika Wendelius 6,845
3 F-ES Sitecore 2,354
4 Suvendu Shekhar Giri 2,205


Advertise | Privacy | Mobile
Web04 | 2.8.150728.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100