reset button

plumage

Active member
Joined
Jul 12, 2004
Messages
38
Programming Experience
Beginner
wat are the coding for the reset button for window application (VB.net)

i wan reset the all field that i enter..

cos currently i clearing each individual field which is troublesome...is there any other way..

thank
 
You can loop through the controls collection of the form, check the type of the control, and take the appropriate action based on the control type. Here is example code for textBox and checkBox controls:

VB.NET:
Private Sub ClearFields()
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If ctrl.GetType Is GetType(TextBox) Then
            ctrl.Text = ""
        ElseIf ctrl.GetType Is GetType(CheckBox) Then
            Dim chkbx As CheckBox = ctrl
            chkbx.Checked = False
        End If
    Next
End Sub
 
i try the code as given above..but only those textbox field out the groupbox can been cleared.
inside my groupbox, i have quite anumber of textfield, but the field can not be clear.can anyone pls help me..thanks
 
Here's the above function modified to handle controls that can have child controls (such as a groupbox or panel):

VB.NET:
Private Sub ClearFields(ByVal mainCtrl As Control)
    Dim ctrl As Control
    For Each ctrl In mainCtrl.Controls
        If ctrl.HasChildren Then ClearFields(ctrl)
        If ctrl.GetType Is GetType(TextBox) Then
            ctrl.Text = ""
        ElseIf ctrl.GetType Is GetType(CheckBox) Then
            Dim chkbx As CheckBox = ctrl
            chkbx.Checked = False
        End If
    Next
End Sub

To clear all the controls on a form use this:
VB.NET:
ClearFields(Me)
 
can u help me c what wrong with my code, cos got problem.
thank


Private Sub btnClear_Click(ByVal sender As System.Object,ByVal mainCtrl As Control, ByVal e As System.EventArgs) Handles btnClear.Click


Dim ctrl As Control

For Each ctrl In mainCtrl.Controls

If ctrl.HasChildren Then btnClear(ctrl)

If ctrl.GetType Is GetType(TextBox) Then

ctrl.Text = ""

End If

Next

End Sub







End
Class

 
reset

any also one more problem.

when i want to reset the field that i chose in the combobox,
i use the code.

cboGender.items.clear()

but how cum, inside all my dropdwn item list are gone?
 
The items in the comboBox are gone because the Clear method of the items collection of the comboBox does just as the name implies, it clears (or removes) the items. You should set the Text property of the comboBox to an empty string instead.

Use this code:

VB.NET:
Private Sub ClearFields(ByVal mainCtrl As Control)
    Dim ctrl As Control
    For Each ctrl In mainCtrl.Controls
        If ctrl.HasChildren Then ClearFields(ctrl)
        If ctrl.GetType Is GetType(TextBox) OrElse _
          ctrl.GetType Is GetType(ComboBox) Then
            ctrl.Text = ""
        ElseIf ctrl.GetType Is GetType(CheckBox) Then
            Dim chkbx As CheckBox = ctrl
            chkbx.Checked = False
        End If
    Next
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles btnClear.Click
    ClearFields(Me)
End Sub
 
when i use combobox and when the dropdownstyle i use is dropdown, then it will clear the field when i press the clear button.

but how cum when the dropdownstyle i use is dropdownlist, the clear button wouldnt work?
 
From the help file: DropDownList - The user cannot directly edit the text portion. The user must click the arrow button to display the list portion.

If you want to use a combobox with the DropDownStyle set to DropDownList and set the value to an empty string instead of some default value you need to create an item in the ComboBox that is an empty string.
 
You can loop through the controls collection of the form, check the type of the control, and take the appropriate action based on the control type. Here is example code for textBox and checkBox controls:

VB.NET:
Private Sub ClearFields()
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If ctrl.GetType Is GetType(TextBox) Then
            ctrl.Text = ""
        ElseIf ctrl.GetType Is GetType(CheckBox) Then
            Dim chkbx As CheckBox = ctrl
            chkbx.Checked = False
        End If
    Next
End Sub

how would you include a listbox in this code? having probs, clearing my listbox information that is displayed...
 
Back
Top