473,320 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to determine the next control in validating event

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
6 5737
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
5
by: Dennis C. Drumm | last post by:
Is there a way to prevent the user from leaving a tab on a TabControl until all the controls on that tab have finished validating? The problem I am experiencing is when a control fails its...
3
by: Denise | last post by:
I want to fire code when the user changes the value of a textbox. I only want it to fire when the user is finished eding the TextBox and leaves (not after each keystroke). I am using the Validating...
3
by: jaYPee | last post by:
is there a way to know if the form is edited or not after calling addnew? Me.BindingContext(DsStudentCourse1, "Students").AddNew() because i got an error after closing the form. because in my...
10
by: Dennis D. | last post by:
Hello: There are a series of textboxes (x.text, y.text, z.text etc.) in which user input is expected. As: dtmX=CDate(x.text) dtmY=CDate(y.text) dtmZ=CDate(z.text) where x, y, and z.text...
2
by: bretth | last post by:
In a VB.Net Windows Forms application, I have a user control that handles mouse events. Another section of code programmatically adds a label to the control. I would like label to ignore all...
3
by: Brian Tkatch | last post by:
I have a form with two DataGrids, which are kept in sync manually via Stored PROCEDURE calls. That is, when a record is selected on the first grid, a stored PROCEDURE is CALLed to Fill() the next...
0
nev
by: nev | last post by:
How can I determine the next control the user clicks on a certain control's validating event? For example, if I have textbox1 with the focus, and I click textbox2, how can I know from textbox1 that...
2
by: Peted | last post by:
Hi if i derive a reference to a control on a winform (ie Control activeControl = somecontrol on the form) how can i test if that control has a validating or validated event and more importantly...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.