Solved

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

Posted on 2008-11-01
4
427 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

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 When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

25 Experts available now in Live!

Get 1:1 Help Now