Array of UserControl - event

Array of UserControl - How to assign events

Page 1 of 1

1 Replies - 9338 Views - Last Post: 30 July 2008 - 04:12 AM Rate Topic: -----

#1 Hansx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-July 08

Array of UserControl - event

Post icon  Posted 29 July 2008 - 02:40 PM

Hi there,
I have a problem. I have created an UserControl, let say a legend to a chart. Here is the relevant part of code.
public Color Color 
		{
			get
			{
				return color;
			}
			set
			{
				if (value >= 0 && value < 8)
					color = value;
				else throw new  OverflowException("Out of Range");
				
				if (ColorChanged != null) {
					ColorChanged(this, EventArgs.Empty);
				}
			}
		}
private Color color;


Now, in another form - Main form I have used the following to create an array of UserControl:
UserControl[] legend;
legend = new UserControl[8];
for (int i=0; i <8; i++)
{
 legend[i] = new UserControl();
 legend[i].Parent = this;
 legend[i].Location = new Point(5, 5 + (i+5)*legend[i].Height;
}
.
.
.


Now, how to recognize which UserControl in the array legend has changed its Color property?
Meaning something like:
void legend[]ColorChanged()
{
 Chart1.Series[].Color = legend[].Color
}



Thanks
Hans

Is This A Good Question/Topic? 0
  • +

Replies To: Array of UserControl - event

#2 kikz   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 32
  • Joined: 21-July 08

Re: Array of UserControl - event

Posted 30 July 2008 - 04:12 AM

Sender in the object sender part of the handler is the object that fired the event. Thus, just cast sender to UserControl and you have which conrol out of your array fired the event.

for (int i=0; i <8; i++)
{
  legend[i] = new UserControl();
  legend[i].Parent = this;
  legend[i].Location = new Point(5, 5 + (i+5)*legend[i].Height;
  legend[i].ColorChanged += new EventHandlerDelegate(MyColorChanged);
}

private void MyColorChanged(object sender, EventArgs e)
{
  UserControl legend = (sender as UserControl);
  if (legend != null)
  {
	// legend is the control that had color changed
  }
}


This post has been edited by kikz: 30 July 2008 - 04:17 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1