TextBox Validating event firing twice

G

Guest

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 !
 
M

Mona

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]
 
D

DanDanDan

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 said:
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]


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 !
 
Y

YUN SHI

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);
}
 
D

DanDanDan

Thank you for your answer !

I also tried this but event also fired twice.

Dan

YUN SHI said:
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);
}


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 !
 
D

DanDanDan

SOLVED

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

Thank you all !

Dan
 
Y

YUN SHI

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 said:
Thank you for your answer !

I also tried this but event also fired twice.

Dan

YUN SHI said:
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);
}


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 !
 
D

DanDanDan

Thank you !

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

Dan

YUN SHI said:
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 said:
Thank you for your answer !

I also tried this but event also fired twice.

Dan

YUN SHI said:
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);
}


<[email protected]> дÈëÏûÏ¢ÐÂÎÅ:OpuL5#[email protected]...
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 !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top