Kishore A
Kishore A

Reputation: 1385

Raise LostFocus event on a control manually

I have a bunch of controls (textbox and combobox) on a form with toolstripcontainer and toolstripbuttons for save, cancel etc for edits. We are using .Net 3.5 SP1
There is bunch of logic written in control.lostfocus and control.leave events. These events are not being called when clicked on the toolstrip buttons. Is there a way to call these events manually when any of these buttons are pressed.

Thanks.
Kishore

[Edit]

This is how I solved the problem. Thanks Chris Marasti-Georg for the pointer. In the button click event I calling focus on the toolstrip instead of the button as the toolstripbutton does not have a focus event. We can access the toolstrip on which the button is placed using

((ToolStripButton)sender).Owner.Focus()

-Kishore

Upvotes: 1

Views: 7082

Answers (3)

configurator
configurator

Reputation: 41620

I'd suggest moving the login to a method outside the event handler and calling that method...

Upvotes: 1

Chris Marasti-Georg
Chris Marasti-Georg

Reputation: 34650

You could listen to the click events on the buttons, and in the handler call their focus method. That would (hopefully) cause the previously focused control to respond correctly. Add the following handler to each button's click event:

private void ButtonClick(object sender, EventArgs e) {
    if(sender != null) {
        sender.Focus();
    }
}

Upvotes: 4

user1228
user1228

Reputation:

You can extend those controls and then call the OnLostFocus and OnLeave protected methods of the base class...

Upvotes: 1

Related Questions