Complex UserControl, need help...

J

junk

Senerio:
I have a custom user control which contains two control arrays. The user
control has a group box in which the two control arrays are dynamically
built. The two control arrays are RadioButtons and PictureBoxes.

Task:
What I need to accomplish with this custom user control is to rearrange the
pictures in the PictureBox Control Array based on what RadioButton is
selected. I do not want my RadioButton Control array to have a dependency on
the PictureBox Control Array as I use that RadioButton Control array
elsewhere in this project.

My though is that I need to have an "event monitor" in the custom user
control, such that when the RadioButton OnClick event is fired the parent is
notified and can then fire off a method to handle changing the pictures in
the PictureBox array. This is however, something that I have not tackled in
the passed so I am unsure how to approach getting this accomplished. There
is most likely a very easy solution.

If anyone would be so kind as to point me into the right direction I would
be very grateful...

Thanks - Johan.
 
G

Guest

Add the below to your class and whenever you radio buttons are clicked you
can call RaiseClick() which will raise the Click event if anybody is
subscribed. Then your form can handle click like it does with a button.


public delegate void RadioButtonArrayClickHandler(RadioButtonArray sender,
EventArgs e);
public event RadioButtonArrayClickHandler Click;

protected void RaiseClick(){
if (Click != null)Click(this,new EventArgs());
}



HTH

Ciaran O'Donnell
 
D

Dave Sexton

Hi Ciaran,

Note: the standard naming convention would be:

protected virtual void OnClick(EventArgs e)
{
}

and could be invoked from the UserControl as such:

OnClick(EventArgs.Empty);


And in 2.0 there is no longer a need to declare a custom delegate, just a
custom EventArgs implementation when desired:

public event EventHandler<RadioClickEventArgs> Click;

public class RadioClickEventArgs : EventArgs
{
...
}
 
J

junk

Ciaran,
Thank you for the quick response. Sorry, but I must ask and so to
clearify...

Put the delegate (your example code) into the control array?

This then allows the custom user control to subscribe to the event. What
does the call in the customer user control look like?

Thanks again - Johan.
 
J

junk

Dave,

I am very confused at this point. The Event propagation from the RadioButton
Control Array to the User Control is still not intuitive enough for me to
grasp. I must be a blockhead.....

Is the following correct?

In the RadioButton Control Array I add the following:
NOTE: I assume that if I already have a ClickHandler in this control array I
should remove it?

public delegate void OnClickEventHandler(object sender, EventArgs e);
public event OnClickEventHandler Click;

protected virtual void OnClick(EventArgs e){
if (Click != null) {
// Invokes the delegates.
Click(this, e);
}
}

Now how does this notifiy my user control that a radio button was click so
that the user control can react to it?

Sorry to be such a pain...

Johan.
 
D

Dave Sexton

Hi Johan,
I am very confused at this point. The Event propagation from the RadioButton
Control Array to the User Control is still not intuitive enough for me to
grasp. I must be a blockhead.....

Maybe if you clarify this statement:

"What I need to accomplish with this custom user control is to rearrange the
pictures in the PictureBox Control Array based on what RadioButton is
selected"

then I'll probably be able to help you.
Is the following correct?

In the RadioButton Control Array I add the following:
NOTE: I assume that if I already have a ClickHandler in this control array I
should remove it?

What do you mean by, "In" the RadioButton Control Array?

I assumed that it was just a one-dimensional array of RadioButtons. Is it a
custom control? If so, is the PictureBox Control Array you have been
referring to a custom control as well?

If so, what are the exact names of the custom controls? (it will be much
easier to refer to them using their given names)
public delegate void OnClickEventHandler(object sender, EventArgs e);
public event OnClickEventHandler Click;

protected virtual void OnClick(EventArgs e){
if (Click != null) {
// Invokes the delegates.
Click(this, e);
}
}

Now how does this notifiy my user control that a radio button was click so
that the user control can react to it?

Well, you aren't using the standardized naming convention for the delegate,
but since you're only using EventArgs you don't even need to declare a
delegate anyway. You could just use System.EventHandler.

But, before answering this question I think you might want to answer mine. I
don't want to confuse the situation.
Sorry to be such a pain...

No pain, no gain - or something like that ;)
 

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