Skip to main content
 首页 » 编程设计

c#之部分打开c#窗体看不懂

2024年02月05日9java哥

我是 c# WinForms 的新手。我有 2 个问题。这段代码基本上告诉我们打开表单的部分。

第一个问题 form1_Activated 或 Form1_Show 或 button_Click 我的意思是所有这些都具有相同的形式“_”和方法名称的固定名称。它是否只是一种样式的语法。

我的第二个问题是程序如何知道应该先执行哪一个。我的意思是代码行是 activated-bindingContextChanged-Load... 但在输出中它是不同的。那是如何发生的,哪些代码使这成为可能

     private void Form1_Activated(object sender, EventArgs e) 
     { 
         Debug.Print("Activated"); 
     } 
     private void Form1_BindingContextChanged(object sender, EventArgs e) 
     { 
         Debug.Print("BindingContextChanged"); 
     } 
     private void Form1_Load(object sender, EventArgs e) 
     { 
         Debug.Print("Load Olayı"); 
     } 
     private void Form1_Paint(object sender, PaintEventArgs e) 
     { 
         Debug.Print("Paint Olayı"); 
     } 
     private void Form1_Shown(object sender, EventArgs e) 
     { 
         Debug.Print("Shown Olayı"); 
     } 
     private void Form1_VisibleChanged(object sender, EventArgs e) 
     { 
         Debug.Print("VisibleChanged"); 
     } 
     private void Form1_Validated(object sender, EventArgs e) 
     { 
         Debug.Print("Validated"); 
     }        
   }  
} 

请您参考如下方法:

事件按照 Microsoft 设计的顺序发生:

通过https://msdn.microsoft.com/en-us/library/86faxx0d.aspx :

The Form and Control classes expose a set of events related to application startup and shutdown. When a Windows Forms application starts, the startup events of the main form are raised in the following order:

  • Control.HandleCreated
  • Control.BindingContextChanged
  • Form.Load
  • Control.VisibleChanged
  • Form.Activated
  • Form.Shown

When an application closes, the shutdown events of the main form are raised in the following order:

  • Form.Closing
  • Form.FormClosing
  • Form.Closed
  • Form.FormClosed
  • Form.Deactivate

The ApplicationExit event of the Application class is raised after the shutdown events of the main form.

方法命名 FormName_Event 也是设计使然的(至少在 IDE 中是这样)。您看到的是表单设计器生成的默认事件处理程序名称。如果您愿意,可以覆盖这些。