Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi..
i want to know how to access main Form component from another class(not form...just a class)...thank you
Posted

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:

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}

public class Class1
{
  public Class1()
  {
    Form1 mainForm = (Form1)Application.OpenForms[0];
  }
}
 
Share this answer
 
v2
Comments
Kim Togo 24-Apr-11 10:28am    
My 5. Application.OpenForms is a collection of open forms owned by the application.
guendouz bachir 24-Apr-11 10:33am    
Mr Nishant Sivakumar give me an example (demo) or an article explain that
Nish Nishant 24-Apr-11 10:35am    
It's just one line. MainForm mainForm = (MainForm)Application.OpenForms[0];
guendouz bachir 24-Apr-11 10:45am    
I'll even with
Nish Nishant 24-Apr-11 10:46am    
>> I'll even with
<<
. . . . . .
What does that mean?
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
 
Share this answer
 
Comments
Nish Nishant 24-Apr-11 13:10pm    
Good suggestion, voted 5.
Sergey Alexandrovich Kryukov 24-Apr-11 13:15pm    
Thank you, Nishant.
--SA
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?
 
Share this answer
 

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