Solved

Easy Question (I Think) regarding accessing object properties

Posted on 2006-06-17
5
194 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 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

11 Experts available now in Live!

Get 1:1 Help Now