2014-09-03 74 views
0

我真的很感兴趣,我怎样才能得到我的变量值,如表单名称或表单文本在program.cs! 例如,我有这样的代码在我的表单如何从窗体获取值到Program.CS(例如当前窗体名称)?

 public partial class Main : Form 
    { 
     private string FormLocation = null; 
     public Main() 
     { 
      InitializeComponent(); 
      FormLocation = this.Name; //<==== Important 
     } 
} 

现在我只想Program.cs中FormLocation价值注册的所有用户活动的,我需要知道在形成用户与形式的关闭按钮退出 正如我在Program.cs中的代码是:

static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.ApplicationExit += new EventHandler(Application_ApplicationExit); 
     Application.Run(new Form1()); 
    } 
    static void Application_ApplicationExit(object sender, EventArgs e) 
    { 
     if (DialogResult.OK == MessageBox.Show("Are You Sure To Exit?", "ExitConfirmation", MessageBoxButtons.OKCancel)) 
     { 
     ActivityRegistration(UserId, FormLocation); //<===== Important 
      if (System.Windows.Forms.Application.MessageLoop) 
       { 
       System.Windows.Forms.Application.Exit(); 
       } 
      else 
       { 
       System.Environment.Exit(1); 
       } 
     } 
    } 
+0

注意,我不想使用窗体events.i想要program.cs中的唯一方法来完成所有窗体的工作...正如您在示例代码的第二部分中所看到的。我确实为所有类型的应用程序编写了一个事件 – user3823980 2014-09-03 04:45:34

+0

您不想跟踪每个表单的表单关闭事件吗? – Hassan 2014-09-03 04:46:22

+0

我的问题是EXACTLLY: 如何在PROGRAM.CS中写入一个事件来识别所有窗体,当他们进入关闭状态时FormCloseButton – user3823980 2014-09-03 04:48:49

回答

0

轨道FormClosing活动形式。

在应用程序退出每个表单关闭事件被触发。从可以访问的公开List<string>保存表格的名称。

的Program.cs

static class Program 
{ 
    static public List<string> lstClosedForms; 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.ApplicationExit += new EventHandler(Application_ApplicationExit); 
     lstClosedForms = new List<string>(); 
     Application.Run(new Form1());   
    } 

    static void Application_ApplicationExit(object sender, EventArgs e) 
    { 
      if (DialogResult.OK == MessageBox.Show("Are You Sure To Exit?", "ExitConfirmation", MessageBoxButtons.OKCancel)) 
      { 
       ActivityRegistration(UserId, lstClosedForms); // 2nd argument changed to List<string> 

       if (System.Windows.Forms.Application.MessageLoop)    
         System.Windows.Forms.Application.Exit();    
       else    
         System.Environment.Exit(1);    
      } 
    } 
}  

Form1中

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); 
    } 

    void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     Program.lstClosedForms.Add(this.Name); 
    } 
} 

注意:修改只要创建新表列表。

+0

请给我一些示例代码,我不明白如何做到这一点 顺便说一句。我不想使用FormClosinng事件... 注意:我希望Program.cs中的一种方法可以为所有表单执行作业自动化 – user3823980 2014-09-03 04:42:30

+0

在这种情况下,将更改ActivityRegistration方法。使用自定义类来跟踪每个表单的信息。 – Hassan 2014-09-03 04:44:46

+0

static public List lstClosedForms; 为什么要声明一个List?为什么不使用一个字符串? – user3823980 2014-09-03 05:04:32

相关问题