Closing a Windows Form, using the Close Window button, when a Validation Message is showing
up vote
0
down vote
favorite
I have a Windows form that has a validation event on a textBox so that if the value of that TextBox is a value that already exists it triggers a validation error.
private void txtUsername_Validating(object sender, CancelEventArgs e)
var alreadyExists = _logic.UserIdExists(txtUsername.Text.Trim());
if(alreadyExists)
errorProvider1.SetError(txtUsername, "This Userid already exists, please choose an alternative");
e.Cancel = true;
private void txtUsername_Validated(object sender, EventArgs e)
errorProvider1.SetError(txtUsername, "");
this.txtUsername.Validating += new System.ComponentModel.CancelEventHandler(this.txtUsername_Validating);
this.txtUsername.Validated += new System.EventHandler(this.txtUsername_Validated);
This results in an error image appearing next to that textBox along with a tooltip error message.
If I try and close the application, using the Close button at the top of the window, at this time I cannot as the above Event keeps firing even when I try and close the window (due to me taking focus away from the Text box).
Is there a way of closing the window, without resorting to creating an additional Close button on the form?
winforms
add a comment |
up vote
0
down vote
favorite
I have a Windows form that has a validation event on a textBox so that if the value of that TextBox is a value that already exists it triggers a validation error.
private void txtUsername_Validating(object sender, CancelEventArgs e)
var alreadyExists = _logic.UserIdExists(txtUsername.Text.Trim());
if(alreadyExists)
errorProvider1.SetError(txtUsername, "This Userid already exists, please choose an alternative");
e.Cancel = true;
private void txtUsername_Validated(object sender, EventArgs e)
errorProvider1.SetError(txtUsername, "");
this.txtUsername.Validating += new System.ComponentModel.CancelEventHandler(this.txtUsername_Validating);
this.txtUsername.Validated += new System.EventHandler(this.txtUsername_Validated);
This results in an error image appearing next to that textBox along with a tooltip error message.
If I try and close the application, using the Close button at the top of the window, at this time I cannot as the above Event keeps firing even when I try and close the window (due to me taking focus away from the Text box).
Is there a way of closing the window, without resorting to creating an additional Close button on the form?
winforms
have you tried giving the focus back to the main window after the textBox is shown?
– TheMri
Nov 8 at 15:20
@TheMri I'm not sure exactly what you mean. Can you elaborate please?
– Sperick
Nov 8 at 15:34
A simple workaround is to use abool
field (e.g.,bool IsFormClosing
), set totrue
just by theFormClosing()
handler. Set the validation handlers to execute only whenIsFormClosing
isfalse
.
– Jimi
Nov 8 at 16:32
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a Windows form that has a validation event on a textBox so that if the value of that TextBox is a value that already exists it triggers a validation error.
private void txtUsername_Validating(object sender, CancelEventArgs e)
var alreadyExists = _logic.UserIdExists(txtUsername.Text.Trim());
if(alreadyExists)
errorProvider1.SetError(txtUsername, "This Userid already exists, please choose an alternative");
e.Cancel = true;
private void txtUsername_Validated(object sender, EventArgs e)
errorProvider1.SetError(txtUsername, "");
this.txtUsername.Validating += new System.ComponentModel.CancelEventHandler(this.txtUsername_Validating);
this.txtUsername.Validated += new System.EventHandler(this.txtUsername_Validated);
This results in an error image appearing next to that textBox along with a tooltip error message.
If I try and close the application, using the Close button at the top of the window, at this time I cannot as the above Event keeps firing even when I try and close the window (due to me taking focus away from the Text box).
Is there a way of closing the window, without resorting to creating an additional Close button on the form?
winforms
I have a Windows form that has a validation event on a textBox so that if the value of that TextBox is a value that already exists it triggers a validation error.
private void txtUsername_Validating(object sender, CancelEventArgs e)
var alreadyExists = _logic.UserIdExists(txtUsername.Text.Trim());
if(alreadyExists)
errorProvider1.SetError(txtUsername, "This Userid already exists, please choose an alternative");
e.Cancel = true;
private void txtUsername_Validated(object sender, EventArgs e)
errorProvider1.SetError(txtUsername, "");
this.txtUsername.Validating += new System.ComponentModel.CancelEventHandler(this.txtUsername_Validating);
this.txtUsername.Validated += new System.EventHandler(this.txtUsername_Validated);
This results in an error image appearing next to that textBox along with a tooltip error message.
If I try and close the application, using the Close button at the top of the window, at this time I cannot as the above Event keeps firing even when I try and close the window (due to me taking focus away from the Text box).
Is there a way of closing the window, without resorting to creating an additional Close button on the form?
winforms
winforms
asked Nov 8 at 15:10
Sperick
1,51742341
1,51742341
have you tried giving the focus back to the main window after the textBox is shown?
– TheMri
Nov 8 at 15:20
@TheMri I'm not sure exactly what you mean. Can you elaborate please?
– Sperick
Nov 8 at 15:34
A simple workaround is to use abool
field (e.g.,bool IsFormClosing
), set totrue
just by theFormClosing()
handler. Set the validation handlers to execute only whenIsFormClosing
isfalse
.
– Jimi
Nov 8 at 16:32
add a comment |
have you tried giving the focus back to the main window after the textBox is shown?
– TheMri
Nov 8 at 15:20
@TheMri I'm not sure exactly what you mean. Can you elaborate please?
– Sperick
Nov 8 at 15:34
A simple workaround is to use abool
field (e.g.,bool IsFormClosing
), set totrue
just by theFormClosing()
handler. Set the validation handlers to execute only whenIsFormClosing
isfalse
.
– Jimi
Nov 8 at 16:32
have you tried giving the focus back to the main window after the textBox is shown?
– TheMri
Nov 8 at 15:20
have you tried giving the focus back to the main window after the textBox is shown?
– TheMri
Nov 8 at 15:20
@TheMri I'm not sure exactly what you mean. Can you elaborate please?
– Sperick
Nov 8 at 15:34
@TheMri I'm not sure exactly what you mean. Can you elaborate please?
– Sperick
Nov 8 at 15:34
A simple workaround is to use a
bool
field (e.g., bool IsFormClosing
), set to true
just by the FormClosing()
handler. Set the validation handlers to execute only when IsFormClosing
is false
.– Jimi
Nov 8 at 16:32
A simple workaround is to use a
bool
field (e.g., bool IsFormClosing
), set to true
just by the FormClosing()
handler. Set the validation handlers to execute only when IsFormClosing
is false
.– Jimi
Nov 8 at 16:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Based on your description, you want to maintain the default auto-validation behavior yet allow the Form to be closed using the title bar close button. I have observed that the Form.Closing event is raised in such a circumstance, however its argument Cancel property is preset to true
. A simple solution is to handle this event and set e.Cancel = false
. Implement any logic in the handler that you deem necessary.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
if (e.CloseReason == CloseReason.UserClosing) e.Cancel = false;
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Based on your description, you want to maintain the default auto-validation behavior yet allow the Form to be closed using the title bar close button. I have observed that the Form.Closing event is raised in such a circumstance, however its argument Cancel property is preset to true
. A simple solution is to handle this event and set e.Cancel = false
. Implement any logic in the handler that you deem necessary.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
if (e.CloseReason == CloseReason.UserClosing) e.Cancel = false;
add a comment |
up vote
2
down vote
accepted
Based on your description, you want to maintain the default auto-validation behavior yet allow the Form to be closed using the title bar close button. I have observed that the Form.Closing event is raised in such a circumstance, however its argument Cancel property is preset to true
. A simple solution is to handle this event and set e.Cancel = false
. Implement any logic in the handler that you deem necessary.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
if (e.CloseReason == CloseReason.UserClosing) e.Cancel = false;
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Based on your description, you want to maintain the default auto-validation behavior yet allow the Form to be closed using the title bar close button. I have observed that the Form.Closing event is raised in such a circumstance, however its argument Cancel property is preset to true
. A simple solution is to handle this event and set e.Cancel = false
. Implement any logic in the handler that you deem necessary.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
if (e.CloseReason == CloseReason.UserClosing) e.Cancel = false;
Based on your description, you want to maintain the default auto-validation behavior yet allow the Form to be closed using the title bar close button. I have observed that the Form.Closing event is raised in such a circumstance, however its argument Cancel property is preset to true
. A simple solution is to handle this event and set e.Cancel = false
. Implement any logic in the handler that you deem necessary.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
if (e.CloseReason == CloseReason.UserClosing) e.Cancel = false;
answered Nov 8 at 20:41
TnTinMn
6,22931026
6,22931026
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53210584%2fclosing-a-windows-form-using-the-close-window-button-when-a-validation-message%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
have you tried giving the focus back to the main window after the textBox is shown?
– TheMri
Nov 8 at 15:20
@TheMri I'm not sure exactly what you mean. Can you elaborate please?
– Sperick
Nov 8 at 15:34
A simple workaround is to use a
bool
field (e.g.,bool IsFormClosing
), set totrue
just by theFormClosing()
handler. Set the validation handlers to execute only whenIsFormClosing
isfalse
.– Jimi
Nov 8 at 16:32