Resetting all text boxes

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Resetting all text boxes

Post by NYIntensity »

I'm trying to use a button's 'click' event to reset all the text boxes on my form. I've tried many iterations of the following:

Code: Select all

 Dim txt As Control
 For Each txt In Me.Controls

  If TypeOf txt Is TextBox Then txt.Text = ""

 Next
It doesn't do anything. If I substitute the clearing action with MsgBox("test"), I still don't get anything, so I feel like for some reason the code isn't actually looking at the collection of controls on the form, but I don't know how to fix it. I could simply list the 27 text boxes and set them all = Nothing, but that's bad practice.

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Resetting all text boxes

Post by StuartR »

What output do you get if you add the line

Code: Select all

Msgbox Me.Controls.Count
at the beginning of your code.
StuartR


NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Resetting all text boxes

Post by NYIntensity »

6. It's seeing my 3 group boxes and 3 command buttons, AFAICT (those are the only other controls on the form besides text boxes).

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Resetting all text boxes

Post by StuartR »

That is certainly interesting, but I have no idea why it doesn't see all the controls on the form. Are you able to post a cut down version of your application that includes this form?
StuartR


NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Resetting all text boxes

Post by NYIntensity »

What's the attachment size limit? And do you have Visual Studio 2008?

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Resetting all text boxes

Post by StuartR »

NYIntensity wrote:What's the attachment size limit?
256KB
NYIntensity wrote:And do you have Visual Studio 2008?
I don't, but hopefully one of our friendly and helpful loungers will have.
StuartR


NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Resetting all text boxes

Post by NYIntensity »

Project attached :)
You do not have the required permissions to view the files attached to this post.

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Resetting all text boxes

Post by rory »

I suspect your type check is failing. Try:

Code: Select all

Dim txt As Control
For Each txt In Me.Controls

  If TypeName(txt) = "TextBox" Then txt.Text = ""

Next
Regards,
Rory

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Resetting all text boxes

Post by NYIntensity »

That doesn't make sense; just because when I count ALL controls, it only counts 6; there are 33 controls on the form.

I gave it a shot anyways; it still didn't work. :hairout:

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Resetting all text boxes

Post by rory »

I will have a look at the project in VS2010 at lunch if I have time.
Regards,
Rory

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Resetting all text boxes

Post by NYIntensity »

I would definitely appreciate it, this is really confusing. I think it may have something to do with the relationship of the text boxes and the group boxes. In fact, I'm going to remove the frames and just use labels...see if that fixes it.

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Resetting all text boxes

Post by NYIntensity »

That was it; as soon as I removed them from the group boxes, all was well.

BigKev
StarLounger
Posts: 78
Joined: 10 Feb 2010, 12:54
Location: Jeddah, Saudi Arabia

Re: Resetting all text boxes

Post by BigKev »

The GroupBoxes are containers and hence have their own Controls Collection. The Contols in those Collections are not 'officially' part of the Controls collection of the Form.

To do this you need something like this:

Private Sub ResetTextBoxes(ByVal pControl As Control)

For Each c As Control In pControl.Controls
If TypeOf c Is Windows.Forms.TextBox Then
' Reset code goes here
Else
If c.Controls.Count <> 0 Then
ResetTextBoxes(c)
End If
End If
Next

End Sub

Assuming the name of the form if myForm then the call would be:

ResetTextBoxes(myForm)

Regards,
Kevin