Dynamically addressed Fields

billvb

New member
Joined
Sep 23, 2004
Messages
2
Programming Experience
1-3
I have a vb.net form it has 25 Fields named Question1 thru Question25 I want to put text into each one based on data from a query read. What I need to know is how do I address the field name to assign the text.



ie

for x = 1 to 25

question & x .text = dtr("thequestion")

next



Any ideas would be helpful.







PS programmers have strong masochistic tendencies
icon11.gif
 
If you are using labels or textboxes, you could put them up as arrays, even two-dimensional if you wish. Then you can manipulate them within you procedure and address them by their subscripts (0-24).

Basically, you dimension or redimension them as an array, such as:
ReDim lblSQ(intOrder, intOrder)

then you create each label to be attached to the array:
lblSQ(intI, intJ) = New Label()

Finally, you add each of them to the container (panel, etc) that you are displaying the information:
panDisplay.Controls.Add(lblSQ(intI, intJ))

If you would like to see an example, you could visit the following site:

http://www.mathpath.uni.cc

Look under magic squares and on that page, you can download the project files of the program, including the .vb file.

Happy programming.
 
Thanks for the help, but the problem I'm having and would still have is how do I turn the code:

txtQuestion & n .text = bla bla bla

back into a textbox so VB knows I'm talking about a control.


Thanks Again
Bill
 
If one uses txtQuestion1.text, txtQuestion2.text, ..., txtQuestionn.text, explicit code is required for each value of n. Unfortunately the proposed syntax of (txtQuestion & n).text will NOT work with VB.NET.

Instead, if the 25 textboxes are declared as an array, you could process them with the same code, as you wished to do, for example:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim txtQ(25) As TextBox
Dim i As Integer
For i = 0 To 24
txtQ(i) = New TextBox()
Controls.Add(txtQ(i))
txtQ(i).Top = Math.Floor(i / 5) * 30
txtQ(i).Left = (i Mod 5) * 50
txtQ(i).Width = 45
txtQ(i).Text = "Box " & i.ToString() ' text according to your needs
Next
End Sub

The above code is a reproduction of the code found at the following site:http://www.mathpath.uni.cc

where an illustration of the output is displayed also.
Look for vb.net and find the example ARRAY OF CONTROLS.

Happy programming.
 
Last edited:
Back
Top