Solved

Easy Question (I Think) regarding accessing object properties

Posted on 2006-06-17
5
193 Views
Last Modified: 2010-04-23
Hi All,

I have a button created existing on a form

I can access the button using the following...

form.button.name = "name"
form.button.text = "text"

and so on.

What I would like to do is give the button a name...lets say btnExample so now the button is called btnExample.  I can access the button by:
form.btnExample.text = text

But what if I wanted to store btnExample in a variable lets say

Dim  buttonName = "btnExample"

and then access the contents of that buttons text property using the name of the button stored in the variable...How would one do that.

For example:  I would like to be able to type form.(variableName).text = "text"

Tried finding example of this, etc but not having any luck.

Thanks
0
Comment
Question by:mike_allain
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 16926722
You can try a couple of things.  First, implementing a control array (explained here:  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchcreatingcontrolarraysinvisualbasicnetvisualcnet.asp ).  Or you can do a simple loop through all the controls on your form to find the right one.  Obviously, if you don't have a lot of controls, the second option is the simpler one.  Here's an example:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim btnName As String
        Dim controlButton As Control

        btnName = "btnExample"

        If GetControlFromName(controlButton, btnName) Then controlButton.Text = "text"
    End Sub

    Private Function GetControlFromName(ByRef control As Control, ByVal strControlName As String) As Boolean
        For Each ctrl As Control In Me.Controls
            If UCase(ctrl.Name) = UCase(strControlName) Then
                control = ctrl
                Return True
            End If
        Next
        Return False
    End Function
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16926726
What version VB.Net?
0
 

Author Comment

by:mike_allain
ID: 16926757
Visual studio 2005 ver 8

I am currently using the loop method.  I was hoping that there might be ome way I could use a varaible instead as I have many text boxes on my form.



0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 16926823
With VB.Net 2005 there is a new Find() function in the Controls collection:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find.aspx

So something like...

        Dim ctl() As System.Windows.Forms.Control
        ctl = Me.Controls.Find("btnExample", True)

        If ctl.Length > 0 Then
            If TypeOf ctl(0) Is Button Then
                Dim btn As Button = CType(ctl(0), Button)
                btn.Text = "You found me..."
            End If
        End If


0
 

Author Comment

by:mike_allain
ID: 16927396
Thank you very much, this is exactly what I have been looking for. Much better than the looping that I have been doing.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now