버튼의 클릭 이벤트 핸들러에서 대화 상자가 닫히지 않도록 방지
와 함께 표시하는 대화 상자가 <class>.ShowDialog()있습니다. 확인 버튼과 취소 버튼이 있습니다. OK 버튼에는 이벤트 핸들러도 있습니다.
이벤트 처리기에서 일부 입력 유효성 검사를 수행하고 실패하면 메시지 상자로 사용자에게 알리고 대화 상자가 닫히지 않도록합니다. 마지막 부분 (종료 방지)을 수행하는 방법을 모르겠습니다.
팝업 오류 대화 상자를 원한다고 지정했다면이 를 수행하는 한 가지 방법은 유효성 검사를OnClosing이벤트 핸들러로 이동하는 것입니다. 이 예에서 사용자가 대화 상자의 질문에 예라고 대답하면 양식 닫기가 중단됩니다.
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}
설정 e.Cancel = true하면 양식이 닫히지 않습니다.
그러나 유효성 검사 오류를 인라인으로 표시하고 (어떤 방식 으로든 문제가되는 필드 강조 표시, 도구 설명 표시 등) 사용자가 처음에 확인 단추를 선택하지 못하도록 하는 것이 더 나은 설계 / 사용자 환경 입니다.
당신은 폼의 설정에 의해 폐쇄을 취소 할 수 있습니다 DialogResult로 DialogResult.None.
button1이 AcceptButton 인 예 :
private void button1_Click(object sender, EventArgs e) {
if (!validate())
this.DialogResult = DialogResult.None;
}
사용자가 button1을 클릭하고 validate 메소드가 false를 리턴하면 양식이 닫히지 않습니다.
이를 위해 FormClosing 이벤트를 사용하지 마십시오. 사용자가 취소 또는 X를 클릭하여 대화 상자를 닫을 수 있도록합니다. 확인 단추의 Click 이벤트 처리기를 구현하고 만족할 때까지 닫지 마십시오.
private void btnOk_Click(object sender, EventArgs e) {
if (ValidateControls())
this.DialogResult = DialogResult.OK;
}
여기서 "ValidateControls"는 유효성 검사 논리입니다. 문제가 있으면 false를 반환합니다.
이것은 귀하의 질문에 직접 답변하지 않지만 (다른 사람은 이미 있음) 사용성 관점에서 입력이 유효하지 않은 동안 문제가되는 버튼을 비활성화하는 것이 좋습니다.
이 코드를 사용하십시오.
private void btnOk_Click(object sender, EventArgs e) {
if (ValidateControls())
this.DialogResult = DialogResult.OK;
}
문제는 사용자가 양식을 닫기 위해 버튼을 두 번 클릭해야한다는 것입니다.
FormClosing을 잡으면 양식이 열린 상태로 유지됩니다. 이를 위해 이벤트 인수 객체의 Cancel 속성을 사용하십시오.
e.Cancel = true;
양식이 닫히지 않게됩니다.
더 나은 예를 찾을 시간이 있었으면 좋겠지 만 기존의 Windows 양식 유효성 검사 기술을 사용하는 것이 훨씬 낫습니다.
http://msdn.microsoft.com/en-us/library/ms229603.aspx
이벤트 함수에 한 줄만 추가하면됩니다.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
this->DialogResult = System::Windows::Forms::DialogResult::None;
}
void SaveInfo()
{
blnCanCloseForm = false;
Vosol[] vs = getAdd2DBVosol();
if (DGError.RowCount > 0)
return;
Thread myThread = new Thread(() =>
{
this.Invoke((MethodInvoker)delegate {
picLoad.Visible = true;
lblProcces.Text = "Saving ...";
});
int intError = setAdd2DBVsosol(vs);
Action action = (() =>
{
if (intError > 0)
{
objVosolError = objVosolError.Where(c => c != null).ToArray();
DGError.DataSource = objVosolError;// dtErrorDup.DefaultView;
DGError.Refresh();
DGError.Show();
lblMSG.Text = "Check Errors...";
}
else
{
MessageBox.Show("Saved All Records...");
blnCanCloseForm = true;
this.DialogResult = DialogResult.OK;
this.Close();
}
});
this.Invoke((MethodInvoker)delegate {
picLoad.Visible = false;
lblProcces.Text = "";
});
this.BeginInvoke(action);
});
myThread.Start();
}
void frmExcellImportInfo_FormClosing(object s, FormClosingEventArgs e)
{
if (!blnCanCloseForm)
e.Cancel = true;
}
You can probably check the form before the users hits the OK button. If that's not an option, then open a message box saying something is wrong and re-open the form with the previous state.
'IT TIP' 카테고리의 다른 글
| 미디어 유형 = application / json에 대한 MessageBodyWriter를 찾을 수 없습니다. (0) | 2020.11.25 |
|---|---|
| Django가 관리자에게 이메일을 보내지 않음 (0) | 2020.11.25 |
| SQLSERVER의 ListAGG (0) | 2020.11.25 |
| Spring Boot에 보조 서블릿을 등록하려면 어떻게해야합니까? (0) | 2020.11.25 |
| "in 절"내의 MySQL 항목 수 (0) | 2020.11.25 |