Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :-

C#
//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 :-

C#
//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 :-

C#
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.
Posted
Updated 12-Oct-10 21:50pm
v4
Comments
DaveyM69 13-Oct-10 3:51am    
Where are you creating the timer?
Where are you starting the timer?
Member 3681443 13-Oct-10 4:54am    
I created the time by dragging it onto the Form and setting its Enabled state to true.

Like I said when the form was a standard form it worked perfectly so i assume Im missing the fundamental understanding of the "global" instance of the form.
Member 3681443 13-Oct-10 5:48am    
Thanks for the advice Dave but I still am having issues. Let me go back a step and outline in some more detail :-

So my Main Form is frmMain (FORM1) and it consists of a few panels. The left hand panel is a series of buttons making up a menu and the right hand side is where the content is placed.

An example of what I am doing is i have another form which I place inside the panel control giving the look and feel of a website with a menu on the left, titla bar at the top and content below.

This second form ("FORM2" inside the panel) had an option to show a new form (FORM3 showdialog) and when I hit a button on that form, I want to close the form (FORM3) and add another form to the main forms content panel (FORM1) effectively replacing FORM2 with another form.

The form FORM3 (showdialog form) does not know about the main form and hence cant add another form to its content panel. By creating a "Global" instance of the frmMain (FORM1) I can then access a method on the form from FORM3 to add to the panel.

clsGlobalVariables.GlobalMainForm.panelMain.Controls.Clear();
frmReturningMember formReturningMember = new frmReturningMember();
formReturningMember.TopLevel = false;
formReturningMember.Dock = DockStyle.Fill;
formReturningMember.FormBorderStyle = FormBorderStyle.None;
clsGlobalVariables.GlobalMainForm.panelMain.Controls.Add(formReturningMember);
formReturningMember.Show();

This is what I am trying to achieve.

Maybe there is a much better way to do this.

I did try adding the following code to the Load Event of the frmMain but it steill didnt work :-

this.tmrClock.Enabled = true;
this.tmrClock.Interval = 1000;
this.tmrClock.Tick += new System.EventHandler(this.tmrClock_Tick);

The Tick event still is never reached.

Thanks,

Daniel.
Toli Cuturicu 13-Oct-10 10:37am    
Never prefix your classes with cls or anything.
Class names always start with an uppercase letter.
Don't use tmr, frm and so on. Have formMain as an instance of FormMain and timerClock as an instance of Timer.
C# does not use any form of hungarian notation. Never.

1 solution

You need to subscribe to the tick event. This is normally done in the designer.cs but since you are using your own class you will have to do it in the frmMain_Load.

Have a look at the old designer generated file for in the section where the timer is declared. If you don't have it anymore, drag a timer onto any form and look at the event code that is generated.

Also, I don't know what you mean by "Global form"? all forms are public by default which means they can be accessed from anywhere as long as the namespace is defined, which you will have to do anyway using your new global namespace

Dave
 
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