all groups > dotnet windows forms > march 2004 >
You're in the

dotnet windows forms

group:

Adding a control programmingly


Adding a control programmingly william
3/31/2004 9:21:11 PM
dotnet windows forms:
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

Re: Adding a control programmingly william
4/1/2004 6:51:09 AM
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
RE: Adding a control programmingly Bruce Wilson [MCSD]
4/1/2004 6:56:16 AM
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
Re: Adding a control programmingly hirf-spam-me-here NO[at]SPAM gmx.at
4/1/2004 7:41:11 AM
* "=?Utf-8?B?d2lsbGlhbQ==?=" <anonymous@discussions.microsoft.com> scripsit:
[quoted text, click to view]

\\\
aControl.BackColor = ...
///

[quoted text, click to view]

Store a reference to the control, for example, in a private variable.

--
Herfried K. Wagner [MVP]
RE: Adding a control programmingly william
4/1/2004 8:26:12 AM
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 ?

Re: Adding a control programmingly hirf-spam-me-here NO[at]SPAM gmx.at
4/1/2004 7:15:37 PM
* "=?Utf-8?B?d2lsbGlhbQ==?=" <anonymous@discussions.microsoft.com> scripsit:
[quoted text, click to view]

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

--
Herfried K. Wagner [MVP]
AddThis Social Bookmark Button