?
Solved

Finding a form object from a name in a string.

Posted on 2004-08-04
7
Medium Priority
?
155 Views
Last Modified: 2010-05-02
This is a vb.net question.

I have a few Labels in my form and if anyone of these Labes change I want to change an associated Label.

Like this:

label one
label onep
label two
label twop
label three
label threep

Then I have a function that handels the change of label one, two and three. In this function I do:

Dim ctrlToChange As String = sender.Name & "p"

and now I wish to get the object onep or twop or threep from the name I have stored in ctrlToChange.

How to do?

Thank you.

Anders Liden
Coder
www.dvdnorden.se

0
Comment
Question by:ExpertsAL
[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
7 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11713861
I think you may be better off using control arrays for this one.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 11713922
I don't believe that VB.NET does control arrays, but I'm not sure.

This is the VB area. There is a separate one for VB.NET. You might stand a better chance there.


0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 11713945
Incidentally in VB, you can address Form controls by their names:

Form1.Controls("Label1").Caption = "New Caption"
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 11714331
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11714551
In VB.NET:

  Public Function OpenForm(ByVal FormName As String) As Form

    Dim oForm As Form = CType(Activator.CreateInstance(Type.GetType(FormName, True, True)), Form)
    oForm.StartPosition = FormStartPosition.CenterScreen
    oForm.Show()

    Return oForm

  End Function

Bob
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 11714903
The code below will cause the counter in each label to be incremented as it is clicked.  When the label text changes, the corresponding label text will also change:


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        one.Text = "0"
        two.Text = "0"
        three.Text = "0"
    End Sub

    Private Sub Label_Clicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles one.Click, two.Click, three.Click
        Dim count As Integer = Integer.Parse(sender.text)
        count = count + 1
        sender.text = count
    End Sub

    Private Sub Label_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles one.TextChanged, two.TextChanged, three.TextChanged
        Dim assLabelName As String = sender.name & "p"
        Dim curControl As Control

        For Each curControl In Me.Controls
            If curControl.Name.Equals(assLabelName) Then
                curControl.Text = sender.text
                Exit For
            End If
        Next
    End Sub

Regards,

Idle_Mind
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11751877
Why a grade of C ExpertsAL?

Idle_Mind
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month15 days, left to enroll

770 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