Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB.NET
|
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 27th, 2004, 05:52 PM
Authorized User
 
Join Date: Jan 2004
Posts: 34
Thanks: 0
Thanked 1 Time in 1 Post
Default Using loop to change text of multiple textboxes

I have a few textboxes named textbox1, textbox2, etc. Is there a way to create a loop to change the text in each text box to the same thing by incrementing the number after textbox.
 
Old July 27th, 2004, 10:11 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You could using the FindControl("controlname") method. Build the control from "textbox" and the loop counter.
 
Old July 28th, 2004, 09:46 AM
Authorized User
 
Join Date: Jan 2004
Posts: 34
Thanks: 0
Thanked 1 Time in 1 Post
Default

I tried using FindControl() but I am getting the error Name 'FindControl' is not declared. I even copied the example from the help file and got the same error.
 
Old July 28th, 2004, 10:43 AM
Authorized User
 
Join Date: Jan 2004
Posts: 34
Thanks: 0
Thanked 1 Time in 1 Post
Default

The code i am using is:
Code:
  Dim textboxNumber As Control
  Dim x As Integer = 1
  For x = 1 To 9
     textboxNumber = FindControl("textbox" + x)
     textboxNumber.Text = ""
  Next
 
Old July 28th, 2004, 11:47 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I responded prematurely to this thread. Are you working in a windows form? Sounds like it. That method applies to web forms. I'm not sure how you'd do this in a windows form apart from putting all the textboxes into some collection or control container so you can loop thru all of that object's members.
 
Old July 28th, 2004, 02:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Try:

_______________________________________

Dim ctrl As Control
For Each ctrl In Me.Controls
   If (ctrl.GetType.ToString = "System.Windows.Forms.TextBox") Then
         CType(ctrl, TextBox).Text = "All textbox text is the same."
   End If
Next

________________________________________

This should get you on the right track. Modify it according to what exactly you want. With this you don't need the number, it just looks to see if the control is a textbox.

The above example is for the entire form, but you can do this for panels, groupboxes, etc., anything that is a container.

J
 
Old July 29th, 2004, 08:54 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Also consider the recursion factor. If you encounter a control that contains controls, you'll need to dig into that as well. So you should write this logic into a method that can call itself if it finds controls within a control's controls collection.





Similar Threads
Thread Thread Starter Forum Replies Last Post
textboxes or list boxes with multiple fore color svharishtech Pro VB 6 5 April 20th, 2007 05:04 PM
Help: character and multiple textboxes snoopy_sweden Javascript How-To 0 February 20th, 2007 04:08 AM
COLOR CHANGE IN TEXTBOXES Rashad Javascript How-To 5 November 28th, 2005 06:06 AM
Validate Multiple Textboxes ~Bean~ ASP.NET 1.0 and 1.1 Professional 2 October 11th, 2005 03:03 PM
Replace textboxes or textareas with some text amit_bhatia Beginning PHP 1 December 7th, 2004 11:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.