By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Accessing textboxes created in code in vb2010

P: 1
Hi. Relatively new to vb2010 so if this is easy then I apologise in advance.

The following code creates textboxes on a form according to the contents of an array named ProdName.

The array; along with "intCount"; is declared in another form named WNew as "Public Shared".

intCount is used to count the number of products entered in WNew and is therefore used to tell this form how many textboxes to create in the loop.

The "tbName" textboxes contain the information drawn from the ProdName array in WNew - and (miraculously) this works!

Where I'm going with this is that when the blank textboxes "tbCrates" are produced alongside the "tbName" boxes the user enters a figure in these boxes. Ideally I need that entered figure stored in an array; but everything I've tried so far fails because I'm struggling to access coded textboxes.

In an Ideal world, if ProdName(1) is, let's say, "Bike" and the user types in 15, then a "Quantity" array should show 15 in Quantity(1).

Does anyone have a solution? I should stress that this is not for commercial purposes but is purely something I'm working on for myself for personal use and to help me learn how to program.

Expand|Select|Wrap|Line Numbers
  1. Public Shared tb(100) As TextBox
  2.     Dim Crates(100) As String
  3.     Dim n As Integer = 1
  4.     Dim r As Integer = 176
  5.     Dim t As Integer = 1
  6.  
  7.  
  8.  Private Sub WPurchases_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.  
  10.      If WNew.ProdName(1) <> "" Then
  11.             For i = 1 To WNew.intCount
  12.  
  13.  
  14.                 tb(n) = New TextBox
  15.                 tb(n).Name = "tbName" & n.ToString
  16.                 tb(n).Text = WNew.ProdName(n)
  17.                 tb(n).Size = New Size(148, 20)
  18.                 tb(n).Location = New Point(21, r)
  19.                 tb(n).BorderStyle = BorderStyle.Fixed3D
  20.                 Me.Controls.Add(tb(n))
  21.                 tb(t) = New TextBox
  22.                 tb(t).Name = "tbCrates" & n.ToString
  23.                 tb(t).Size = New Size(55, 20)
  24.                 tb(t).Location = New Point(175, r)
  25.                 tb(t).BorderStyle = BorderStyle.Fixed3D
  26.                 Me.Controls.Add(tb(t))
  27.  
  28.                 n += 1
  29.                 t += 1
  30.                 r += 24
  31.  
  32.             Next i
  33.  
  34.         End If
  35.  
  36.     End Sub
Mar 1 '13 #1
Share this Question
Share on Google+
1 Reply


P: 2
I don't see the Quantity() array in your code. Where is the code that moves the value the user enters, in one of the textboxes, into an array?

If I understand your goal correctly, however, what you would need is an array of event handlers (ie TextBox1_TextChanged) matching your array of textboxes - then when the TextChanged event fires on one of them, the handler gets the value and puts it into the Quantity() array. This problem goes away when you use Classes and property viewers to manage the input/storage of information. For your example, you might write a class called "Product" that has the ProductName and Quantity properties, a textbox (withevents), a handler for the text_changed event, and methods for positioning/formatting the textbox and doing something useful with the information. In your form, you would have an array of type Product (or better, a List(of Product)) of instances of that class.
Mar 1 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.