全局Windows表单 [英] Global Windows Form

查看:88
本文介绍了全局Windows表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我希望有人可以帮助我解决我遇到的问题.我有一个我正在编写的应用程序,该应用程序具有一个计时器,该计时器在其滴答事件中会更新一个在屏幕上显示时间的标签.

直到我决定更改表格使其具有全局性为止,这一直很完美.我有一个全局变量类,其中包含了项目的所有全局变量.

因此,在clsGlobalVariable文件中,我在下面存储了对frmMain的引用:-

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();        
}



我必须缺少一些东西,因为当我尝试调试应用程序时,计时器事件永不触发.当将表单用作标准"表单时,所有工作都可以找到,但是由于我将其更改为使用全局引用,因此这停止了工作.任何人有任何想法吗?谢谢,Daniel.



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.

推荐答案

您需要订阅tick事件.通常这是在designer.cs中完成的,但是由于您使用的是自己的类,因此必须在frmMain_Load中进行.

在声明计时器的部分中查看旧的Designer生成的文件.如果没有了,请将计时器拖到任何窗体上,然后查看生成的事件代码.

另外,我不知道您所说的全球形式"是什么意思?默认情况下,所有表单都是公共的,这意味着只要定义了名称空间,就可以从任何地方访问它们,无论如何,您都必须使用新的全局名称空间进行操作

戴夫
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


这篇关于全局Windows表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆