|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Replacing control arrays in .NETI've just started using vb.net (vs.net 2005) after programming in VB6. I've noticed that control arrays are no longer used and need a little help. In VB6 I would have done this to loop through my recordset and set some values for text boxes on my form: Dim i as integer i = 0 for i = 0 to rstMyRecordSet.Recordcount -1 txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value Next i Obviously I can't do that anymore so can anyone help? I have a form with 30 text boxes. Each textbox is called txtMyTextbox1, txtMyTextbox2, txtMyTextbox3 etc etc to 30. I have a datareader with my data in it and I would like to put the values in it into my text boxes without having to write out txtMyTextbox1.text = etc for all 30. Or maybe a better way I can bind my text boxes to my datareader? Can anyone suggest a way to do it in code? Any help, greatly appreciated. Many thanks Paul On Nov 13, 1:24 pm, Paul <P***@discussions.microsoft.com> wrote:
Show quoteHide quote > Hi everyone, You can use either Me.Controls.Find("txtMyTextBox" & i.ToString()) to> > I've just started using vb.net (vs.net 2005) after programming in VB6. I've > noticed that control arrays are no longer used and need a little help. In VB6 > I would have done this to loop through my recordset and set some values for > text boxes on my form: > > Dim i as integer > i = 0 > for i = 0 to rstMyRecordSet.Recordcount -1 > txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value > Next i > > Obviously I can't do that anymore so can anyone help? > > I have a form with 30 text boxes. Each textbox is called txtMyTextbox1, > txtMyTextbox2, txtMyTextbox3 etc etc to 30. > > I have a datareader with my data in it and I would like to put the values in > it into my text boxes without having to write out txtMyTextbox1.text = etc > for all 30. Or maybe a better way I can bind my text boxes to my datareader? > > Can anyone suggest a way to do it in code? > > Any help, greatly appreciated. > > Many thanks > Paul locate the control by it's name. One other option is to use a Generic dictionary(Of Integer, TextBox) to hold the index number and the reference to the textbox. You could build this dictionary when the form loads and then grab the necessary textbox by key. Let me know if you need some sample code - I'm out of the IDE at the moment so my description may be a bit scrambled. Thanks, Seth Rowe Thanks Seth, that sounds great. Some sample code would be really good, if you
don't mind? Thanks very much, Paul Show quoteHide quote "rowe_newsgroups" wrote: > On Nov 13, 1:24 pm, Paul <P***@discussions.microsoft.com> wrote: > > Hi everyone, > > > > I've just started using vb.net (vs.net 2005) after programming in VB6. I've > > noticed that control arrays are no longer used and need a little help. In VB6 > > I would have done this to loop through my recordset and set some values for > > text boxes on my form: > > > > Dim i as integer > > i = 0 > > for i = 0 to rstMyRecordSet.Recordcount -1 > > txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value > > Next i > > > > Obviously I can't do that anymore so can anyone help? > > > > I have a form with 30 text boxes. Each textbox is called txtMyTextbox1, > > txtMyTextbox2, txtMyTextbox3 etc etc to 30. > > > > I have a datareader with my data in it and I would like to put the values in > > it into my text boxes without having to write out txtMyTextbox1.text = etc > > for all 30. Or maybe a better way I can bind my text boxes to my datareader? > > > > Can anyone suggest a way to do it in code? > > > > Any help, greatly appreciated. > > > > Many thanks > > Paul > > You can use either Me.Controls.Find("txtMyTextBox" & i.ToString()) to > locate the control by it's name. One other option is to use a Generic > dictionary(Of Integer, TextBox) to hold the index number and the > reference to the textbox. You could build this dictionary when the > form loads and then grab the necessary textbox by key. > > Let me know if you need some sample code - I'm out of the IDE at the > moment so my description may be a bit scrambled. > > Thanks, > > Seth Rowe > > On Nov 13, 1:54 pm, Paul <P***@discussions.microsoft.com> wrote:
Show quoteHide quote > Thanks Seth, that sounds great. Some sample code would be really good, if you No problem!> don't mind? > > Thanks very much, > Paul > > "rowe_newsgroups" wrote: > > On Nov 13, 1:24 pm, Paul <P***@discussions.microsoft.com> wrote: > > > Hi everyone, > > > > I've just started using vb.net (vs.net 2005) after programming in VB6. I've > > > noticed that control arrays are no longer used and need a little help. In VB6 > > > I would have done this to loop through my recordset and set some values for > > > text boxes on my form: > > > > Dim i as integer > > > i = 0 > > > for i = 0 to rstMyRecordSet.Recordcount -1 > > > txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value > > > Next i > > > > Obviously I can't do that anymore so can anyone help? > > > > I have a form with 30 text boxes. Each textbox is called txtMyTextbox1, > > > txtMyTextbox2, txtMyTextbox3 etc etc to 30. > > > > I have a datareader with my data in it and I would like to put the values in > > > it into my text boxes without having to write out txtMyTextbox1.text = etc > > > for all 30. Or maybe a better way I can bind my text boxes to my datareader? > > > > Can anyone suggest a way to do it in code? > > > > Any help, greatly appreciated. > > > > Many thanks > > > Paul > > > You can use either Me.Controls.Find("txtMyTextBox" & i.ToString()) to > > locate the control by it's name. One other option is to use a Generic > > dictionary(Of Integer, TextBox) to hold the index number and the > > reference to the textbox. You could build this dictionary when the > > form loads and then grab the necessary textbox by key. > > > Let me know if you need some sample code - I'm out of the IDE at the > > moment so my description may be a bit scrambled. > > > Thanks, > > > Seth Rowe The below contains two methods, the first (cleverly named Method1) is very compact and relies of the Controls.Find method. The second is probably more performant, but requires additional setup and is more verbose. This code assumes you have 4 textboxes on a form named TextBox1, TextBox2, TextBox3 and TextBox4. ////////////////////// Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load InitializeDictionaryForMethod2() '// Uncomment one of the below methods ' Method1() ' Method2() End Sub Private Sub Method1() For i As Integer = 1 To 4 Dim textBox As TextBox = DirectCast(Me.Controls.Find("TextBox" & i.ToString(), True)(0), TextBox) textBox.Text = String.Format("Hello, World. I'm TextBox{0}", i) Next End Sub Private Sub Method2() For i As Integer = 1 To 4 textboxes(i).Text = String.Format("Hello, World. I'm TextBox{0}", i) Next End Sub Private Sub InitializeDictionaryForMethod2() textboxes = New Dictionary(Of Integer, TextBox)() textboxes.Add(1, Me.TextBox1) textboxes.Add(2, Me.TextBox2) textboxes.Add(3, Me.TextBox3) textboxes.Add(4, Me.TextBox4) End Sub Private textboxes As Dictionary(Of Integer, TextBox) End Class ////////////////////// Thanks, Seth Rowe Thanks Seth, that is exactly what I need.
Thank you very much indeed! Paul Show quoteHide quote "rowe_newsgroups" wrote: > On Nov 13, 1:54 pm, Paul <P***@discussions.microsoft.com> wrote: > > Thanks Seth, that sounds great. Some sample code would be really good, if you > > don't mind? > > > > Thanks very much, > > Paul > > > > "rowe_newsgroups" wrote: > > > On Nov 13, 1:24 pm, Paul <P***@discussions.microsoft.com> wrote: > > > > Hi everyone, > > > > > > I've just started using vb.net (vs.net 2005) after programming in VB6. I've > > > > noticed that control arrays are no longer used and need a little help. In VB6 > > > > I would have done this to loop through my recordset and set some values for > > > > text boxes on my form: > > > > > > Dim i as integer > > > > i = 0 > > > > for i = 0 to rstMyRecordSet.Recordcount -1 > > > > txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value > > > > Next i > > > > > > Obviously I can't do that anymore so can anyone help? > > > > > > I have a form with 30 text boxes. Each textbox is called txtMyTextbox1, > > > > txtMyTextbox2, txtMyTextbox3 etc etc to 30. > > > > > > I have a datareader with my data in it and I would like to put the values in > > > > it into my text boxes without having to write out txtMyTextbox1.text = etc > > > > for all 30. Or maybe a better way I can bind my text boxes to my datareader? > > > > > > Can anyone suggest a way to do it in code? > > > > > > Any help, greatly appreciated. > > > > > > Many thanks > > > > Paul > > > > > You can use either Me.Controls.Find("txtMyTextBox" & i.ToString()) to > > > locate the control by it's name. One other option is to use a Generic > > > dictionary(Of Integer, TextBox) to hold the index number and the > > > reference to the textbox. You could build this dictionary when the > > > form loads and then grab the necessary textbox by key. > > > > > Let me know if you need some sample code - I'm out of the IDE at the > > > moment so my description may be a bit scrambled. > > > > > Thanks, > > > > > Seth Rowe > > No problem! > > The below contains two methods, the first (cleverly named Method1) is > very compact and relies of the Controls.Find method. The second is > probably more performant, but requires additional setup and is more > verbose. This code assumes you have 4 textboxes on a form named > TextBox1, TextBox2, TextBox3 and TextBox4. > > ////////////////////// > Public Class Form1 > > Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Load > InitializeDictionaryForMethod2() > > '// Uncomment one of the below methods > > ' Method1() > ' Method2() > End Sub > > Private Sub Method1() > For i As Integer = 1 To 4 > Dim textBox As TextBox = > DirectCast(Me.Controls.Find("TextBox" & i.ToString(), True)(0), > TextBox) > textBox.Text = String.Format("Hello, World. I'm > TextBox{0}", i) > Next > End Sub > > Private Sub Method2() > For i As Integer = 1 To 4 > textboxes(i).Text = String.Format("Hello, World. I'm > TextBox{0}", i) > Next > End Sub > > Private Sub InitializeDictionaryForMethod2() > textboxes = New Dictionary(Of Integer, TextBox)() > > textboxes.Add(1, Me.TextBox1) > textboxes.Add(2, Me.TextBox2) > textboxes.Add(3, Me.TextBox3) > textboxes.Add(4, Me.TextBox4) > End Sub > > Private textboxes As Dictionary(Of Integer, TextBox) > > End Class > ////////////////////// > > Thanks, > > Seth Rowe > > On Nov 13, 1:24 pm, Paul <P***@discussions.microsoft.com> wrote:
Show quoteHide quote > Hi everyone, You can use control arrays in .NET, just not at design time. You have> > I've just started using vb.net (vs.net 2005) after programming in VB6. I've > noticed that control arrays are no longer used and need a little help. In VB6 > I would have done this to loop through my recordset and set some values for > text boxes on my form: > > Dim i as integer > i = 0 > for i = 0 to rstMyRecordSet.Recordcount -1 > txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value > Next i > > Obviously I can't do that anymore so can anyone help? > > I have a form with 30 text boxes. Each textbox is called txtMyTextbox1, > txtMyTextbox2, txtMyTextbox3 etc etc to 30. > > I have a datareader with my data in it and I would like to put the values in > it into my text boxes without having to write out txtMyTextbox1.text = etc > for all 30. Or maybe a better way I can bind my text boxes to my datareader? > > Can anyone suggest a way to do it in code? > > Any help, greatly appreciated. > > Many thanks > Paul to do it completely in code.
clearing dataset or datatable
Force form to foreground? Structures and Delegates and ByRef Arguments Split a large application Using an Excel sheet as a DB table Problem drawing a WMF File VB.NET2005 (and Working in VB6) Picking the right control ShellExecute error Pause doesn't work in vb.net Scroll picture box |
|||||||||||||||||||||||