Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Easy Question (I Think) regarding accessing object properties

Posted on 2006-06-17
5
Medium Priority
?
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 500 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

609 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