Solved

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

Posted on 2008-11-01
4
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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