Solved

Finding a form object from a name in a string.

Posted on 2004-08-04
7
149 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
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 85

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 85

Expert Comment

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

Idle_Mind
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now