yes/no/cancel messagebox에서 취소 버튼 처리
-
12-09-2019 - |
문제
나는 a 예/아니오/메시지 상자를 취소합니다 안에 형태를 이루어 내 형태의 방법. 그리고 이제 이것은 메시지 상자 텍스트입니다. 데이터를 저장 하시겠습니까?
나는 Profesional이 아니며 사용자가 클릭하면 처리하는 방법을 모릅니다. 취소 단추? 취소 버튼을 클릭 한 결과는 양식이 열려 있어야합니다.
내 양식 폐쇄를 방지하는 방법 형태를 이루어 방법?
나는 지금까지 썼다 :;)
DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);
//...
else if (dr == DialogResult.Cancel)
{
???
}
내 코드를 완성하도록 도와주세요!
감사
해결책
Formclosing에는 부울 매개 변수가 있습니다. 부울 매개 변수는 함수가 반환 될 때 true로 설정되면 양식을 닫는 것이 취소됩니다.
편집 : 예를 들어,
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
// Set e.Cancel to Boolean true to cancel closing the form
}
다른 팁
사실 나는 당신이 이벤트 핸들러를 놓치고 있다고 생각합니다. 오, 짝수 핸들러 없이도 그 일을 할 수 없습니다. 이와 같은 이벤트 핸들러와 함께 이벤트를 추가해야합니다.
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);
}
C#에 쓰지 않았고 여기에 붙여 넣기를 복사하지 않았기 때문에이 코드에 잘못된 철자가 있으면 용서하십시오. 방금 여기에 썼습니다. :)
다음과 같은 것을 가질 수 있습니다.
if(dr == DialogResult.Cancel)
{
e.Cancel = true;
}
else if(dr == DialogResult.Yes)
{
//Save the data
}
위의 코드는 예 또는 아니오를 선택한 경우에만 양식을 닫고 예를 선택할 때 데이터를 저장해야합니다.
이 기능을 시도해야합니다
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();
}
}
당신은 이것을 시도 할 수 있습니다 :
if (MessageBox.Show("Are you sure you want to quit?", "Attention!!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == DialogResult.Yes)
{
//this block will be executed only when Yes is selected
MessageBox.Show("Data Deleted", "Done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
//this block will be executed when No/Cancel is selected
//the effect of selecting No/Cancel is same in MessageBox (particularly in this event)
}
필요한 경우 동일하게 할 수 있습니다 No
그리고 Cancel
버튼을 클릭합니다 DialogResult
수업
제휴하지 않습니다 StackOverflow