Click here to Skip to main content
14,176,878 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I'm trying to access the variables in a child form. I am transitioning from a multi-form WinForm program to a MDI with a ribbon. A lot of the code in the child forms is the same and I would like to code as much is the parent form. I can access and modify controls in the child, but can't figure out how, or find any hints as to how to access and change the variables.

My child forms include a ListBox that is populated from a List<t>. In changing the data to display I normally change the List<t> and the set the ListBox's Datasource to point to the list.

What I have tried:

I've tried making them public, and have tried to use accessors.

The workaround that I am using is to use a child form control (label), and change that. The child control xxChanged property then takes the appropriate action. The only thing is the code is still in the child.

Isn't there a collection (like in Controls) of variables in the child form I can address?
Posted
Updated 4-Mar-17 11:32am
Comments
Graeme_Grant 20-Feb-17 15:50pm
   
What have you tried so far?
Dave Kreskowiak 20-Feb-17 15:57pm
   
Why MDI?? It's been dead for a long time now.

The MDI mode in Windows Forms was for multiple document editor windows. For example, a Notepad that can open and edit multiple files at the same time. If something like this is not what you're doing, MDI is a bad choice.

If you're using it because you have the same code in multiple child forms then the solution is to have a form that contains this common code and subclass your specific child forms from it, not move the code to your Main form.
Dana Bell 21-Feb-17 12:57pm
   
Yes, I do want MDI. I will have multiple child windows, each with different data, and I would like them trapped in the main programs window instead of scattered across the desktop. You can see the multi-form version at http://www.tylerhosting.com/hoot/help/.

Subclassing is what I'm beginning to move to. It'll be tedious since forms have various degrees of similarity. I supposed I could build another level of inheritance.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Google Search is your friend... A quick search highlights many possible results that could help you. Have you tried doing any research?
   
Comments
Dana Bell 21-Feb-17 12:48pm
   
Yes. I have searched for a couple of hours. I've found how to access child controls, how to send data to a child form, how to access parent. Nothing about accessing child form variables (i.e. Lists).
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I think you're approaching this problem the wrong way.

If you want to re-use code that populates a list control from a collection or data source, abstract that functionality into a method in a new helper class (e.g. ListViewHelper) like so:
public static PopulateListView<br />
    (ListView listView,<br />
     IEnumerable<MyDataObject> dataObjects)
and call that method in your various Forms.

/ravi
   
Comments
Dana Bell 21-Feb-17 15:20pm
   
That sounds like a good idea. But, then, that wouldn't save many steps in my case. I use a ListBox instead and populate it by assigning a List datasource to it with a single line of code.

With
List<hookset> resultSet;
ListBox lstResults;

I use the commands
resultSet = Hoot.Hooks.get_Hooks(search, LexWords.hootData); // search
or resultSet = letterFilter.Cast<hookset>().OrderBy(Word => Word.ToString()).ToList(); // sort

then
lstResults.DataSource = resultSet;

The rest of the code modifies labels and such. Since I can't execute the commands in the parent, I'm thinking now the better idea is a set of inherited forms.
Ravi Bhavnani 21-Feb-17 16:08pm
   
> inherited forms.
Ouch, ouch, ouch. That will rely on your inherited form to have labels with the same name as the parent form. Inheriting UI functionality is fine but inheriting the raw UI is awful due to the restrictions it places on naming. If you want to inherit the form, have your inherited method be generic - i.e. let it accept a Label control as an argument (vs. assuming the child form has a label named "lblFoo").

/ravi
Dana Bell 22-Feb-17 17:05pm
   
I've created one and it's not so bad. The biggest issue is the fact that inherited forms can not accept passed variables, apparently because it only allows one constructor. I suppose I could work around that by creating a inherited form that gets the "passed variables" somewhere else, or the caller could populate a control on the inherited form.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

While I did not find a way to access variables in a child form directly, I did figure out a way to execute code in the child form from the ribbon.

1. Create a label on the child form

2. Make it public (or protected)

3. Add a TextChanged event for the label

4. Find and change the label text from the ribbon.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190526.1 | Last Updated 4 Mar 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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