Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Windows Form

Status
Not open for further replies.

danielkelly

IS-IT--Management
Aug 13, 2006
28
AU
Hi All,

I hoping someone can assist me with the problem I am having. I have an application which Im writing that has a timer which on its tick event updates a label which displays the time on the screen. This worked perfectly until I decided to change the form to make it global.

I have a Global Variable Class which houses all the global variables for the project.

So in the clsGlobalVariable file I store a reference to the frmMain so below :-

//Global Main Parent Form
private static frmMain g_MainForm = new frmMain();

public static frmMain GlobalMainForm
{
get { return g_MainForm; }
set { g_MainForm = value; }
}

The call to show the main form appears after the login screen with the following call :-

//Initialise The Form Labels
clsGlobalVariables.GlobalMainForm.lblUserName.Text = "User : " + clsGlobalVariables.GlobalUserName;
clsGlobalVariables.GlobalMainForm.lblTerminalID.Text = "Terminal : " + clsGlobalVariables.GlobalTerminalID;

clsGlobalVariables.GlobalMainForm.ShowDialog();

the timer based code is as below :-

private void frmMain_Load(object sender, EventArgs e)
{
//Set the Date and Time on the Form
clsGlobalVariables.GlobalMainForm.lblDate.Text = DateTime.Now.ToLongDateString();
clsGlobalVariables.GlobalMainForm.lblTime.Text = DateTime.Now.ToLongTimeString();
}

public void tmrClock_Tick(object sender, EventArgs e)
{
//Set the Date and Time on the Form
clsGlobalVariables.GlobalMainForm.lblDate.Text = DateTime.Now.ToLongDateString();
clsGlobalVariables.GlobalMainForm.lblTime.Text = DateTime.Now.ToLongTimeString();
}

I must be missing something as when I try to debug the application the timer event is nevering firing. Everything worked find when the form was being used as a "standard" form but since I changed it to using teh global reference, this stopped working.

Any1 have any ideas?

Thanks,

Daniel.

 
I would avoid globals all together. Why do you need global variables?

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I need to have access to my Main Form from all other forms. This is because of the design and layout of the application.

For forms that open directly from the Main Form I can pass an instance of the main form in as a constructor but what about sub-forms of those forms?

I thought global variables would be ideal for this instance.

Thanks.
 
sub forms still need originate from the main form. how else are they opened? Something else to consider is why you need to pass the form around so much? I recommend researching the concept of an Event Broker to manage complex UI interactions. I spiked a simple example using WinForms and Castle Windsor. It's hosted on Google Code if your interested

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top