Click to See Complete Forum and Search --> : Passing Data between Controls


lyar1031
October 25th, 2006, 01:14 PM
I have a question. I created 2 custom controls which my application uses. For simplicity lets call them Ctrl1 and Ctrl2.

How do I pass data from Ctrl1 to Ctrl2.
For instance lets say Ctrl1 changes a color. That same color must be passed to Ctrl2.


OR:
Is it possible to raise an event in Ctrl1 (OnColorChange...for example). This event gets called in the main application. From the main application the color can be passed to Ctrl2.

I believe the Event is the way to do it. Any help would be appreciated


Thanks

Skoons
October 25th, 2006, 06:52 PM
Well, event is correct idea, for example you created event on ColorChange, so to exchange data you need to create properties like

public Color MyColor
{
get{return this.color;}
set{this.color=value;}
}
// and event ofcourse
void OnColorChange(object Sender)
{
Ctrl1.MyColor=Ctrl2.SelectedColor;
}

Ofcourse you need to create properties and data fields to both controls :wave:

lyar1031
October 25th, 2006, 09:05 PM
Thanks, Actually I thought it was more compilcated. I guess I did not explain myself well. But if you goto the help and type Raising Events, there is an example that deals w/ an alarm clock.

It does exactly what I wanted. raising an event that I created in one class and another class consumes the event!

Either way...Thanks for all the help

jhammer
October 26th, 2006, 03:54 AM
There is a more simple way.
In the form Load event you just add one line of code:

private void Form1_Load(object sender, System.EventArgs e)
{
label2.DataBindings.Add("ForeColor",label1,"ForeColor");
}

This way whenever the ForeColor of label1 changes, the ForeColor of label2 changes to the same color. No need for events.