Multiple controls at runtime problem
-
Hi all, here is what I do and need; - I create multiple controls at runtime: (myControl1, myControl2, myControl3...) - I have only one event definition for all my controls: (myControl_Enter) - Everything works fine but what I need is: when the myControl_Enter event is fired, I want "only" that instance of the control to change backcolor. below is the part of the code needed:
private int counter = 0; public void AddNewMyControl() { counter += 1; myClass.MyControl myControl = new myClass.MyControl(); myControl.Dock = System.Windows.Forms.DockStyle.Top; myControl.Location = new System.Drawing.Point(0, 0); myControl.Name = "myControl" + counter; myControl.Size = new System.Drawing.Size(536, 168); myControl.Enter += new System.EventHandler(myControl_Enter); this.Controls.Add(myControl); } private void myControl_Enter(object sender, System.EventArgs e) { //change the backcolor of only this instance of myControl }
thank you in advance. Radgar "Imagination is more important than knowledge." - Albert Einstein -
Hi all, here is what I do and need; - I create multiple controls at runtime: (myControl1, myControl2, myControl3...) - I have only one event definition for all my controls: (myControl_Enter) - Everything works fine but what I need is: when the myControl_Enter event is fired, I want "only" that instance of the control to change backcolor. below is the part of the code needed:
private int counter = 0; public void AddNewMyControl() { counter += 1; myClass.MyControl myControl = new myClass.MyControl(); myControl.Dock = System.Windows.Forms.DockStyle.Top; myControl.Location = new System.Drawing.Point(0, 0); myControl.Name = "myControl" + counter; myControl.Size = new System.Drawing.Size(536, 168); myControl.Enter += new System.EventHandler(myControl_Enter); this.Controls.Add(myControl); } private void myControl_Enter(object sender, System.EventArgs e) { //change the backcolor of only this instance of myControl }
thank you in advance. Radgar "Imagination is more important than knowledge." - Albert EinsteinThe sender object that is sent as one of the arguments of the event is a reference to the object throwing the event. so you only have to cast the sender object and set the background color. Like this:
private void myControl_Enter(object sender, System.EventArgs e) { ((myClass.MyControl)sender).BackColor = System.Drawing.Color.Green; }
Gidon -
The sender object that is sent as one of the arguments of the event is a reference to the object throwing the event. so you only have to cast the sender object and set the background color. Like this:
private void myControl_Enter(object sender, System.EventArgs e) { ((myClass.MyControl)sender).BackColor = System.Drawing.Color.Green; }
Gidon