469,588 Members | 2,680 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

Can i access list<> from 1 form to another?

I have created an application where i have 2 windows forms.form1 and form2.
there is data in list<> in form1.and now i want to access that information in form2.can i access the list<> in form2?and if yes,how?
Apr 13 '10 #1
3 5285
You can do something like this in form2, just make sure your list can be accessed from outwith form1.

Expand|Select|Wrap|Line Numbers
  1. Form1 nForm = new Form1();
  2. nForm.List.Items.Add("");
Apr 13 '10 #2
3,525 Expert 2GB
Kyndig: That isn't really going to do what the OP wants. She already has a Form1 and Form2. You are making a *new* form1 as the child of form2. So you would be accessing a different list of data than in the original form1.
Apr 13 '10 #3
3,525 Expert 2GB
How do I get my Form2 to react to something on my Form1?
How do I make my Form1 control something on my Form2?
Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
Events tutorial (including Form to Form which is the same as class to class)
This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.
Bad: Directly accessing controls of one class/form from another.
Expand|Select|Wrap|Line Numbers
  1. bool IsOn = Form2.button1.IsChecked;

Good: Use a property to get such information
Expand|Select|Wrap|Line Numbers
  1. //Form1
  2. bool IsOn = Form2.IsOn;
Expand|Select|Wrap|Line Numbers
  1. //Form 2
  2. public bool IsOn
  3. {
  4.    get { return button1.Checked; }
  5.    set { button1.Checked = value; }
  6. }
It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: No break in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden on not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentalize the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

Expand|Select|Wrap|Line Numbers
  1. public bool IsOn
  2. {
  3.    get { return btnMeaningfulName.Checked; }
  4.    set {
  5.             btnMeaningfulName.Checked = value;
  6.             panelDashboard.Visible = value;
  7.             labelStatus = value == true ? "On" : "Off";
  8.             btnRunNow.Enabled = value;
  9. }
Now when Form1 tells Form2 to turn on, Form2 will check a box, make an entire panel visible, change its Status label to say 'On' and enable a Run Now button.
Apr 13 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Jegger | last post: by
14 posts views Thread by Dave | last post: by
1 post views Thread by Diego | last post: by
7 posts views Thread by Andrew Robinson | last post: by
2 posts views Thread by Ben Pfaff | last post: by
9 posts views Thread by =?Utf-8?B?VHJlY2l1cw==?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.