Adding a control programmingly

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, everyone

After adding a control into a container like form using form.controls.add(myControl), how could I reference it
For example
dim aControl as new butto
me.controls.add(aControl

then I want set backcolor of this button, how

Thanks

william
 
* "=?Utf-8?B?d2lsbGlhbQ==?= said:
After adding a control into a container like form using form.controls.add(myControl), how could I reference it?
For example,
dim aControl as new button

\\\
aControl.BackColor = ...
///
me.controls.add(aControl)

then I want set backcolor of this button, how?

Store a reference to the control, for example, in a private variable.
 
Hi
Sorry, I didn't explain clearly
I added the control in a function, so the local parameter aControl is out of the scope, and I want to set the backcolor of the control in other function, How can I get the handler of the control

Thanks

willia
 
William
What you want to do is very easily when you utilize the built-in structures of the System.Windows.Forms objects
In your code segment, you added a control to the form. When you add a control to an object with a ControlCollection structure, it assigns the parent reference of the control to the object, in your case, the form

To be even more direct, let me point out the ControlsCollection is not the parent of the control you added to the form. It is just a structure to hold references to the controls

You can use the form.Controls(intControlIndex) to access your control. For example

Dim aControl As New Button(
Dim intControlIndex As Intege
Me.Controls.Add(aControl
intControlIndex = Me.Controls.Count -
Me.Controls(intControlIndex).BackColor = System.Drawing.Color.Purpl
 
Hi ,
Thanks, that helps.
Let's look further, if I added control in my parent form, and I have a child form inherited from the parent form, in child form, how can I set backcolor of the control ?

william
 
* "=?Utf-8?B?d2lsbGlhbQ==?= said:
Sorry, I didn't explain clearly.
I added the control in a function, so the local parameter aControl is out of the scope, and I want to set the backcolor of the control in other function, How can I get the handler of the control?

\\\
DirectCast(TheForm.Controls(5), Label).Text = "Foo"
///
 
Back
Top