Click Event problem

G

Guest

Hi

[ Using C# in .Net 1.1 ]

I have a usercontrol with a number of nested controls (listviews, etc... ).
When the user clicks *anywhere* on the usercontrol I want the event to be
handled by the usercontrol. At the moment if I click on the listbox, then
the listbox grabs that click and it is not seen by the usercontrol. Is it
possible to easily make the listboxes not grab the click ( I know I could
forward the click on, but this would mean I would need to make every control
on my form handle this which would be a pain ), so that I can have a single
event handler in my usercontrol ??

regards,
Rich.
 
B

Bruce Wood

First, you need to decide whether you want to user control to react to
the click event, in addition to the individual controls doing their
usual thing, or you want the user control to swallow the click event
and the individual controls never see it.

The second option is really strange and I would question why you would
want to do that: it would mean that the user would never be able to
select anything in a list box, for example, since the list box would
never receive a Click event. In addition, I'm not even sure how you
would go about this.

The first option is more logical and yes, it requires that your user
control subscribe to the Click event from every control it contains.
This, however, isn't as bad as it sounds. In your user control's
constructor, you can simply say:

foreach (Control c in this.Controls)
{
c.Click += new System.EventHandler(this.aControl_Click);
}

and add two methods:

protected override void OnControlAdded(ControlEventArgs e)
{
e.Control.Click += new System.EventHandler(this.aControl_Click);
}

protected override void OnControlRemoved(ControlEventArgs e)
{
e.Control.Click -= new System.EventHandler(this.aControl_Click);
}

and you're done.
 
G

Guest

Unfortunately your solution doesnt really help, and yes it is the "second
option". The reason I want this is that I have an information screen with
listboxes ( and other nested controls ) of info etc... And I want my
ContextMenu to be launched regardless of where I rightclick ( or press the
"application" key, or Ctrl-F10 ) on that info screen.

I'm not over happy with my current solution but it will have to do; I loop
though all of the controls and then recursively call this for child controls
to assign my context menu to all of the controls ( and nested controls ) on
my info screen.

I did look at overriding the WndProc func and returning "HTTRANSPARENT" for
the WM_NCHITTEST msg, but this would require that I overrode all of the
controls on my info screen, which would have been a hassle.

Thanks for the reply.
RichS.
 
B

Bruce Wood

Ahhh. More information means better advice. :)

I would do it the same way that you are doing it: loop through all
child controls and assign the context menu to each one in turn. Of
course I would override the ContextMenu property in my user control and
set the context menu in all child controls from in there, so that the
"outside world" just sees a ContextMenu property and that's it.
 
G

Guest

Thanks, I should have mentioned the ContextMenu in the first post, but I was
hoping that there was an easy way of routing all mouse / keyboard events to a
parent control. Thinking that this would be useful for other situations.

I like your idea of customising the ContextMenu to encapsulate the iterative
assignment rather than performing this externally.

Thanks for the replies.
Richs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top