By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,265 Members | 1,731 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,265 IT Pros & Developers. It's quick & easy.

TextBox Validating event firing twice

P: n/a
Hello to all

I'm handling the Validating event for one text box.
If something is wrong in user input I show a warning message.
The problem is that if I add [e.Cancel=true] to method the message is
displayed twice (event fired twice).
Without [e.Cancel=true] event is fired once.

if(txtMSG.Length > 0)
{
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
e.Cancel = true;
}

Can you help me ?

Thank you !
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi Dan,

Ensure that the focus is set to some other control when moving off text box
control. If the focus is not on some other control than text box but you try
closing the form, this event will be fired twice, since the form performs
the validation again.

Thanks
Mona [GrapeCity]
<da*@msdn.com> wrote in message
news:Op****************@TK2MSFTNGP10.phx.gbl...
Hello to all

I'm handling the Validating event for one text box.
If something is wrong in user input I show a warning message.
The problem is that if I add [e.Cancel=true] to method the message is
displayed twice (event fired twice).
Without [e.Cancel=true] event is fired once.

if(txtMSG.Length > 0)
{
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
e.Cancel = true;
}

Can you help me ?

Thank you !

Nov 17 '05 #2

P: n/a
Thank you for your answer !

I don't want to close the form. I want to stop the user moving from TextBox
if enter some invalid data. If I add e.Cancel=true then user can't move to
other control until enter valid data.
Moving to other control is not an option for me.

Dan

"Mona" <mo**@discussions.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl...
Hi Dan,

Ensure that the focus is set to some other control when moving off text
box control. If the focus is not on some other control than text box but
you try closing the form, this event will be fired twice, since the form
performs the validation again.

Thanks
Mona [GrapeCity]
<da*@msdn.com> wrote in message
news:Op****************@TK2MSFTNGP10.phx.gbl...
Hello to all

I'm handling the Validating event for one text box.
If something is wrong in user input I show a warning message.
The problem is that if I add [e.Cancel=true] to method the message is
displayed twice (event fired twice).
Without [e.Cancel=true] event is fired once.

if(txtMSG.Length > 0)
{
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
e.Cancel = true;
}

Can you help me ?

Thank you !


Nov 17 '05 #3

P: n/a
Well, when you change the value of e.Cancel, the Validating event is fired
again. So I think you should change the procedure as follow
if(txtMSG.Length > 0)
{
e.Cancel = true;
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
<da*@msdn.com> 写入消息新闻:Op**************@TK2MSFTNGP10.phx.gbl ...
Hello to all

I'm handling the Validating event for one text box.
If something is wrong in user input I show a warning message.
The problem is that if I add [e.Cancel=true] to method the message is
displayed twice (event fired twice).
Without [e.Cancel=true] event is fired once.

if(txtMSG.Length > 0)
{
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
e.Cancel = true;
}

Can you help me ?

Thank you !

Nov 17 '05 #4

P: n/a
Thank you for your answer !

I also tried this but event also fired twice.

Dan

"YUN SHI" <te******@citiz.net> wrote in message
news:uh**************@TK2MSFTNGP14.phx.gbl...
Well, when you change the value of e.Cancel, the Validating event is fired
again. So I think you should change the procedure as follow
if(txtMSG.Length > 0)
{
e.Cancel = true;
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
<da*@msdn.com> 写入消息新闻:Op**************@TK2MSFTNGP10.phx.gbl ...
Hello to all

I'm handling the Validating event for one text box.
If something is wrong in user input I show a warning message.
The problem is that if I add [e.Cancel=true] to method the message is
displayed twice (event fired twice).
Without [e.Cancel=true] event is fired once.

if(txtMSG.Length > 0)
{
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
e.Cancel = true;
}

Can you help me ?

Thank you !


Nov 17 '05 #5

P: n/a
SOLVED

My mistake. I manage the enter key in KeyPress event and I doubled this key
by mistake.

Thank you all !

Dan

<da*@msdn.com> wrote in message
news:Op****************@TK2MSFTNGP10.phx.gbl...
Hello to all

I'm handling the Validating event for one text box.
If something is wrong in user input I show a warning message.
The problem is that if I add [e.Cancel=true] to method the message is
displayed twice (event fired twice).
Without [e.Cancel=true] event is fired once.

if(txtMSG.Length > 0)
{
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
e.Cancel = true;
}

Can you help me ?

Thank you !


Nov 17 '05 #6

P: n/a
See this

private void txtCode_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (txtCode.Text.Length > 5)
{
e.Cancel = true;
MessageBox.Show("The Code should not be more than 5 chars!", "Warning
!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
It works pretty much well. When the text in the txtCode control be more than
5 chars, the messagebox will show only once.
"DanDanDan" <da*@msdn.com> 写入消息新闻
:uW**************@TK2MSFTNGP15.phx.gbl...
Thank you for your answer !

I also tried this but event also fired twice.

Dan

"YUN SHI" <te******@citiz.net> wrote in message
news:uh**************@TK2MSFTNGP14.phx.gbl...
Well, when you change the value of e.Cancel, the Validating event is fired again. So I think you should change the procedure as follow
if(txtMSG.Length > 0)
{
e.Cancel = true;
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
<da*@msdn.com> 写入消息新闻:Op**************@TK2MSFTNGP10.phx.gbl ...
Hello to all

I'm handling the Validating event for one text box.
If something is wrong in user input I show a warning message.
The problem is that if I add [e.Cancel=true] to method the message is
displayed twice (event fired twice).
Without [e.Cancel=true] event is fired once.

if(txtMSG.Length > 0)
{
MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
e.Cancel = true;
}

Can you help me ?

Thank you !



Nov 17 '05 #7

P: n/a
Thank you !

My mistake. I manage the enter key in KeyPress event and I doubled this key
by mistake.

Dan

"YUN SHI" <te*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
See this

private void txtCode_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (txtCode.Text.Length > 5)
{
e.Cancel = true;
MessageBox.Show("The Code should not be more than 5 chars!", "Warning
!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
It works pretty much well. When the text in the txtCode control be more
than
5 chars, the messagebox will show only once.
"DanDanDan" <da*@msdn.com> 写入消息新闻
:uW**************@TK2MSFTNGP15.phx.gbl...
Thank you for your answer !

I also tried this but event also fired twice.

Dan

"YUN SHI" <te******@citiz.net> wrote in message
news:uh**************@TK2MSFTNGP14.phx.gbl...
> Well, when you change the value of e.Cancel, the Validating event is fired > again. So I think you should change the procedure as follow
> if(txtMSG.Length > 0)
> {
> e.Cancel = true;
> MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
> MessageBoxIcon.Exclamation);
> }
>
>
> <da*@msdn.com> 写入消息新闻:Op**************@TK2MSFTNGP10.phx.gbl ...
>> Hello to all
>>
>> I'm handling the Validating event for one text box.
>> If something is wrong in user input I show a warning message.
>> The problem is that if I add [e.Cancel=true] to method the message is
>> displayed twice (event fired twice).
>> Without [e.Cancel=true] event is fired once.
>>
>> if(txtMSG.Length > 0)
>> {
>> MessageBox.Show(txtMSG, "Warning !", MessageBoxButtons.OK,
>> MessageBoxIcon.Exclamation);
>> e.Cancel = true;
>> }
>>
>> Can you help me ?
>>
>> Thank you !
>>
>>
>
>



Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.