473,324 Members | 1,646 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,324 software developers and data experts.

How to determine which button was pressed in OnValidating event.

I have a custom control which has it's own OnValidating event handler. I
would like to skip the validations if the Cancel/Exit button is pressed.
Also, if possible, would like to skip the validations if the X was clicked to
close the form. How can I do this?

Thanks,
Vern
Nov 17 '05 #1
2 2519
>I have a custom control which has it's own OnValidating event handler. I
would like to skip the validations if the Cancel/Exit button is pressed.
Cancel/Exit buttons on a form should have the CausesValidation property set to "false".
If they are set to "true" for some reason and you want to ignore the validation for your control, then here's a quick solution:

private bool closing = false;
private Form form;

protected override void OnValidating(System.ComponentModel.CancelEventArgs e)
{
base.OnValidating (e);

// A dialog form's cancel button should have the "CausesValidation" property
// set to false, but this will catch that just in case it doesn't :)
if (closing || form != null && form.Modal && form.DialogResult == DialogResult.Cancel)
return;

// Todo: validate
}
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged (e);

if (form != null)
form.Closing-=new System.ComponentModel.CancelEventHandler(form_Clos ing);

if (Parent == null)
return;

form = FindForm();

if (form != null)
form.Closing+=new System.ComponentModel.CancelEventHandler(form_Clos ing);
}

private void form_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
closing = true;
}
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Vern" <Ve**@discussions.microsoft.com> wrote in message news:D3**********************************@microsof t.com...I have a custom control which has it's own OnValidating event handler. I
would like to skip the validations if the Cancel/Exit button is pressed.
Also, if possible, would like to skip the validations if the X was clicked to
close the form. How can I do this?

Thanks,
Vern

Nov 17 '05 #2
Actually, I do have the CausesValidation property set to false for the cancel
button. But it executes the overridden OnValidating routine anyway. Here's
the code that I'd like to do something to get it to ignore the validations.
This code is part of a custom auto-fill combo box control.
protected override void OnValidating(CancelEventArgs e)
{

ShowErrorIcon("");

if (_required && Text == "")
{
OnNotEntered(e);
}
else
{
if (_limitToList && Text != "")
{
int pos = FindStringExact(Text);
if (pos == -1)
OnNotInList(e);
else
this.SelectedIndex = pos;
}
}
base.OnValidating(e);
}
"Dave" wrote:
I have a custom control which has it's own OnValidating event handler. I
would like to skip the validations if the Cancel/Exit button is pressed.


Cancel/Exit buttons on a form should have the CausesValidation property set to "false".
If they are set to "true" for some reason and you want to ignore the validation for your control, then here's a quick solution:

private bool closing = false;
private Form form;

protected override void OnValidating(System.ComponentModel.CancelEventArgs e)
{
base.OnValidating (e);

// A dialog form's cancel button should have the "CausesValidation" property
// set to false, but this will catch that just in case it doesn't :)
if (closing || form != null && form.Modal && form.DialogResult == DialogResult.Cancel)
return;

// Todo: validate
}
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged (e);

if (form != null)
form.Closing-=new System.ComponentModel.CancelEventHandler(form_Clos ing);

if (Parent == null)
return;

form = FindForm();

if (form != null)
form.Closing+=new System.ComponentModel.CancelEventHandler(form_Clos ing);
}

private void form_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
closing = true;
}
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Vern" <Ve**@discussions.microsoft.com> wrote in message news:D3**********************************@microsof t.com...
I have a custom control which has it's own OnValidating event handler. I
would like to skip the validations if the Cancel/Exit button is pressed.
Also, if possible, would like to skip the validations if the X was clicked to
close the form. How can I do this?

Thanks,
Vern


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: M | last post by:
i have a form which i would like to input different "action" url depending on the button that was clicked. is there a way that javascript can prefill a defined action based on the button...
4
by: MJW | last post by:
Is there a way for me to know if or which command button was just clicked that triggers the LostFocus event for the current control on a Form? I have a form that has many types of users who each...
2
by: Jim | last post by:
Is it possible to determine where the focus is going in or before the OnValidating event has been processed? I know you can ge the window recieveing focus in the LostFocus event (WM_KILLFOCUS)...
1
by: BuddyWork | last post by:
Hello, How can I trigger the OnValidating event for a TextBox from another Form? Say Form1 has TextBox, Form1 is loaded modaless and Form2 is loaded Modaless but I want to run the TextBox...
0
by: BuddyWork | last post by:
Hello, Here is the problem, My code exists out of the TextBox.OnValidating event cleanly because the user has click on a menu that allows the user to load another screen, when that screen is...
0
by: BuddyWork | last post by:
Hello, Here is the problem, My code exists out of the TextBox.OnValidating event cleanly because the user has click on a menu that allows the user to load another screen, when that screen is...
0
by: Vern | last post by:
I have a custom control which has it's own OnValidating event handler. I would like to skip the validations if the Cancel/Exit button is pressed. Also, if possible, would like to skip the...
3
by: PJ6 | last post by:
I've added a button to a class inheriting from System.Web.UI.Control and want to handle (pass on) its click event to the page that contains it. Coming from a WinForms background, I thought this...
1
by: thewickedman | last post by:
Hi, I try to open new OpenOffice document (writer), But I am getting the exception FRM 40735:WHEN-BUTTON-PRESSED Trigger Raised unhandled Exception ORA - 305500 Please help me to resolve...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.