473,545 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reference Control help

Hi

I have a control that allows embeddable editors, so for example I can set a
property of controlsEmbedda bleEditor =me.TextBox1 on my form, no problem
here - what I want to do is the same thing but from a string variable.

i.e. I have 14 controls that have a name which is a permutation of 4
characters, my program selects which control to use by a 4 character name
which matches a control name (it's a zillion times more complex than this
but for examples sake). my trouble is I know the name of the control to use,
and the control is contained on the form, but how do I reference it, as the
name is contained within a string i.e
controlsEmbedda bleEditor="myco ntrolname" can I ctype, Directcast e.t.c if so
how???

Many thanks.

Regards,
Nov 20 '05 #1
2 1335
* "Merlin" <im*****@hotmai l.com> scripsit:
i.e. I have 14 controls that have a name which is a permutation of 4
characters, my program selects which control to use by a 4 character name
which matches a control name (it's a zillion times more complex than this
but for examples sake). my trouble is I know the name of the control to use,
and the control is contained on the form, but how do I reference it, as the
name is contained within a string i.e
controlsEmbedda bleEditor="myco ntrolname" can I ctype, Directcast e.t.c if so
how???


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 #2
Many thanks Herfried.

Regards,
Merlin
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:2g******** ****@uni-berlin.de...
* "Merlin" <im*****@hotmai l.com> scripsit:
i.e. I have 14 controls that have a name which is a permutation of 4
characters, my program selects which control to use by a 4 character name which matches a control name (it's a zillion times more complex than this but for examples sake). my trouble is I know the name of the control to use, and the control is contained on the form, but how do I reference it, as the name is contained within a string i.e
controlsEmbedda bleEditor="myco ntrolname" can I ctype, Directcast e.t.c if so how???
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.../vbtchCreating
ControlArraysIn VisualBasicNETV isualCNET.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 #3

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

Similar topics

2
10526
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server Error in '/Cr_Dataset' Application. ----------------------------------------------------------- ---------------------
4
2673
by: Frawls | last post by:
Hi, I get the following error when trying to run a search on my aspx site, this error only occours if the product im searching for does not exist. Can anybody explain this please and help me with a solution, i know its probably simple but im new to the game...Cheers ----------------------------------------------------------------------- ...
1
3009
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have access to the methods and properties from the Page_Load, no problem there. But as soon as I want access to the user control from another procedure on...
3
1746
by: jm | last post by:
Dim order_type As DropDownList = _ (CType(e.Item.FindControl("order_type"), DropDownList)) This control is in a datalist itemtemplate. It will not find it. Whenever I try and do anything further with this, it says: Object reference not set to an instance of an object. My guess is that that means it cannot find the control on the form.
6
6107
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page then tried to access 2nd, or 3rd, etc page. The problem is it happens sometimes - sometimes when they clicked refresh button, then everything is ok....
7
3179
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb -- UC/ ----- Classic/
3
2434
by: Brano | last post by:
HI all, I have a problem i have a web application that was working fine and this morning when i run it and click on a button that does Reponse.Redirect to a page i get this error : Server Error in '/IPFWEb_6' Application. -------------------------------------------------------------------------------- Object reference not set to an...
2
2567
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a good while and I'm getting really frustrated now! My problem is this - my custom controls periodically disappear from my
2
2202
by: Jeff | last post by:
I'm getting an Object Reference error before I even run my app, and I'm not sure where to look to find the cause. I'd appreciate your help. When I open my Windows Application project, the following Microsoft Development Environment error message displays: "Object reference not set to an instance of an object." Then when I access the...
9
1401
by: =?Utf-8?B?VG9tbXkgTG9uZw==?= | last post by:
I don't know if the following is what you are looking for, but to me what you described was using a control array. If you were using vb6 that would be easy, the following articles hopefully explain using control arrays in .net Hope they help? http://visualbasic.about.com/od/usingvbnet/l/bldykctrlarraya.htm ...
0
7475
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...
0
7409
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...
0
7664
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. ...
0
7918
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...
1
7436
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7766
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...
0
3463
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...
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.