TheWizardofInt Posted June 6, 2003 Posted June 6, 2003 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 Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
wyrd Posted June 6, 2003 Posted June 6, 2003 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.. Quote Gamer extraordinaire. Programmer wannabe.
TheWizardofInt Posted June 6, 2003 Author Posted June 6, 2003 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. Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
*Experts* mutant Posted June 6, 2003 *Experts* Posted June 6, 2003 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 :) Quote
TheWizardofInt Posted June 6, 2003 Author Posted June 6, 2003 If you try that code it doesn't work It gives you a class error Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
aewarnick Posted June 6, 2003 Posted June 6, 2003 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. Quote C#
TheWizardofInt Posted June 6, 2003 Author Posted June 6, 2003 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. Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
*Experts* jfackler Posted June 6, 2003 *Experts* Posted June 6, 2003 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 Quote
wyrd Posted June 6, 2003 Posted June 6, 2003 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"; Quote Gamer extraordinaire. Programmer wannabe.
TheWizardofInt Posted June 6, 2003 Author Posted June 6, 2003 (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 November 26, 2005 by PlausiblyDamp Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
*Experts* jfackler Posted June 6, 2003 *Experts* Posted June 6, 2003 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. Quote
TheWizardofInt Posted June 6, 2003 Author Posted June 6, 2003 (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 November 26, 2005 by PlausiblyDamp Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
*Gurus* divil Posted June 6, 2003 *Gurus* Posted June 6, 2003 I couldn't help noticing you are converting typenames to strings and comparing them. There is no need for this, use the following simple syntax instead: If TypeOf c Is Textbox Then etc Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
TheWizardofInt Posted June 6, 2003 Author Posted June 6, 2003 (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 November 26, 2005 by PlausiblyDamp Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
*Gurus* divil Posted June 6, 2003 *Gurus* Posted June 6, 2003 Then there is nothing in the textbox. That is the correct way of doing it. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* jfackler Posted June 6, 2003 *Experts* Posted June 6, 2003 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 Quote
TheWizardofInt Posted June 6, 2003 Author Posted June 6, 2003 (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 November 26, 2005 by PlausiblyDamp Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
*Gurus* divil Posted June 6, 2003 *Gurus* Posted June 6, 2003 jfackler: There are no design time control arrays in VS.NET 2003. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
a_jam_sandwich Posted June 6, 2003 Posted June 6, 2003 (edited) 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 :) Andycontrol looping.zip Edited November 26, 2005 by PlausiblyDamp Quote Code today gone tomorrow!
*Experts* jfackler Posted June 6, 2003 *Experts* Posted June 6, 2003 Divil, Old post, but do you remember this discussion? I guess I was swayed by the earnestness of steved's belief that the control arrays were going to be part of the framework. http://www.xtremedotnettalk.com/showthread.php?s=&threadid=70941&perpage=5&pagenumber=3 Should believe only what I see I suppose. Comments? Jon Quote
*Gurus* divil Posted June 6, 2003 *Gurus* Posted June 6, 2003 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 :) Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.