Clear form in .Net

papa_k

Well-known member
Joined
Jun 12, 2003
Messages
77
Location
UK
This is an easy one guys i am sure - i just dont know how to do it...

I want to do this...

***THIS IS VB6 CODE!***

Dim ctrX As Control

For Each ctrX In Controls

If (TypeOf ctrX Is ComboBox) And (ctrX.Container Is objFrame) Then

ctrX.ListIndex = 0

End If

If (TypeOf ctrX Is ListBox) Then

For intLoopCounter = 1 To ctrX.ListCount Step 1

ctrX.Selected(intLoopCounter - 1) = False

Next intLoopCounter

End If

Next ctrX


When i have upgraded my code to .Net it has decided that this will not longer work.

This is the code that i am not given:

Dim ctrX As System.Windows.Forms.Control
Dim objFrame As Object

For Each ctrX In Controls
UPGRADE_WARNING: TypeOf has a new behavior. Click for more: ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1041"

If (TypeOf ctrX Is System.Windows.Forms.TextBox) And (ctrX.Parent Is objFrame) Then
ctrX.Text = ""
End If

UPGRADE_WARNING: TypeOf has a new behavior. Click for more: ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1041"

If (TypeOf ctrX Is System.Windows.Forms.ComboBox) And (ctrX.Parent Is objFrame) Then

UPGRADE_WARNING: Couldnt resolve default property of object ctrX.ListIndex. Click for more: ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"

ctrX.ListIndex = 0

End If

UPGRADE_WARNING: TypeOf has a new behavior. Click for more: ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1041"

If (TypeOf ctrX Is System.Windows.Forms.ListBox) Then

UPGRADE_WARNING: Couldnt resolve default property of object ctrX.ListCount. Click for more: ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"

For intLoopCounter = 1 To ctrX.ListCount Step 1

UPGRADE_WARNING: Couldnt resolve default property of object ctrX.Selected. Click for more: ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"

ctrX.Selected(intLoopCounter - 1) = False

Next intLoopCounter

End If

Next ctrX

This has flumuxed me - simple to fix? I have no idea, i thought it would be but after some searching it appears no.

The errors i have in the task area window of the vb.net screen


GetCheckedIndices is not a member of System.Windows.Forms.CheckedListBox.
ListCount is not a member of System.Windows.Forms.Control.
ListIndex is not a member of System.Windows.Forms.Control.
Selected is not a member of System.Windows.Forms.Control.

Please help,

Papa.
 
You have to recursively go through all the controls on your form like so:

Code:
Dim c As Control

For Each c In Parent.Controls
    Recurse passing c, first call passes Form itself
    If TypeOf c Is ListBox Then DirectCast(c, ListBox).SelectedIndex = -1
    If TypeOf c Is TextBox Then c.Text = ""
Next c

Untested of course but Im sure you get my drift.
 
i might try that way to see how it works , i created another way which does work :
Code:
   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim f As Control
        For Each f In Form1.ActiveForm.Controls // forms name
            If TypeOf f Is ComboBox Then
                clearBox(f)
            ElseIf TypeOf f Is ListView Then
                ClearListView(f)
            End If
        Next
    End Sub
    Public Sub clearBox(ByVal cb As ComboBox)
        cb.Items.Clear()
    End Sub
    Public Sub ClearListView(ByVal lv As ListView)
        lv.Items.Clear()
    End Sub
 
In ASP.Net

Code:
Dim c As Control

For Each c In Parent.Controls
    Recurse passing c, first call passes Form itself
    If TypeOf c Is ListBox Then DirectCast(c, ListBox).SelectedIndex = -1
    If TypeOf c Is TextBox Then c.Text = ""
Next c
Doesnt work. There isnt a .text item for c
 
Back
Top