Click here to Skip to main content
15,904,351 members
Home / Discussions / C#
   

C#

 
AnswerRe: Question: how to access main Form component from another class ? Pin
EliottA10-Jan-09 20:55
EliottA10-Jan-09 20:55 
GeneralRe: Question: how to access main Form component from another class ? Pin
Mohammemd11-Jan-09 2:42
Mohammemd11-Jan-09 2:42 
AnswerRe: Question: how to access main Form component from another class ? Pin
ananthvivek10-Jan-09 21:11
ananthvivek10-Jan-09 21:11 
GeneralRe: Question: how to access main Form component from another class ? Pin
Mohammemd11-Jan-09 2:45
Mohammemd11-Jan-09 2:45 
GeneralRe: Question: how to access main Form component from another class ? Pin
Christian Graus11-Jan-09 4:57
protectorChristian Graus11-Jan-09 4:57 
GeneralRe: Question: how to access main Form component from another class ? Pin
Mohammemd11-Jan-09 8:05
Mohammemd11-Jan-09 8:05 
GeneralRe: Question: how to access main Form component from another class ? Pin
Christian Graus11-Jan-09 4:56
protectorChristian Graus11-Jan-09 4:56 
AnswerRe: Question: how to access main Form component from another class ? Pin
Wendelius10-Jan-09 21:43
mentorWendelius10-Jan-09 21:43 
It's not necessarily a good idea to directly update form's controls from several places. However, there are several approaches to this. Since this is your main form, I take it there can be only 1 instance at all time.

One possibility is to make it a singleton. In that case you would always have access to the form itself. The form would look like:
public partial class MainForm : Form {
private static MainForm _theForm;

private MainForm() {
   InitializeComponent();
   _theForm = this;
}
public static MainForm Instance {
   get {
      if (_theForm == null) {
         _theForm = new MainForm();
      }
      return _theForm;
   }
}
...


Now when you start the application instead of creating the form you refer to it's Instance:
Application.Run(MainForm.Instance);


When you want to access it's properties or methods you always use Instance. For example if you want to get Height, you would have:
System.Windows.Forms.MessageBox.Show(MainForm.Instance.Height.TowString());


So now you can write your own properties on the main form and access them via Instance.

Hope this helps.

The need to optimize rises from a bad design.My articles[^]

AnswerRe: Question: how to access main Form component from another class ? Pin
Mohammemd11-Jan-09 3:03
Mohammemd11-Jan-09 3:03 
GeneralRe: Question: how to access main Form component from another class ? Pin
Wendelius11-Jan-09 3:49
mentorWendelius11-Jan-09 3:49 
AnswerRe: Question: how to access main Form component from another class ? Pin
DaveyM6910-Jan-09 23:47
professionalDaveyM6910-Jan-09 23:47 
GeneralRe: Question: how to access main Form component from another class ? Pin
Mohammemd11-Jan-09 7:40
Mohammemd11-Jan-09 7:40 
AnswerRe: Question: how to access main Form component from another class ? Pin
DaveyM6911-Jan-09 0:13
professionalDaveyM6911-Jan-09 0:13 
GeneralRe: Question: how to access main Form component from another class ? Pin
Mohammemd11-Jan-09 7:42
Mohammemd11-Jan-09 7:42 
AnswerRe: Question: how to access main Form component from another class ? Pin
DaveyM6911-Jan-09 0:47
professionalDaveyM6911-Jan-09 0:47 
GeneralRe: Question: how to access main Form component from another class ? Pin
Mohammemd11-Jan-09 7:47
Mohammemd11-Jan-09 7:47 
GeneralRe: Question: how to access main Form component from another class ? Pin
DaveyM6911-Jan-09 8:29
professionalDaveyM6911-Jan-09 8:29 
GeneralRe: Question: how to access main Form component from another class ? Pin
Wendelius11-Jan-09 12:01
mentorWendelius11-Jan-09 12:01 
GeneralRe: Question: how to access main Form component from another class ? Pin
Mohammemd11-Jan-09 18:32
Mohammemd11-Jan-09 18:32 
AnswerRe: Question: how to access main Form component from another class ? Pin
N a v a n e e t h11-Jan-09 21:45
N a v a n e e t h11-Jan-09 21:45 
QuestionHow should I design my project to support future plug-ins? Pin
Artmansoft10-Jan-09 19:47
Artmansoft10-Jan-09 19:47 
AnswerRe: How should I design my project to support future plug-ins? Pin
wjp_auhtm11-Jan-09 1:14
wjp_auhtm11-Jan-09 1:14 
GeneralRe: How should I design my project to support future plug-ins? Pin
Artmansoft11-Jan-09 1:50
Artmansoft11-Jan-09 1:50 
AnswerRe: How should I design my project to support future plug-ins? Pin
wjp_auhtm11-Jan-09 5:05
wjp_auhtm11-Jan-09 5:05 
GeneralRe: How should I design my project to support future plug-ins? Pin
Artmansoft11-Jan-09 18:17
Artmansoft11-Jan-09 18:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.