Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to destroy events

Status
Not open for further replies.

jamsek19

Programmer
Feb 4, 2002
35
SI
Hello.
I have a problem regarding events:
When I want to leave (for example) textbox I catching Leave event of that control and do a check if I can leave this control. In a case that control must not be leaved I fire event to set focus to this control again. The problem here is that the next control in a tabindex row got focus and then Leave event is fired again but this time from next control in a tabindex row caused by Enter method from first control.

Is it possible to somehow destroy other events in the case when I don't want to leave the text box.
Validate event is not an option!

Thanks in advance
Andreo
 
Make it a generic event handler and set it to the next textbox if the tab key is pressed.

private EventHandler resetfocusevent = new EventHandler(resetfocus);

private void resetfocus(object sender, EventArgs e)
{
((Control)sender).SetFocus();
}


then you can attach it to the textboxes individually:

private void MoveNext()
{
textbox1.LostFocus -= resetfocusevent; //remove from the previous
textbox2.LostFocus += resetfocusevent; //add to the next
}


That's a very rough idea...
 
Thanks JurkMonkey for replay.

This is very rough idea and I think is not so good.
I'm searching for something that can destroy events before got focus in next control in a row.
Well, at the moment I have implemented this by one global variable which on error in GotFocus method return focus back. Problem here is that I got many events and therefore slowing down application.

Best regards
Andreo
 
Hi Andreo,

Maybe you can check on <control>.Validating event. After the Leave event is fired, the Validating event follows if the <control>.CausesValidation property is true. From here, you will have access to the CancelEventArgs with a Cancel member, setting it to true will prevent the caret/focus from leaving the control.
Code:
void yourcontrol_Validating(object sender, CancelEventArgs e)
{
    if ( [COLOR=green]your_condition[/color] )  
    {
        [COLOR=green]// Cancel leaving, provided the CausesValidation property
        // is set to "true"[/color]
        e.Cancel = true;
    }
}
hope this helps [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top