473,480 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Propagation of events from custom control to Form

Rob
When a custom control is used within a form, that control does not get
events directly. I've run into this a couple times with both mouse
and keyboard events.

Ex: A custom control inherits from UserControl (or Panel, etc). If
that particular control is being edited, it IS possible to assign
handlers and get events.

However, when the control is used on the main form, assigning a
handler to the particular control does nothing--the handler never
executes.

Rephrased: Using a ListView directly on a form == no problem.
CustomListView control has a ListView anchored to all sides. Drag it
to a form. Assign event handlers. Nothing.

This has to be something to do with the hierarchy of event
propagation, right? How is this normally handled?

Nov 7 '06 #1
9 5086
What events specifically? Most events don't automatically propegate - they
are local to the source. If you want it to do this you need to handle it
yourself.

Marc
Nov 7 '06 #2
Rob
On Tue, 7 Nov 2006 10:56:14 -0000, "Marc Gravell"
<ma**********@gmail.comwrote:
>What events specifically?
Keyboard in this case. Previously ran across the same thing with Mouse
events.
Most events don't automatically propegate - they
are local to the source.
Every normal control handles its events at the expected place. I'm not
sure why this would be any different. Drag the custom control to the
form, use the properties editor on that screen to assign a KeyDown
handler. What could be more intuitive?
If you want it to do this you need to handle it yourself.
How is that?
Nov 7 '06 #3
The events you subscribe in the IDE are going to your UserControl / Control;
however, this will only fire events if it is the control receiving input.
For instance, a "Click" *can* get fired *if* you click on the grey area
between the child controls. Of course, if you have anchor / dock - filled,
then it can't get these inputs.

The trick here would either be for your child control to trigger the wrapper
control's events (by calling OnSomeEvent), or to expose passthru events
instead, e.g.

public EventHandler TextboxClicked {
add {textbox1.Click += value;}
remove {textbox1.Click -= value;}
}

Of course, this latter can be confusing as the "sender" is now textbox1, not
your (wrapper) control instance...

Marc
Nov 7 '06 #4
I am not sure exactly what you are asking but here are two possible
causes of the behaviour you mention.

Your user control contains other controls that have the keyboard focus.
These events will be passed to the UserControl but will not be passed
to the form unless you explicitly relay them on in code.

The form has KeyPreview set to true and are being flagged up as
handled. In this case the key press will never be passed on to any
other control.

The event will be handled somewhere you just need to find out where and
then route it through to the form.

Rob wrote:
On Tue, 7 Nov 2006 10:56:14 -0000, "Marc Gravell"
<ma**********@gmail.comwrote:
What events specifically?

Keyboard in this case. Previously ran across the same thing with Mouse
events.
Most events don't automatically propegate - they
are local to the source.

Every normal control handles its events at the expected place. I'm not
sure why this would be any different. Drag the custom control to the
form, use the properties editor on that screen to assign a KeyDown
handler. What could be more intuitive?
If you want it to do this you need to handle it yourself.

How is that?
Nov 7 '06 #5
rob
On 7 Nov 2006 05:23:19 -0800, "Olie" <ow****@gmail.comwrote:
>I am not sure exactly what you are asking but here are two possible
causes of the behaviour you mention.

Your user control contains other controls that have the keyboard focus.
These events will be passed to the UserControl but will not be passed
to the form unless you explicitly relay them on in code.

The form has KeyPreview set to true and are being flagged up as
handled. In this case the key press will never be passed on to any
other control.

The event will be handled somewhere you just need to find out where and
then route it through to the form.
To clarify:

class CustomControl : UserControl
{
ListView lv; // anchored to all four sides
}

Normally, when a ListView control is dropped onto a form, the control
on the form can be r-clicked, and its events can be wired into the
form's code.

You are correct that in this case, the event never gets that far, but
it is not due to KeyPreview--they just stop at the UserControl level.
Ideally, this custom control should generate events exactly like a
regular ListView.

So I've got to 'rethrow' keyboard events to make it appear that it was
generated by the custom control. And it should look just like a
regular KeyPress event to the client form. That is where I'm short on
info. Any references?
Nov 8 '06 #6
rob
On Tue, 7 Nov 2006 13:14:05 -0000, "Marc Gravell"
<ma**********@gmail.comwrote:
>The events you subscribe in the IDE are going to your UserControl / Control;
however, this will only fire events if it is the control receiving input.
For instance, a "Click" *can* get fired *if* you click on the grey area
between the child controls. Of course, if you have anchor / dock - filled,
then it can't get these inputs.

The trick here would either be for your child control to trigger the wrapper
control's events (by calling OnSomeEvent), or to expose passthru events
instead, e.g.

public EventHandler TextboxClicked {
add {textbox1.Click += value;}
remove {textbox1.Click -= value;}
}

Of course, this latter can be confusing as the "sender" is now textbox1, not
your (wrapper) control instance...
I was with you up to the last part. I did consider trying to find the
parent's 'OnKeyPressed' handler and connecting to that, but it does
not seem like the right approach.

When you refer to 'passthru events', do you mean there is a mechanism
in place for passing the ListView's KeyPressed events up through to
the client form? Easy enough to get them in the UserControl-based
container, of course, but the problem is how to regenerate them so it
looks like the UserControl is the source of the KeyPressed event. That
is what I'm looking for.
Nov 8 '06 #7
Nope; unfortunately the events are not marked as virtual, otherwise yes: you
could do this and simply hook into every sub-control. So if you want to use
the standard events, you must explicitely raise the OnEvents yourself...
AFAIK.

Marc
Nov 8 '06 #8
I think I have a better understanding of what you are trying to do. My
first question to you is why are you not inheritting from ListBox
rather than UserControl. If you wanted to construct your user control
from multiple controls then your approach is correct. If you inherit
from ListBox then you will not have to worry about re-routting the
events.

Going back to what you are trying to do. I think this is where you are
getting confused but I am not sure.

When you drop a control on a UserControl the events for that control
will need to be handled in the UserControl Code. So you add a list box
to the control and it will fire an event in the UserControl. If you
then do not handle it the event will be lost. They do not get
propagated down to the form! There is a very good reason for this as it
allows you to control the events in your user control.

You need to handle the ListBox events in your UserControl and then
raise the event in your user control. This way the event will be passed
to the form.

Nov 9 '06 #9
rob
On 9 Nov 2006 02:04:55 -0800, "Olie" <ow****@gmail.comwrote:
>I think I have a better understanding of what you are trying to do. My
first question to you is why are you not inheritting from ListBox
rather than UserControl. If you wanted to construct your user control
from multiple controls then your approach is correct. If you inherit
from ListBox then you will not have to worry about re-routting the
events.
A week late in reply, sorry.

It's actually a ListView. I needed to set column defaults, etc for the
ListView, and it seemed that putting the listView in a UserControl
would be an easy way to do that.
>You need to handle the ListBox events in your UserControl and then
raise the event in your user control. This way the event will be passed
to the form.
I have been able to do that with most events, but I haven't been able
to get item checkbox-clicked events back to the parent.

The problem could be solved by:
Getting checkbox-changed events to the parent form
or
Finding a convenient way to edit colummns and other parameters when
inheriting directly from ListView.
Nov 19 '06 #10

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

Similar topics

3
25213
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a...
1
7296
by: Mark | last post by:
Hi, I'm writing user controls with custom events. I have a parent custom event that exposes some abstract methods and some custom events. I have also created some new user controls that...
3
549
by: jlea | last post by:
I've created a custom control based on TreeView and it handles several events, such as Mousedown. I added this custom control to the toolbox in another project, dragged the custom control to the...
1
1961
by: Lamont Adams | last post by:
Hi all, I've created numerous custom controls of varying complexity, but I've been on this problem for a day and a half, and I can't figure this mystery out. I hope one of you kind folks can...
12
2759
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
5
1704
by: snesbit | last post by:
If a screen is made up of several user controls and those user controls contain various packaged or standard controls such as a grid, how do you raise both standard and custom events from the user...
4
1084
by: Pieter | last post by:
Hi, For a VB.NET 2005 application I need some kind Application Variable that holds a value that can be accessed everywhere in the application. It should also be available for a usercontrol in a...
7
1729
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its...
2
3236
by: ahmed.maryam | last post by:
Hello Everyone, I designed a custom control that is entirely covered by a picture box. I then dragged this custom control onto a windows form application (called main) and I need to handle mouse...
0
7033
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6903
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6726
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.