Solved

Finding a form object from a name in a string.

Posted on 2004-08-04
7
153 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

749 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