how to determine the next control in validating event

A

Alex Bink

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
 
C

Chris, Master of All Things Insignificant

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
 
B

Bruce Wood

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..._frm/thread/5ea5d67a73414103/3a87446046433a3b

(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.
 
A

Alex Bink

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;
}
 
B

Bruce Wood

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).
 
A

Alex Bink

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...
 
B

Bruce Wood

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.
 

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