Read a textbox by its ID

TheWizardofInt

Well-known member
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
I need to be able to update and access over 100 text boxes on a form by their ID.s

So that, if the Text Box is txtOne, I need to be able to update its text with SomeControl("txtOne").text, or however it works

Thanks
 
Hmm.. I dont know about IDs or anything, but perhaps I can offer a work around?

How about having a 100 element TextBox array? That way you can reference them via the array by number;

tbArray[0] = txtOne;
tbArray[1] = txtTwo;

tbArray[0].Text = "mr. one!";
tbArray[1].Text = "mr. two!";

etc..
 
The names of the textboxes are matched to a recordset

Rather than writing hundreds of lines of code, I am trying to pull the names of the fields in the recordset and match them to the form, so that I can populate the form or the recordset with a single loop.
 
Im not sure what you need but you can try this. Go through every textbox and check its name to see if it matches something.
Code:
Dim txt as TextBox
For each txt in Me.Controls
         if txt.Name = something then
                do something
         end if
next
Again, im not sure what you want so this might not help :)
 
It may be giving you an error becuase your text boxes do not have names. You must specifically name the textboxes using the name property of the control. The MS designer does that automatically.

Or you could like wyrd said use an array of your textboxes. Only references to those textboxes are stored, so you would not be losing performace. That is the way I have done one of my programs that has 12 textboxes.
 
It seems to not be working because you have to drill down past the primary label into forms, and from the forms into tables, and from there into the text boxes.
 
Each control on your form is indexed. The index is based on when it is placed on the form.
Heres a block of code that allows me to hide a group of comboboxes based on a user selected parameter.
The program is a payroll program that allows for one week or two weeks of days (as well as weekends or no weekends) in the form of comboboxes that allow an employee to enter their clock in and clock out times.
This block of code:
Code:
 check is payroll interval two weeks?
        payrollinterval = CBool(dataset3.Tables("Company").Rows.Item(0)("PayrollIntervalTwoWeeks"))
        If Not (payrollinterval) Then
            Dim intindex As Integer
            For intindex = 0 To 40
                Me.Controls(intindex).Visible = False hide controls with index 0 through40 i.e. the second two week block
            Next
        End If
does the job.
You can of course, loop through the controls in a variety of other ways including (perhaps most appropriately for you) by control type:
Code:
Dim cntrl As Control
        For Each cntrl In Me.Controls
            If cntrl.GetType.ToString = "System.Windows.Forms.TextBox" Then
                do something
            End If
        Next

As per mutants suggestion, the controls on your form are included in the controls collection and can be accessed via the For Each loop above.

If you know the controls by control name, you can do the following:
Code:
Dim cntrl as Control
For Each cntrl In Me.Controls
      If cntrl.Name.ToString = "someName" Then
        do something
      End If
Next

Just some options to start your exploration.

Jon
 
Originally posted by TheWizardofInt
The names of the textboxes are matched to a recordset

Rather than writing hundreds of lines of code, I am trying to pull the names of the fields in the recordset and match them to the form, so that I can populate the form or the recordset with a single loop.

Hmm.. how about a hash table instead of an array?
tbHash["someField"].Text = "whatever";
 
I ended up using this code:

Code:
                If c.Controls.Count > 0 Then
                    For i = 0 To c.Controls.Count - 1
                        s = c.GetType.ToString
                        sName = c.Controls.Item(i).GetType.ToString
                        If sName = "System.Web.UI.WebControls.TextBox" Then
                            sField = RTrim(c.Controls.Item(i).ID)
                            If Not myDataReader.IsDBNull(sField) Then
                                CType(c.Controls.Item(i), TextBox).Text = myDataReader.GetString(sField)
                            End If
                        End If
                        If c.Controls.Item(i).Controls.Count > 0 Then
                            For j = 0 To c.Controls.Item(i).Controls.Count - 1
                                sName = c.Controls.Item(i).Controls.Item(j).GetType.ToString
                                If sName = "System.Web.UI.WebControls.TextBox" Then
                                    If Not myDataReader.IsDBNull(sField) Then
                                         CType(c.Controls.Item(i).Controls.Item(j), TextBox).Text = myDataReader.GetString(sField)
                                    End If
                                End If
                            Next j
                        End If
                    Next i
                End If
            Next c

Which finds every text box on the form

The problem now is that it is taking the text box names and is unable to match them back to the database. I thought you could pass the name of the field instead of its number.
 
Last edited by a moderator:
Now we have a different problem. Is myDataReader really a datareader? How would you feel about using a dataset instead?

Jon

edit: the other problem, Im an old guy...time to sleep.
 
Yep, moved it to a datareader, and it works great.

For anyone else who wants it:

Code:
        load the fields
        Dim c As Control
        Dim s As String
        Dim i As Integer
        Dim t As TextBox
        Dim sName As String
        Dim sField As String
        Dim j As Integer
        Dim strSQL As String = "Select * FROM gmfields WHERE ID=1"
        Dim MyConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
            Server.MapPath("db/Source.mdb") & ";")
        Dim cmd As New OleDb.OleDbCommand()
        Dim ds As DataTable = New Data.DataTable()
        Dim dc As Data.DataColumn

        cmd.Connection = MyConnection

        cmd.CommandText = strSQL
        Dim datareader As OleDb.OleDbDataAdapter
        datareader = New OleDb.OleDbDataAdapter(cmd)

        datareader.Fill(ds)
        With ds.Rows(0)
            For Each c In Controls
                            If c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") Then
                If c.Controls.Count > 0 Then
                    For i = 0 To c.Controls.Count - 1
                        s = c.GetType.ToString
                        sName = c.Controls.Item(i).GetType.ToString
                        If sName = "System.Web.UI.WebControls.TextBox" Then
                            sField = RTrim(c.Controls.Item(i).ID)
                            CType(c.Controls.Item(i), TextBox).Text = .Item(sField).ToString
                        End If
                        If c.Controls.Item(i).Controls.Count > 0 Then
                            For j = 0 To c.Controls.Item(i).Controls.Count - 1
                                sName = c.Controls.Item(i).Controls.Item(j).GetType.ToString
                                If sName = "System.Web.UI.WebControls.TextBox" Then
                                    CType(c.Controls.Item(i).Controls.Item(j), TextBox).Text = .Item(sField).ToString
                                End If
                            Next j
                        End If
                    Next i
                End If
            Next c
        End With

        datareader.Dispose()
        cmd.Dispose()
        MyConnection.Close()
 
Last edited by a moderator:
I couldnt help noticing you are converting typenames to strings and comparing them. There is no need for this, use the following simple syntax instead:

Code:
If TypeOf c Is Textbox Then

etc
 
Any idea how to do this in reverse, now?

I would think that:

Code:
sValue = CType(c.Controls.Item(i), TextBox).Text

Would put the text in the box into sValue, which is a string, but it always returns nothing
 
Last edited by a moderator:
Then there is nothing in the textbox. That is the correct way of doing it.
 
I dont have vs2003 yet but my understanding of the control array capability that is supposed to be included would make this whole problem even easier at design time.
Anybody with experience using the design time control arrays yet?

Jon
 
Originally posted by divil
Then there is nothing in the textbox. That is the correct way of doing it.

Doesnt work, tho, and I have all of the (170) fields on the form filled in.

Here is the specific code:

Code:
     For Each c In Controls
            If c.Controls.Count > 0 Then
                For i = 0 To c.Controls.Count - 1
                    s = c.GetType.ToString
                    sName = c.Controls.Item(i).GetType.ToString
                    If sName = "System.Web.UI.WebControls.TextBox" Then
                        sField = RTrim(c.Controls.Item(i).ID)
                        sValue = CType(c.Controls.Item(i), TextBox).Text
                        sSQL = sSQL & sField & "=" & RTrim(sValue) & ", "
                    End If
                    If c.Controls.Item(i).Controls.Count > 0 Then
                        For j = 0 To c.Controls.Item(i).Controls.Count - 1
                            sName = c.Controls.Item(i).Controls.Item(j).GetType.ToString
                            If sName = CType(c.Controls.Item(i).Controls.Item(j), TextBox).Text() Then
                                sField = RTrim(c.Controls.Item(i).Controls.Item(j).ID)
                                sValue = CType(c.Controls.Item(i).Controls.Item(j), TextBox).Text
                                sSQL = sSQL & sField & "=" & RTrim(sValue) & ", "
                            End If
                        Next j
                    End If
                Next i
            End If
        Next c
It finds every textbox but it thinks they are all ""

And thanks for all of the help you guys give.
 
Last edited by a moderator:
Ive knocked you a example of loading and save/load from textbox controls in VB.Net have a look if you need more help just ask :)

Andy
 

Attachments

Last edited by a moderator:
Back
Top