Solved

Finding a form object from a name in a string.

Posted on 2004-08-04
7
154 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

695 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