2010-07-02 6 views
3

C# .NET 4.0 프로그래밍은 최신 열정이며 표준 Windows.Forms 끝내기 단추 (양식의 오른쪽 위 모서리에있는 빨간색 X)에 기능을 추가하는 방법을 알고 싶습니다.Windows.Forms 종료 버튼에 기능을 추가 하시겠습니까?

버튼을 비활성화하는 방법을 찾았지만 사용자 경험을 손상시키는 것으로 생각하기 때문에 대신 일부 기능을 연결하려고합니다. 종료 버튼을 비활성화하는 방법

:

#region items to disable quit-button 
    const int MF_BYPOSITION = 0x400; 
    [DllImport("User32")] 
    private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags); 
    [DllImport("User32")] 
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); 
    [DllImport("User32")] 
    private static extern int GetMenuItemCount(IntPtr hWnd); 
    #endregion 

...

private void DatabaseEditor_Load(object sender, EventArgs e) 
    { 
     this.graphTableAdapter.Fill(this.diagramDBDataSet.Graph); 
     this.intervalTableAdapter.Fill(this.diagramDBDataSet.Interval); 

     // Disable quit-button on load 
     IntPtr hMenu = GetSystemMenu(this.Handle, false); 
     int menuItemCount = GetMenuItemCount(hMenu); 
     RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION); 
    } 

하지만 어떻게 땅에서 나는 표준 출구 버튼을 사용하여 응용 프로그램이 종료하기 전에, 방법을 첨부 할. Windows Form을 끝내기 전에 List를 XmlSerialize하고 싶습니다. 당신이 양식을 폐쇄하기 전에 코드를 작성하려는 경우

답변

4
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
    if(MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) 
    { 
     e.Cancel = true; 
    } 
} 
+0

고맙습니다 폴! 그 트릭을 했어 :) BR –

+0

레코드의 경우 이벤트는 'FormClosing'이라고하며 속성 관리자를 사용하여 메서드를 추가 할 때이 메서드는'Form1_FormClosing' 메서드를 호출합니다. 나는 왜 'Closing'이벤트가 없었으며 그것이 실제로 'FormClosing'이라는 것을 깨달을 수 없었습니다. – styfle

5

, FormClosing 이벤트를 사용

private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 

    } 
+0

고맙습니다 세르칸 :

this.Closed += new EventHandler(theWindow_Closed); 

그럼 당신은 방법을 만들! 그 트릭을했습니다 :) BR –

+0

귀하의 환영 ... –

1

내가 찾은 가장 좋은 방법은 당신이 호출 할 메소드를 호출 할 것이다 이벤트 핸들러를 만들 실제로 . 생성자에서

:

private void theWindow_Closed(object sender, System.EventArgs e) 
{ 
    //do the closing stuff 
} 
관련 문제