Solved

How to update textbox value using global string variable as textbox name

Posted on 2008-11-01
4
419 Views
Last Modified: 2012-05-05
I would normally use the following line of code to update the value of a date textbox in a form.......

SpecificForm.DateTextBox.Text = Format(MonthCalendar1.SelectionStart, "MM/dd/yyyy").ToString

BUT, in this particular sub routine I need to be able to update various maskedtextboxes as shown above using a global variable that has the name of the textbox stored in it.  Using a select case I'm able to identify the form that contains the date field, but I need to be able to use the global variable in updating the textbox because there are multiple date fields on various forms....as opposed to one date field.

How can I replace "DateTextBox" with the global variable to accomplish the task?
0
Comment
Question by:TSFLLC
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:bigdaddyz99
ID: 22859819
You should be able to access the control  by using the form and "finding" the control by name.

dim tbname as string = "mytextbox"

Dim tb As TextBox = CType(Me.FindControl(tbname), TextBox)
tb.Text = "mytext"
0
 

Author Comment

by:TSFLLC
ID: 22860772
I see FindForm in the list but I do not see FindControl.

I'm using VB2005 and Framework 2.0.
0
 
LVL 3

Accepted Solution

by:
bigdaddyz99 earned 500 total points
ID: 22862052
Here's what I do...
Add a function to your form to find the control,
then in your code, call it like this for a textbox...

 Dim tb As TextBox
 tb = CType(GetControl("mytextbox"), TextBox)
 tb.Text = "123"
    Public Function GetControl(ByVal cn As String) As Control

        Dim c As Control

        For Each c In Me.Controls

            If c.Name.ToLower = cn.ToLower Then

                Return c

            End If

        Next

        Return Nothing

    End Function
 

     

Open in new window

0
 

Author Comment

by:TSFLLC
ID: 22862800
I already had a similar function set up on most if not all of my forms when it came to a textbox.....only with this one I needed to add new functions on about ten forms for MaskedTextBox 's.

And after doing that, the procedure worked like a charm.

Thanks! for the effort.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

21 Experts available now in Live!

Get 1:1 Help Now