Results 1 to 8 of 8

Thread: Code ? Looping through controls

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Edmonton
    Posts
    23

    Code ? Looping through controls

    Code:
        
        Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reset.Click
    
            Dim tb As System.Web.UI.WebControls.TextBox
    
            For Each tb In Page.Controls
                tb.Text = ""
            Next
    
        End Sub
    Hi everybody I have a reset button on a form and I want to clear all the textboxes on the form when you hit reset obviously... I don't want to have to go
    textbox1.text = ""
    textbox2.text = ""
    textbox3.text = ""
    a zillion times but the code above keep giving me errors anybody have any ideas as to what I'm doing wrong here...
    Thanks for your help in advance

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi Agent
    I am not up to speed on web controls but if it is anything like normal form controls then...
    VB Code:
    1. Private Sub Command1_Click()
    2. 'This works
    3.     Dim lControl As Control
    4.     For Each lControl In Me.Controls
    5.         If TypeOf lControl Is TextBox Then
    6.             lControl.Text = "Me"
    7.         End If
    8.     Next
    9. End Sub
    10.  
    11. Private Sub Command2_Click()
    12. 'This doesnt.. similar to ur code
    13.     Dim lControl As TextBox
    14.     For Each lControl In Me.Controls
    15.         lControl.Text = "Me"
    16.     Next
    17. End Sub
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Edmonton
    Posts
    23

    Thanks Beachbum

    Thanks Beachbum for the reply... I knew how to do it in vb but I'm totally stumped with how to do it using VB.Net I tried all sorts of combinations of some of your posted code...

    Well I've made a little progress I managed to count all the textboxes on a WinForm with the following code
    Woopee

    VB Code:
    1. Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reset.Click
    2.  
    3.         Dim i As Integer
    4.         Dim x As Integer
    5.  
    6.         Try
    7.             For i = 0 To form1.Controls.Count - 1
    8.                 If TypeOf form1.Controls.Item(i) Is TextBox Then
    9.  
    10. ' I need to somehow access each textbox in here
    11. ' it would be ideal if I could do something like this
    12. ' form1.Controls.Item(i).Text = ""
    13. ' but I can't
    14.  
    15.                     x += 1
    16.                     Label1.Text = "There are " & x & " textboxes on your form"
    17.                 End If
    18.             Next
    19.         Catch er As Exception
    20.             Label1.Text = er.Message
    21.         End Try
    22.     End Sub
    So Please respond if you have any more suggestions this is driving me nutz

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Sorry Agent
    Silly me didnt even notice that this post was in .NET forum. So, having never seen .Net, i cant offer any help. Good luck
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  5. #5
    Junior Member
    Join Date
    Aug 2001
    Posts
    17

    Hi AgentBxb

    Here is the code to clear all control in windows application of VB.NET

    Sub ClearText(ByVal ThisForm As System.Windows.Forms.Form)
    Dim ThisControl As System.Windows.Forms.Control
    For Each ThisControl In ThisForm.Controls
    ThisControl.Text = ""
    Next ThisControl
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ClearText(Me)
    End Sub

    Let me know if it really helpful

    kumar

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Edmonton
    Posts
    23

    Hi Kumars

    Hey thanks for the code did you get that working in a standard web application cause I'm trying to do it in a Asp.Net Web Application I think it's a little different, cause I couldn't get it working so I just quite trying to be fancy and went with
    textbox1.text = ""
    textbox2.text = ""
    textbox3.text = ""
    Not as elegant but oh well what can you do

  7. #7
    Junior Member
    Join Date
    Aug 2001
    Posts
    17

    Hi

    May i get ur email id

    kumars

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Edmonton
    Posts
    23

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width