Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hi..
i want to know how to access main Form component from another class(not form...just a class)...thank you
Posted 24-Apr-11 4:55am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Typically, this would be Application.OpenForms[0] (You will need to cast it to the derived form type). Alternatively you could store a reference to the main form in a static variable.
 
[Update]
-----------
 
In response to your comment, see this example code:
 
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}
 
public class Class1
{
  public Class1()
  {
    Form1 mainForm = (Form1)Application.OpenForms[0];
  }
}
  Permalink  
v2
Comments
Kim Togo at 24-Apr-11 10:28am
   
My 5. Application.OpenForms is a collection of open forms owned by the application.
guendouz bachir at 24-Apr-11 10:33am
   
Mr Nishant Sivakumar give me an example (demo) or an article explain that
Nishant Sivakumar at 24-Apr-11 10:35am
   
It's just one line. MainForm mainForm = (MainForm)Application.OpenForms[0];
guendouz bachir at 24-Apr-11 10:45am
   
I'll even with
Nishant Sivakumar at 24-Apr-11 10:46am
   
>> I'll even with
<<
. . . . . .
What does that mean?
guendouz bachir at 24-Apr-11 12:19pm
   
I tried what you told me, but it don't work ...see the comment in down
Nishant Sivakumar at 24-Apr-11 12:26pm
   
I don't see any code where you've done what I suggested above.
guendouz bachir at 24-Apr-11 12:34pm
   
Explained to me more, i can't understand your suggested above(it don't work)...
Nishant Sivakumar at 24-Apr-11 12:39pm
   
I have updated my answer now.
guendouz bachir at 24-Apr-11 12:37pm
   
see the solution 3 i had put an explain ...i hope that u can understand it
Nishant Sivakumar at 24-Apr-11 12:39pm
   
Check the example in my updated answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Another way which I think is the most robust is using interfaces.
 
Please see my Solution and other solutions here:
How to copy all the items between listboxes in two forms[^].
 
—SA
  Permalink  
Comments
Nishant Sivakumar at 24-Apr-11 13:10pm
   
Good suggestion, voted 5.
SAKryukov at 24-Apr-11 13:15pm
   
Thank you, Nishant.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

This is the from class
<pre lang="cs">namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}
 
This is the other class named Class1...
 
<pre lang="midl">namespace WindowsFormsApplication
{
    class Class1
    {
    }
}
 
this is the exemple, i want access to oblect(listbox) in form, from class1 ...how?
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 24 Apr 2011
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