Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Query not working in client's PC 1 37
Advice on Xojo as a development tool over VB. 4 56
Footer for each row on Gridview 2 21
.NET 2008 VB and C# 6 28
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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