473,769 Members | 5,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Control loading through code

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=tr ue
x(i).show
next i
Nov 20 '05 #1
3 1392
>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=tr ue
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.
Nov 20 '05 #2
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=tr ue
x(i).show
next i


You must also add the controls to the Controls

collection of theparent (probably your Form).

Controls.Add(x (i))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.comPlease reply only to the newsgroup.
.

Nov 20 '05 #3
* "Robert" <an*******@disc ussions.microso ft.com> 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(Con trolName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(Find Control("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 DynamicPictureB ox As New PictureBox()
DynamicPictureB ox.Name = "PictureBox 1"
m_Controls.Add( DynamicPictureB ox.Name, DynamicPictureB ox)
///

Looking for a control:

\\\
Dim p As PictureBox = DirectCast(m_Co ntrols.Item("Pi ctureBox1"), PictureBox)
///

Removing a control:

\\\
m_Controls.Remo ve("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/vbtchCreatingCo ntrolArraysInVi sualBasicNETVis ualCNET.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/>
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2457
by: Eric | last post by:
I'm trying to dynamically load a user control using on the .NET framework (not Visual Studio). The control was designed in Visual Studio and is named: Disable.ascx The first line is: <%@ Control Language="c#" className="Disable1" src="Disable.ascx.cs" %> The host page for this control is named UserControl.aspx. The first 2 lines are: <%@ Reference control="Disable.ascx" %>
3
8524
by: buran | last post by:
Dear ASP.NET Programmers, I am loading a web user control ("taclient.ascx") into a placeholder (ID: phFA). The web user control contains a cancel button (ID: btnCancel). I would like to "unload" the web user control when the user clicks on the cancel button. How can I accomplish this? Thanks in advance. dim myControl as Control myControl = LoadControl("taclient.ascx") phFA.Controls.Clear()
1
3359
by: John Cosmas | last post by:
I've got a page which loads up a different user control into a placeholder control every time a button is clicked on the parent page. I use a statement like Me.plcTabViews.Controls.Add(LoadControl("test.ascx")). There are up to 30 possibilities which are decided by CASE statements and keeps one from loading up onto another. However, I'm running into a unique error... Failed to load viewstate. The control tree into which viewstate is...
7
3017
by: Shimon Sim | last post by:
I have a custom composite control I have following property
0
1318
by: Joe | last post by:
I've a web form that inherits from a BasePage. There is a user control on the web form. Does anyone thinks it's a problem if I've my BasePage loading a user control and my web form loading a different user control. I'm more worry on the sequence as which user control gets loaded first. The user control I've on the web form depends on the user control on the BasePage to load first. Most of the time, it seems everything is ok (BasePage...
0
1208
by: Prabhakar | last post by:
Hi Friends, Do we have any provision for using RichTextControl or Loading Files in ASP.NET using C# as Code Behind??? We dont have RichTextControl, so i searched in Microsoft website but i still didnt get the control....if there is any free control plz send me the link...or if there is any way of loading files plz tell me the way....
7
12310
by: ApexData | last post by:
I am using the following code in my TabControl to manage subform loads. The code assigns the subForms SourceObject. - Do I also need code to DeAssign the SourceObject when leaving the Tab, I'm thinking the Table will stay open otherwise ??? - Do I also need to use code to Assign the Child&Master Links, or can I just type the names into the subForms Control Property and just depend on the SourceObject to link to Table???
2
15071
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have been able to find the cause of the problem, and will describe it here first textually and then through a code example. The purpose of what I am trying to do is to create a postback-free web application through the use of ASP.net AJAX UpdatePanels...
2
7180
by: adiel_g | last post by:
I added a user control to a webform in Asp.net 2.0. I am also adding several other user controls to this webform. Now I am trying to find a way to stop the user controls from loading up when I call the webform. I could possibly have 10 user controls in this page and I would not want all of them to load up. I would like to control which user control loads up. I have tried placing each user control in a separate placeholder and then...
2
1689
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I have a web app that when I click a menu, I load a control. On one of these controls, I have a button that when clicked, I need to unload the control to unload itself and load a different control in its place. I am not sure how to do this from within a control itself. Can anyone offer any help and show me what I am doing wrong with my code below? //This is on one of my controls. protected void btnSubmit_Click(object sender, EventArgs e)...
0
9583
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10039
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5297
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.