PC Review


Reply
Thread Tools Rate Thread

Control loading through code

 
 
Robert
Guest
Posts: n/a
 
      16th Jul 2004
How can I declare in VB .NET an array of labels for
example and afterwards using a FOR structure load every
component of the array?

I've used this code but it doesn't work:

dim x(10) as label

for i=0 to 10
x(i)=new label
x(i).visible=true
x(i).show
next i
 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      16th Jul 2004
>How can I declare in VB .NET an array of labels for
>example and afterwards using a FOR structure load every
>component of the array?


Yes.


>I've used this code but it doesn't work:
>
>dim x(10) as label
>
>for i=0 to 10
> x(i)=new label
> x(i).visible=true
> x(i).show
>next i


You must also add the controls to the Controls collection of the
parent (probably your Form).

Controls.Add(x(i))



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Robert
Guest
Posts: n/a
 
      16th Jul 2004
Thanks a lot, it works!!!

I've also noticed in vb. net that if I have a label
control on the form and then copy it on the same form I
don't have the option to create an array. Was this
possibility removed?


>-----Original Message-----
>>How can I declare in VB .NET an array of labels for
>>example and afterwards using a FOR structure load every
>>component of the array?

>
>Yes.
>
>
>>I've used this code but it doesn't work:
>>
>>dim x(10) as label
>>
>>for i=0 to 10
>> x(i)=new label
>> x(i).visible=true
>> x(i).show
>>next i

>
>You must also add the controls to the Controls

collection of the
>parent (probably your Form).
>
>Controls.Add(x(i))
>
>
>
>Mattias
>
>--
>Mattias Sjögren [MVP] mattias @ mvps.org
>http://www.msjogren.net/dotnet/ |

http://www.dotnetinterop.com
>Please reply only to the newsgroup.
>.
>

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      16th Jul 2004
* "Robert" <(E-Mail Removed)> scripsit:
> I've also noticed in vb. net that if I have a label
> control on the form and then copy it on the same form I
> don't have the option to create an array. Was this
> possibility removed?


Yes.

My FAQ:

\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("btnBla", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a
lot of controls by name very often, you should store references to them in a
'Hashtable' object. You can use the name of the control as key:

\\\
Private m_Controls As New Hashtable()
///

Adding a control:

\\\
Dim DynamicPictureBox As New PictureBox()
DynamicPictureBox.Name = "PictureBox1"
m_Controls.Add(DynamicPictureBox.Name, DynamicPictureBox)
///

Looking for a control:

\\\
Dim p As PictureBox = DirectCast(m_Controls.Item("PictureBox1"), PictureBox)
///

Removing a control:

\\\
m_Controls.Remove("PictureBox1")
///

Sometimes it's even better to add the control to an array. This will allow
fast and easy index-based access to the control references:

\\\
Dim MyLabels() As Label = {Label1, Label2, ..., Label10}
///

Access by 'MyLabels(0)' to 'MyLabels(9)'.

Control arrays:

Control arrays, as known from VB6, are not included in VB.NET 2002/2003.

Creating Control Arrays in Visual Basic .NET and Visual C# .NET:
<URL:http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vbtchCreatingControlArraysInVisualBasicNETVisualCNET.asp>

WinForms Controls--Creating Control Arrays in VB.NET
<URL:http://www.devx.com/vb2themax/Article/19907/>

In VS.NET "Whidbey" (2005) control arrays will be supported natively.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading native code dll using vc++ .net 2.0 managed code antarikshv Microsoft Dot NET Framework 5 25th Apr 2009 10:13 AM
Dynamically loading user control from current control davidr@sharpesoft.com Microsoft ASP .NET 0 11th Sep 2006 10:02 PM
Loading Dynamic User Control Error: "The control must be placed inside a form tag with runat=server" Help Please Second time posting. davidr@sharpesoft.com Microsoft ASP .NET 0 31st Aug 2006 06:26 PM
Dynamic control array loading, can't unload control/replace with o =?Utf-8?B?Y2luZHk=?= Microsoft ASP .NET 2 8th Jun 2005 04:54 AM
Re: adding runat=server to a control on aspx page, don't cause the IDE to add the control to the code behind Phil Winstanley [Microsoft MVP ASP.NET] Microsoft ASP .NET 0 24th Jun 2004 01:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:21 PM.