Why do some events just appear in the code? - c#

I noticed something weird , for the textbox there are some events that just appear in the code, but in the design(when I open properties , and click on the events tab) these events are not visible !
for example :
textbox1.LostFocus += textbox1.LostFocus;
or the GotFocus event .
Anybody has an idea regard this ??
thanx in advance

it is because some events not Browsable
if you check the documentation you will see [BrowsableAttribute(false)] for the events like GotFocus
if BrowsableAttribute false then that event not displayed in a Properties window.
Note The GotFocus and LostFocus events are low-level focus events
that are tied to the WM_KILLFOCUS and WM_SETFOCUS Windows messages.
Typically, the GotFocus and LostFocus events are only used when
updating UICues or when writing custom controls. Instead the Enter and
Leave events should be used for all controls except the Form class,
which uses the Activated and Deactivate events.

In which method / function do you register (+= ) that events? Only the events registered in the InitializeComponent (DesignerCode) / constructor code, are considered by the Designer.
Edit: In that case, Damith is right with Browsable(false).
My answer would be valid if you see the event in the Designer, but not the linked event handler method.

Related

WinForm Hosted ComboBox Leave Event does not fire within WPF

I added the the Both from Code behind with cb.Leave += new EventHandler(cb_Leave); or design by Leave="cb_Leave" but none of them worked. is it a bug or what?
Examined LostFocus event instead and this one also has malfunction and does not fire correctly every time.
I also added KeyPress event to my hosted control but it is working correctly.
using LostFocus Event for its parent WindowsFormsHost satisfied my wish.

Form events not fired when other control is in place

I have an application that contains a form with multiple controls.
I have subscribed to the form mouse up event. However when I click on the form if thewre is an other control placed on the form the event is not fired.
So, I would like to capture an form event on the form (even when an control is in place). Is this possible?
Thanks in advance.
As far as i know windows forms doesn't implement the concept of event bubbling. So you should manually tweak controls to handle the event. You can do it manually looping through all controls, or you can create some kind of wrapper for your form/container to subscribe to the event automatically. You may check general implementation of this idea here. .

Keydown event not firing in user control obj

I have a user control which is not declared in the designer. I have a button that I want to have create this user control when I click it - it should initialize the user control and insert it in the main UI.
However, it happens that the user control has a key press event on it, which is not firing.
Why does this happen?
I already tried to attach the event on the user control itself but it seems that it's not firing at all. Is there some kind of bug?
It is very hard to fix problems with code that you can't see, but in WPF, there are often reasons why Bubbling events like the KeyDown event don't fire. Occasionally certain controls may make use of these events and set them as handled internally, thereby stopping them from bubbling up any further.
The normal solution on these occasions is to use the related Tunneling events instead, which are raised before the Bubbling methods and not used internally by controls. So, while I can't guarantee that this will fix your problem, it is certainly worth trying to handle the UIElement.PreviewKeyUp event instead of the UIElement.KeyUp event.

C#: custom control calls click handler every other click (winform, not ASP)

I have written a custom control in C# (inherited from Forms.Control) and it seems to working fine, but if you press the button fast enough a problem occurs: only every other click will call the click event handler. This doesn't happen if you don't press it fast (less than once a second). The mouseUp and mouseDown handlers always get called no matter how fast you click the button.
Of course doesn't happen with the canned winform button.
I cannot use the canned button because I'm writing an application for the .net compact framework, so I need a custom control in order to make the UI more presentable. Also, I tested out my code on the full version of the .net framework, and I still have the same problem.
Any help would be greatly appreciated. Thank you!
If you are clicking rapidly enough, you are getting into DoubleClick territory.
According to above MSDN Page the order of events are:
The following series of events is raised by the control when such a user action takes place:
MouseDown event.
Click event.
MouseClick event.
MouseUp event.
MouseDown event.
DoubleClick event.
MouseDoubleClick event.
MouseUp event
If you will notice there is only one Click event per DoubleClick
For a way to disable it try looking at this MSDN Page discussing ControlStyles.
From above link:
StandardClick -- If true, the control implements the standard Click behavior.
StandardDoubleClick -- If true, the control implements the standard DoubleClick behavior. This style is ignored if the StandardClick bit is not set to true.
So try this in your controls constructor or load event:
this.SetStyle(ControlStyles.StandardClick, true );
this.SetStyle(ControlStyles.StandardDoubleClick, false);
Since SetStyle does not appear to be in the Compact Framework you could add a DoublClick Event and have it trigger the Click event Programmically like this.
YourClickEvent(sender, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left,1,0,0,0));
When you click your control fast enough, it calls double click rather than click.
So, you should do something like this in your click function:
{
control.Enabled = false;
......
control.Enabled = true;
}

OnLostFocus is called after dispose is called in a dataGridView

I Have a control inheriting the dataGridView control.
I have the onLostFocus method overrided. Lately I encountereda weird behavior. if trying to close the form while a cell is in teh middle of being edited. the dispose method will be called and then teh onLostFocus is called that results in a nullReferenceException
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
base.DefaultCellStyle = myStyle1;
}
}
my question is how come the lostFocus is called after the userControl starts being disposed?
and what is the correct way to handle this isuue?
A workaround can be to check explicitly if dispose had started and then return from the OnLostFocus. But I'd rather understans better what happens behind.
Thanks!
According to http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx, Microsoft suggested that OnEnter and OnLeave should be used instead of OnGotFocus and OnLostFocus.
The GotFocus and LostFocus events are
low-level focus events that are tied
to the WM_KILLFOCUS and WM_SETFOCUS
Windows messages. Typically, the
GotFocus and LostFocus events are only
used when updating UICues or when
writing custom controls. Instead the
Enter and Leave events should be used
for all controls except the Form
class, which uses the Activated and
Deactivate events. For more
information about the GotFocus and
LostFocus events, see the WM_SETFOCUS
and WM_KILLFOCUS topics in the
"Keyboard Input Reference" section in
the MSDN library at
http://msdn.microsoft.com/library.http://msdn.microsoft.com/library.

Categories

Resources