Solved

Finding a form object from a name in a string.

Posted on 2004-08-04
7
150 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

911 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

17 Experts available now in Live!

Get 1:1 Help Now