When Adding A Control, what events fire? [modified]
-
I'm building a custom control inherited from a picturebox control, and I would like it to automatically resize to the dimensions of the container control to which it is added(could be any valid container control) Referencing the Parent of the custom control in its structure definition Public MycustomePicBox() { this.Size=new System.Drawing.Size(this.Parent.Clientrectangle.... } isn't possible because at the time the code executes, the custom control hasn't been added to the container control yet. Is there an event that fires for a Control when it has been added to a container?
modified on Sunday, March 29, 2009 10:43 PM
-
I'm building a custom control inherited from a picturebox control, and I would like it to automatically resize to the dimensions of the container control to which it is added(could be any valid container control) Referencing the Parent of the custom control in its structure definition Public MycustomePicBox() { this.Size=new System.Drawing.Size(this.Parent.Clientrectangle.... } isn't possible because at the time the code executes, the custom control hasn't been added to the container control yet. Is there an event that fires for a Control when it has been added to a container?
modified on Sunday, March 29, 2009 10:43 PM
I hope I haven’t got the wrong end of the stick of what you’re trying to achieve. I’ve just had a quick look, and found that there’s an event available for when controls are added to a user control. Try using the ControlAdded Event. To initialize it do the following…
public userControl1() { InitializeComponent(); /* Please note, the code below should really be inside InitializeComponent(); But for speed, i've added it in here. Add the line below… Note: Press Tab key after typing in += as this will auto complete the rest of the event. */ this.ControlAdded+=new ControlEventHandler(userControl1_ControlAdded); } private void userControl1_ControlAdded(object sender, ControlEventArgs e) { // execute your methods for resizing to parent control. }
I hope this has helped? :) P.S. I am using the latest version of C#.Net framework 3.5 so I’m guessing that these events are also available for frameworks 1.1 and 2. Rule 1 - Don’t break what’s not broken! Rule 2 - Don’t reinvent the wheel… just give it a lick of paint! Rule 3 - Keep plenty of programmer foods to hand… Pizza, beer & crisps... including the books on "How to Crash Your System in Twenty Seconds!" -
I hope I haven’t got the wrong end of the stick of what you’re trying to achieve. I’ve just had a quick look, and found that there’s an event available for when controls are added to a user control. Try using the ControlAdded Event. To initialize it do the following…
public userControl1() { InitializeComponent(); /* Please note, the code below should really be inside InitializeComponent(); But for speed, i've added it in here. Add the line below… Note: Press Tab key after typing in += as this will auto complete the rest of the event. */ this.ControlAdded+=new ControlEventHandler(userControl1_ControlAdded); } private void userControl1_ControlAdded(object sender, ControlEventArgs e) { // execute your methods for resizing to parent control. }
I hope this has helped? :) P.S. I am using the latest version of C#.Net framework 3.5 so I’m guessing that these events are also available for frameworks 1.1 and 2. Rule 1 - Don’t break what’s not broken! Rule 2 - Don’t reinvent the wheel… just give it a lick of paint! Rule 3 - Keep plenty of programmer foods to hand… Pizza, beer & crisps... including the books on "How to Crash Your System in Twenty Seconds!"