Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

Posted

Hmm.. I don't 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..

Gamer extraordinaire. Programmer wannabe.
Posted

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.

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

  • *Experts*
Posted

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.

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 :)

Posted

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.

C#
  • *Experts*
Posted

Each control on your form is indexed. The index is based on when it is placed on the form.

Here's 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:

'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:

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:

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

Posted
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";

Gamer extraordinaire. Programmer wannabe.
Posted (edited)

I ended up using this 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.

Edited by PlausiblyDamp

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

  • *Experts*
Posted

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, I'm an old guy...time to sleep.

Posted (edited)

Yep, moved it to a datareader, and it works great.

 

For anyone else who wants it:

 

       '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()

Edited by PlausiblyDamp

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

Posted (edited)

Any idea how to do this in reverse, now?

 

I would think that:

 

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

Edited by PlausiblyDamp

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

  • *Experts*
Posted

I don't 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

Posted (edited)
Then there is nothing in the textbox. That is the correct way of doing it.

 

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

 

Here is the specific 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.

Edited by PlausiblyDamp

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

  • *Gurus*
Posted

I remember the discussion well. I know how the design-time architecture works, and I knew control arrays at design time were a near impossibility because of it.

 

I think I just left him to believe what he did after asserting so firmly that they would be included. I guess he made an angry call to ms after all :)

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...