2009-08-09 1 views
3

에 메시지 박스를 취소 나는 내 양식의 FormClosing 방법에 예/아니오/메시지 박스을 취소했습니다. 이제 메시지 상자 텍스트입니다. 데이터를 저장 하시겠습니까?처리 예에서 취소 버튼/아니오/FormClosing 방법

나는 사용자가 클릭하면 처리 방법을 모르겠다. 취소 버튼을 취소 하시겠습니까? 취소 버튼을 클릭 한 결과는 반드시 양식이 열려 있어야합니다.
내 양식 닫는 것을 막는 방법 FormClosed 방법?

내가 지금까지 쓴 :)

DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning); 

//... 
else if (dr == DialogResult.Cancel) 
{ 
    ??? 
} 

이 내 코드를 완료하는 데 도와주세요!
감사합니다

답변

11

FormClosing에는 함수가 반환 될 때 True로 설정되면 IIRC 양식을 닫는 취소됩니다 부울 매개 변수가 있습니다.

EDIT : 예를 들어,

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) { 
    // Set e.Cancel to Boolean true to cancel closing the form 
} 

See here.

if(dr == DialogResult.Cancel) 
{ 
    e.Cancel = true; 
} 
else if(dr == DialogResult.Yes) 
{ 
    //Save the data 
} 

은 위의 코드는 아니오 예를 선택하는 경우 양식을 닫거나해야하며, 예를 선택하면 데이터를 저장합니다

+0

감사합니다. – mahdiahmadirad

6

는 다음과 같은 것을 가질 수 있습니다.

6

사실 나는 당신이 없다고 생각합니다. 이벤트 처리기, 오 이런 처리기가 없어도 그걸로 돌릴 수는 없어요. 다음과 같은 이벤트 핸들러가있는 이벤트를 추가해야합니다. 이 코드의 일부 잘못 철자가있는 경우 내가 C#에서 그것을 작성하고 여기에 붙여 복사하지 않았기 때문에

private void myform_Closing(object sender, FormClosingEventArgs e) 
{ 
    DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning) 

    if (dr == DialogResult.Cancel) 
    { 
     e.Cancel = true; 
     return; 
    } 
    else if (dr == DialogResult.Yes) 
    { 
     //TODO: Save 
    } 
} 

//now add a default constructor 
public myform() // here use your form name. 
{ 
    this.FormClosing += new FormClosingEventHandler(myform_Closing); 
} 

은 용서. 방금 여기에 썼습니다. :)

0

이 기능

public DialogResult msgClose(string msg) 
{ 
    return MessageBox.Show(msg, "Close", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); 
} 

과 같이 사용을 시도해야합니다.

private void frm_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    if (conn.msgClose("Application close?") == DialogResult.No) 
     e.Cancel = true; 
    else 
    { 
     this.Close(); 
    } 
}