How to add events to custom UI controls in Visual Studio? - c#

I'm working on a project that uses a RichTextBox. My project required me to make use of bold/italics/underline/colour/etc. To handle all of this, I downloaded a custom application from the Internet, "RicherTextBox".
This new control works well for formatting text, however events like KeyDown, KeyUp don't work. Other events that were present in RichTextBox like LinkClicked are missing.
Since I have the code for RicherTextBox, I can customize it as desired.
How can I make KeyDown, KeyUp work like it does for RichTextBox?
How can I add missing events like LinkClicked?

That entirely depends on what's going on inside that RicherTextBox control. Does it just extend the regular RichTextBox control? They could be intercepting and handling events. You'll have to check out the source code to be sure.
See if they're subscribing to the KeyDown and KeyUp events. There's a Handled property on the KeyEventArgs parameter for those events. If they set e.Handled = true, it'll prevent the event from going any further up the chain.
I don't see a Handled event in the LinkClickedEventArgs that's passed to the LinkClicked event, so I'm not sure. You'll have to dig into the source code.

Related

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.

(MouseClick versus Click events for a RichTextBox) Why did MouseClick stop working?

I had a MouseClick event for a RichTextBox working in my code, and something I must have done caused it to stop working. (Perhaps by altering the selected text location via the code below). I have now gotten my code to function well by using the Click event instead. The MouseClick event is simply not firing anymore, but it used to work.
_Box.Select(selectionExecutePosition,selectionExecuteLength);
_Box.Focus();
the events in question:
this.richTextBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.richTextBox1_Clicked);
this.richTextBox1.Click += new System.EventHandler(this.richTextBox1_Clicked);
I must have changed something unawares. Any guesses/thoughts as to what I might have accidentally changed?

Why do some events just appear in the code?

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.

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;
}

Global PreviewKeyDown handler vs local PreviewKeyDown handler

I have a PreviewKeyDown handler on my mainwindow which handles up and down keys so I can navigate with the keyboard between my controls.
Now I have the problem that in some Textboxes I also want to use the up/down keys. This seems impossible because the other handler seems to swallow the keys first.
Is it possible that when one of these TextBox controls are focused they get the up/down keys first and then then swallow them so that the "global" PreviewKeyDown does not get them?
Sure I could disable the global handler somehow when such a TextBox got focus but is this good style?
You don't really have an option, aside from filtering out those keys in the global key handler.
The reason that you're having this problem is that all of the Preview* events are tunneling, meaning that controls higher in the visual tree get them first (starting at the root). The very reason why you're using this event in the first place is causing your problem.
One less than ideal option would be to register a class handler for TextBox.PreviewKeyDown (see EventManager.RegisterClassHandler()). While this would be called before your window's PreviewKeyDown handler, it will be called for all TextBoxes in your application. This may or not be what you want.

Categories

Resources