Newbie Question 2, Reference To Controls On A Form

  • Thread starter Thread starter 97T
  • Start date Start date
9

97T

Please excuse my ignorance.

In other high level languages I've played with, there were functions that
let you build commands or lines of code.

For instance, you could assemble a string variable, and convert it to a line
of code, then execute the line of code.

That would be one way to do what I want.

Something like (in English),

Set CommandVariable equal to String1 & String2 & String3
Execute CommandVariable

This would make it simple for me to step through an array and assign my
bytes to a series of sequentially numbered textboxes in a form. I could do
a loop and change the value of one part of it.

I've waded into the discussions about control arrays that don't work
anymore, and I got mired in quicksand.

Do I have to create classes and all that just to transfer information to
textboxes without having to type out each one by name in the code? I'd
rather use a for / next loop ......

This would be nice if it worked, :)

(I've placed some text boxes on my form and named them Txtbx0, Txtbx1, etc.)
For I = 0 to 4
Form1.Txtbx(I).Text = MyArray(I)
Next

Thanks for helping the newbie,

--NinerSevenTango--
 
Private myTextBoxesAry As TextBox()

Private Sub Form1_Load(...)...
myTextBoxesAry = New TextBox() {TextBox1, TextBox2, TextBox3}
End Sub

Doesn't get easier than that!... Except of course if the Form Designer did
this for you behind the scenes... (as VB.Classic could do).

Or you can set a reference to Visual Basic Compatibility library... I think
it contains a ControlArray component... though MS discourages the use of
this library as it might not be supported forever.
 
To create an array of text boxes at runtime I use code similar to the
following:

Public cells(4) as TextBox
For j = 1 To 4
cells(j) = New TextBox
cells(j).Name = CStr( j)
cells( j).Width = 40
cells(j).Height = 40
cells(j).Text = "Cell " & CStr(j)
cells(j).Top = 150
cells(j).Left = 200 + j * (cells(j).Width + 2)
cells(j).ForeColor = Color.Black
cells(j).BackColor = Color.LightBlue
cells(j).BorderStyle = BorderStyle.FixedSingle
cells(j).TextAlign = HorizontalAlignment.Center
cells(j).AutoSize = False
AddHandler cells(j).Click, AddressOf Me.TextBoxClick
Me.Controls.Add(cells(j))
Next
 
97T said:
Please excuse my ignorance.

In other high level languages I've played with, there were functions that
let you build commands or lines of code.

For instance, you could assemble a string variable, and convert it to a
line of code, then execute the line of code.

That would be one way to do what I want.

Something like (in English),

Set CommandVariable equal to String1 & String2 & String3
Execute CommandVariable

This would make it simple for me to step through an array and assign my
bytes to a series of sequentially numbered textboxes in a form. I could
do a loop and change the value of one part of it.

I've waded into the discussions about control arrays that don't work
anymore, and I got mired in quicksand.

Do I have to create classes and all that just to transfer information to
textboxes without having to type out each one by name in the code? I'd
rather use a for / next loop ......

This would be nice if it worked, :)

(I've placed some text boxes on my form and named them Txtbx0, Txtbx1,
etc.)
For I = 0 to 4
Form1.Txtbx(I).Text = MyArray(I)
Next

Thanks for helping the newbie,

--NinerSevenTango--

Thank you all for your responses, I will investigate what it takes to do it
by adding the boxes at runtime.

But in my application, I have already placed a good number of them into my
form, because they will always be there. I just want to populate them with
text which will already be in an array, without having to type in the name
of each one in turn in the code. I would rather loop through them if it is
at all possible. Since I will be making a large number of such screens, if
I can't loop through them like I hoped to do, I guess I will generate them
at runtime.

Thanks again for your help,

--NinerSevenTango--
 
Jeff said:
97T,

If I understand what you are trying to do, it seems that you are trying
to add controls at runtime. If thats right then, this article should be
able to give you some direction. Let me know if thats not what you were
trying to do.

http://msdn.microsoft.com/library/d...ontrolslistitemcollectionclassremovetopic.asp

Jeff

Jeff,

Thank you for your response, sir.

This article is about removing items from a listbox control. I'll keep
looking, thanks for trying, maybe you gave me a different link than you were
thinking of?

--NinerSevenTango--
 
97T,

There is almost always a way to do whatever you want to do in .NET. Try the
following code and see if it is what you had in mind. You will need 3
TextBoxes and 1 Button on the form to run it.

The elegant RecurseControls code was copied from an earlier post by Herfried
K. Wagner a couple of days ago in response to a question about Control
Collections. The somewhat less elegant code is mine :-). I often just read
the questions and answers here to pick up tips on how to do things.

-----------------------

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

RecurseControls(Me)

End Sub

Private Sub RecurseControls(ByVal ctr As Control)

Debug.WriteLine(ctr.Name)

If TypeOf (ctr) Is TextBox Then

Dim i As Integer

Dim myTextBoxCounter As Integer = 3

Dim myTextArray As String() = {"Box 1", "Box 2", "Box 3"}

For i = 1 To myTextBoxCounter

If ctr.Name = "TextBox" & i Then

' myTextArray is 0 based

ctr.Text = myTextArray(i - 1)

End If

Next (i)

End If

If ctr.HasChildren Then

For Each c As Control In ctr.Controls

RecurseControls(c)

Next c

End If

End Sub

-----------------------------

Herfried's orignal code:

\\\
Private Sub RecurseControls(ByVal ctr As Control)
Debug.WriteLine(ctr.Name)
If ctr.HasChildren Then
For Each c As Control In ctr.Controls
RecurseControls(c)
Next c
End If
End Sub
..
..
..
RecurseControls(Me)
///

Stan


Stan Smith
ACT! Certified Consultant
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com
 
Thanks, Stan, I think this one will give me what I need to get it done.

I'm off to try it out.

--NinerSevenTango--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top