Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2004

Tip: Looking for answers? Try searching our database.

Controls Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gigaworld - 29 Jun 2004 12:52 GMT
I have this PROBLEM with controls collection:

The old VB 6.0 syntax was:

MyStr = me.controls("button1").text

VB .NET doesn't function in this way, because in .NET
I must know the index of the control! I need to use the
STRING NAME of the control to address the object, because
my target is doing something like this:

for i = 1 to 10
    MyStr(i) = me.controls("button" & i ).text
next i

if I use this:

MyStr = me.controls(IndexOf(button1)).text

I have again the same problem: cannot use the STRING NAME
of the object!
Herfried K. Wagner [MVP] - 29 Jun 2004 18:38 GMT
* "Gigaworld" <servizi@gigaworld.it> scripsit:
> I have this PROBLEM with controls collection:
>
[quoted text clipped - 17 lines]
> I have again the same problem: cannot use the STRING NAME
> of the object!

Please don't multipost!

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/vbtchCreatingControlAr
raysInVisualBasicNETVisualCNET.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.

Signature

Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.