Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. looping through textboxes

looping through textboxes

Scheduled Pinned Locked Moved ASP.NET
help
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hounetdev
    wrote on last edited by
    #1

    Ok, I am trying to loop through 6 textboxes to check,if they are blank replace with zero. i can do this with each one but i want to put it in a loop for efficiancy. here is what i want to do dim i as integer for i=1 to 6 if textbox(i).text ="" then textbox(i).text = 0 end if I get this error 'textbox' is a type and cannot be used as an expression, and it highlights this line - if textbox(i).text="" then Thanks for the help, Santana

    S 1 Reply Last reply
    0
    • H hounetdev

      Ok, I am trying to loop through 6 textboxes to check,if they are blank replace with zero. i can do this with each one but i want to put it in a loop for efficiancy. here is what i want to do dim i as integer for i=1 to 6 if textbox(i).text ="" then textbox(i).text = 0 end if I get this error 'textbox' is a type and cannot be used as an expression, and it highlights this line - if textbox(i).text="" then Thanks for the help, Santana

      S Offline
      S Offline
      sivilian
      wrote on last edited by
      #2

      I do not think there is a way to put textboxes into an array, in a way to loop through them as you are doing. You can however, iterate through the controls on the page: Private Sub TraverseControls(ByVal container As Control) Dim control As control For Each control In container.Controls If control.GetType.Name = "TextBox" Then CType(control, TextBox).Text = String.Empty End If If control.HasControls() Then TraverseControls(control) End If Next End Sub You can call this function with Page, but it will set all your textboxes to an empty string. In order to make good use of it, you can put your 6 textboxes in a Panel, and then call the function with that Panel. hope this helps, sivilian

      C 1 Reply Last reply
      0
      • S sivilian

        I do not think there is a way to put textboxes into an array, in a way to loop through them as you are doing. You can however, iterate through the controls on the page: Private Sub TraverseControls(ByVal container As Control) Dim control As control For Each control In container.Controls If control.GetType.Name = "TextBox" Then CType(control, TextBox).Text = String.Empty End If If control.HasControls() Then TraverseControls(control) End If Next End Sub You can call this function with Page, but it will set all your textboxes to an empty string. In order to make good use of it, you can put your 6 textboxes in a Panel, and then call the function with that Panel. hope this helps, sivilian

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        sivilian wrote: I do not think there is a way to put textboxes into an array Yes, there is. The controls collection is essentially an array of Control objects which is the base class of all the controls, including TextBox. So, it is not that much different to having an array of TextBoxes. I'm a C# developer, so I can't show you exactly in VB.NET, so I'll give you a C# example that assumes there are 6 TextBoxes:

        TextBox[] myTextBoxes = new TextBox[6];
        int i = 0;
        foreach(Control ctrl in container.Controls)
        {
        TextBox tb = ctrl as TextBox;
        if (tb == null)
        continue;
        myTextBoxes[i] = tb;
        i++;
        }

        At the end of the myTextBoxes contains the 6 text boxes, which can be looped over as normal.


        "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups