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

how can i make textbox(n).text, how can i control n to write something in there

P: 2
Guys! Please help me! For example, i have a programme. There are 4 textboxes and 1 button. So i write in the first textbox a number 2, 3 or 4 and then in one of the textbox programme writes something (doesn't matter what):

Expand|Select|Wrap|Line Numbers
  1. Dim x As Integer
  3. Sub Button1.Click(....)
  4. x = Val(TextBox1.Text)
  5. TextBox(x).Text = "blahblahblah"
  6. End Sub
I hope i wrote it clear and you understand what i want - i want to control with this "x" in which textbox will i write something, but obviously my code isn't working. Please help!
Oct 25 '14 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 1,208
Assuming you named the controls and know what they are you can simply use the name of the textbox control to modify the value.

If I assume you don't have the names of the textboxes because they are dynamic you can iterate the controls.
Expand|Select|Wrap|Line Numbers
  1.  For Each ctrl as control In form1.Controls
  2.    If (ctrl.GetType() Is GetType(TextBox)) Then
  3.       ctrl.text="blahblahblah"
  4.    end if
  5.  Next
But thirdly if the controls are dynamic you can store references to them in an array when you create them and then modify them that way.
Oct 26 '14 #2

P: 2
Sorry, i didn't get your message fully (because i don't know english perfectly, yeah), but as I understood you're trying to say that i need to use "If". I tried to do it, and, well, it works, but i use to many code on that:

Expand|Select|Wrap|Line Numbers
  1. x = Val(TextBox1.Text)
  2. if x = 2 Then TextBox2.Text = "blahblahblah"
  3. if x = 3 Then TextBox3.Text = "blahblahblah"
  4. if x = 4 Then TextBox4.Text = "blahblahblah"
So I don't like this version and want just to make like "TextBox(x).Text" without "If" or "For", but I don't know how to do it.

Or maybe I just understood you wrong.
Oct 27 '14 #3

Expert 100+
P: 1,208
the for loop I posted is probably going to be you're easiest solution. either that or create an array containing references to the textboxes you want.

I mean if you really wanted TextBox(i).text
Expand|Select|Wrap|Line Numbers
  1.     Dim TextBox As New List(Of TextBox)
  2.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3.         For Each ctrl As Control In Me.Controls
  4.             If (ctrl.GetType() Is GetType(TextBox)) Then
  5.                 TextBox.Add(ctrl)
  6.             End If
  7.         Next
  8.         TextBox(1).Text = "test"
  9.     End Sub
Oct 28 '14 #4

Post your reply

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