Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
1.00/5 (1 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

You could use a for each statment block

VB
For each txt as control in me.controls
 if TypeOf txt is Textbox Then
   txt.Text = string.empty
  End if
Next
 
Share this answer
 
VB
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
         ClearTextboxes(ctrl.Controls)
      end if
   next 
end sub


Then just call it from your code like this
VB
....
ClearTextboxes(me.Controls)


This method uses recursion to make sure all textboxes are cleared even if they are inside a container control.
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
Jigar Sangoi 21-Jan-13 11:27am    
can you give me code because i tried but i am getting error
fjdiewornncalwe 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.
fjdiewornncalwe 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.
you need to typecast textbox then it works in vb.net
instead of writing
VB
ctrl.Text = String.Empty
you need to write
VB
CType(ctrl, TextBox).Text = String.Empty
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900