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

how to determine the next control in validating event

P: n/a
Hi,

I have a validating event on a textbox in which I want to prevent the user
to leave the textbox without entering the right data. Only if he clicks on
another specific control he is allowed to leave the textbox without entering
the right information. Is there a way to determine which other control was
clicked in the validating event of the textbox?

Thanks
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I don't know of a way to do this easily, since the validating event happens
before then new control gets focus.

This is just a brainstorming idea, you'd have to play with it to see if it
would work. Capture the mousedown event, then see if the click is in the
location of one of the controls your allowing it to move to. I think (but
could be wrong) the mousedown will happen before your validate.

Sorry if this doesn't work, it was just an idea.
Chris

"Alex Bink" <no****@hotmail.com> wrote in message
news:w7*******************@amsnews05.chello.com...
Hi,

I have a validating event on a textbox in which I want to prevent the user
to leave the textbox without entering the right data. Only if he clicks on
another specific control he is allowed to leave the textbox without
entering the right information. Is there a way to determine which other
control was clicked in the validating event of the textbox?

Thanks

Nov 16 '05 #2

P: n/a
You want to set CausesValidation on that other, specific control to
false. This will allow that control to gain focus without firing the
Validating event on the text box that the user is leaving. Sort of.

You can read about all the gory details of CausesValidation and
validating in this thread:

http://groups-beta.google.com/group/...87446046433a3b

(watch for line wrap on the URL)

The bottom line is that CausesValidation will (with a few caveats)
solve your problem by preventing your Validating event handler from
being called (sort of... like I said, read the other link), so you
don't have to test for which control has focus in your Validating event
handler.

Nov 16 '05 #3

P: n/a
Thanks,

That helped. It was a little more difficult to understand than I hoped, but
my program works now the way I want it to.
If you're interested:

I have 2 textboxes. Moving focus from textbox 1 to textbox 2 or from textbox
2 to textbox 1 should not fire a validating event. But moving out of textbox
1 or textbox 2 to another control should fire the validating event.
This is the code that works for me:

private void textBox1_Enter(object sender, System.EventArgs e)
{
textBox1.CausesValidation = true;
textBox2.CausesValidation = false;
}
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Validating text 1");
TextBox2.CausesValidation = true;
}

private void textBox2_Enter(object sender, System.EventArgs e)
{
textBox2.CausesValidation = true;
textBox1.CausesValidation = false;
}

private void textBox2_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Validating text 2");
textBox1.CausesValidation = true;
}

"Bruce Wood" <br*******@canada.com> schreef in bericht
news:11**********************@f14g2000cwb.googlegr oups.com...
You want to set CausesValidation on that other, specific control to
false. This will allow that control to gain focus without firing the
Validating event on the text box that the user is leaving. Sort of.

You can read about all the gory details of CausesValidation and
validating in this thread:

http://groups-beta.google.com/group/...87446046433a3b

(watch for line wrap on the URL)

The bottom line is that CausesValidation will (with a few caveats)
solve your problem by preventing your Validating event handler from
being called (sort of... like I said, read the other link), so you
don't have to test for which control has focus in your Validating event
handler.

Nov 16 '05 #4

P: n/a
Hmmm.... I see what you're doing, but I have two questions.

1. Is the setting of CausesValidation on the "other" text box in the
Validating handler really necessary? I would have though that the lines
in the Entering handlers would have been sufficient.

2. Doesn't it work to simply set CausesValidation to "false" on both
text boxes and then set it to "true" for all other controls, and not
manipulate it at all in the event handlers? In theory this should cause
the behaviour you describe: you can move freely between the text boxes,
but trying to send focus to any other control will cause both text
boxes' Validating events to fire (assuming that you've visited both).

Nov 16 '05 #5

P: n/a
You are right in your first question. Setting the CausesValidation of the
other textbox is not necessary for the logic between the two textboxes. It's
just that all controls have CausesValidation by default set to true.
Entering textbox1 will set the CausesValidation property of textbox 2 to
false. So I think it's safer that after actually leaving textbox1 all
properties of the other textbox are back to the state they were in.
If for example there is yet another control that needs validation, the
CausesValidation properties of both textboxes have to be set to true. This
way I can be certain that they are...

About your second question: I think I tried that. But having the
CausesValidation set to false will also prevent the validation event to be
fired for the control itself. In this thread you were referring to, you
stated that the event is postponed until focus moves to a control that has
CausesValidation set to true. I'm not able to reproduce that behaviour (in
an experiment with 3 textboxes). So for now, I'm happy that the code is
working... (and hopefully it will keep working when I add more panels and
other controls that may need validation ;-)

Regards...

"Bruce Wood" <br*******@canada.com> schreef in bericht
news:11**********************@z14g2000cwz.googlegr oups.com...
Hmmm.... I see what you're doing, but I have two questions.

1. Is the setting of CausesValidation on the "other" text box in the
Validating handler really necessary? I would have though that the lines
in the Entering handlers would have been sufficient.

2. Doesn't it work to simply set CausesValidation to "false" on both
text boxes and then set it to "true" for all other controls, and not
manipulate it at all in the event handlers? In theory this should cause
the behaviour you describe: you can move freely between the text boxes,
but trying to send focus to any other control will cause both text
boxes' Validating events to fire (assuming that you've visited both).

Nov 16 '05 #6

P: n/a
I just repeated your experiment. You are absolutely right: the two text
boxes with CausesValidation set to False never validate. That is
freaky. It goes against everything I've read about validation, but then
I've never tried setting CausesValidation to false on a text box
before, just on buttons. Weird.

There are some aspects of .NET that aren't quite "there". Data binding,
tab pages and tab controls, ... add validation to that list.

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.