Click here to Skip to main content
12,957,526 members (59,121 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to clear all text box of webpage using vb code and if i make it as function then what parameter should i pass in sub or function
Posted 21-Jan-13 2:47am
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

You could use a for each statment block

For each txt as control in me.controls
 if TypeOf txt is Textbox Then
   txt.Text = string.empty
  End if
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

public sub ClearTextboxes(controls as ControlCollection)
   for each ctrl as Control in controls 
      if TypeOf ctrl is Textbox Then
         ctrl.Text = String.Empty
      else if ctrl.HasControls
      end if
end sub

Then just call it from your code like this

This method uses recursion to make sure all textboxes are cleared even if they are inside a container control.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you are clearing all textbox instances in your page, then you don't need any arguments in the function call. You just need to create a sub Private Sub ClearAllTextboxes () and then loop through all the controls in your page. For every control in your page, check if it is a textbox. If it is you can proceed to set the Text to string.Empty and voila, you have cleared all the textboxes.
Jigar Sangoi 21-Jan-13 11:27am
can you give me code because i tried but i am getting error
Marcus Kramer 21-Jan-13 13:36pm
I pretty much have in my answer already. If you don't know how to deal with the simplest of Sub declarations and the simplest of loops within that then you probably need to study your course notes a little more carefully first. I'm not going to give you a complete code sample because if I do your homework for you, you learn nothing.
Adam R Harris 21-Jan-13 15:17pm
I did end up just giving him a code sample simply because i can understand that the recursion required to check inside containers can be a little intimidating. I do however agree that if the basics of subs and loops is too much then one needs to review notes or take a course a second time.

Just wanted to let you know that my intention is not to undermine you but to assist with the more daunting task of recursion.
Marcus Kramer 21-Jan-13 16:21pm
I would have preferred to have the OP post his code with the error so that we can see him/her actually making an effort to help themselves. I think it's just too easy for people to say "I have an error" in cases like this instead of actually trying. Cheers.
Rate this: bad
Please Sign up or sign in to vote.

Solution 8

you need to typecast textbox then it works in
instead of writing
ctrl.Text = String.Empty
you need to write
CType(ctrl, TextBox).Text = String.Empty
Pete Olsen 19-May-17 11:46am
Thanks for your solution. I searched high and low for two days looking for this... CType huh.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,994
CHill60 3,550
Maciej Los 3,312
ppolymorphe 2,170
Jochen Arndt 1,973

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100