|
-
Oct 17th, 2002, 11:29 PM
#1
Thread Starter
New Member
How do I clear all TEXTBOX controls using For...Next loop in VB.Net
I would like to clear all textbox controls on a form
using a generic For...Next loop - I cannot get this to work using VB.Net
-
Oct 18th, 2002, 04:44 AM
#2
Frenzied Member
VB.NET unlike VB6 does not support control arrays anymore, and it discusses why this change has been made-look at msdn, there you will find it.
However .NET uses some compatiblity objects like TextBoxArray control which you can find in Tools, Customize ToolBox, and choose .NET Framework Components. I really dont know how it works, you may find a way.
Anyway in cases like you i do this: Each container, maybe your form here (or panels and group boxes) indexes the controls in a way that the last one added gets the zero and so on. So in this manner and utilising the index you can do what you mean in a FOr NEXT loop. For example form1.activeform.controls(i)
-
Oct 18th, 2002, 04:54 AM
#3
Addicted Member
As you can have loads of containers the best way i have found to do this is to add them to a collection when they are created. Here is an example for forms
Create a class
Class cFormsCollectionClass : Implements IEnumerable
Private c As New Collection()
Sub Add(ByVal f As Form)
c.Add(f)
End Sub
Sub Remove(ByVal f As Form)
Dim itemCount As Integer
For itemCount = 1 To c.Count
If f Is c.Item(itemCount) Then
c.Remove(itemCount)
Exit For
End If
Next
End Sub
ReadOnly Property Item(ByVal index) As Form
Get
Return c.Item(index)
End Get
End Property
Overridable Function GetEnumerator() As _
IEnumerator Implements IEnumerable.GetEnumerator
Return c.GetEnumerator
End Function
End Class
declare it
Public Forms As New cFormsCollectionClass()
Add object to it in the sub New of the object
Forms.Add(Me)
Hope that helps
Wind and waves resolves all problems.
-
Oct 18th, 2002, 12:21 PM
#4
Hyperactive Member
AYECHARUMBA!!! No Pimentos!!!!!
Why do you say control arrays aren't supported?
Dim txtX(5) As Textbox (though I guess it doesn't work the same way as VB6 control arrays, )
Anyway, no you cannot use the For Each loop like that, I've tired! However here's something similar:
Code:
Dim A As Int16
Dim txtTest As New TextBox()
For A = 0 To Me.Controls.Count - 1
If Me.Controls.Item(A).GetType().Equals(txtTest.GetType) Then
Controls.Item(A).Text = "Hello There"
End If
Next
'You can also use this if you dont want to instantiate a new
'TextBox control:
For A = 0 To Me.Controls.Count - 1
If Me.Controls.Item(A).GetType().ToString = "System.Windows.Forms.TextBox" Then
Controls.Item(A).Text = "Hello There"
End If
Next
Last edited by Hu Flung Dung; Oct 18th, 2002 at 12:25 PM.
-
Oct 18th, 2002, 01:13 PM
#5
Frenzied Member
Exctracts from MSDN
Control Array Changes in Visual Basic .NET
In Visual Basic 6.0, control arrays could be used to specify a group of controls that shared a set of events. The controls had to be of the same type, and they had to have the same name.
In Visual Basic .NET, control arrays are no longer supported. Changes to the event model make control arrays unnecessary. Just as control arrays in Visual Basic 6.0 could share events, the event model in Visual Basic .NET allows any event handler to handle events from multiple controls. In effect, this allows you to create groups of controls of disparate types that share the same events...
-
Oct 18th, 2002, 01:55 PM
#6
Member
Dim txtControl As Control
For Each txtControl in Me.Controls
If TypeOf (txtControl) Is TextBox Then
txtControl.Text = ""
End If
Next
Harold Hoffman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|