Solved

Easy Question (I Think) regarding accessing object properties

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

713 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