Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Clear contents from all controls on a form

dhanukadhanuka Posts: 5Member
Is there an easy way to clear all controls on a form or should I write code for each and every individual control and clear its contents...

Comments

  • merciclemercicle Posts: 18Member
    : Is there an easy way to clear all controls on a form or should I write code for each and every individual control and clear its contents...
    :
    :
    I think you may be able to use a control collection

    For Each Control in FormName.Controls
    Process Object
    Next Control
  • dhanukadhanuka Posts: 5Member
    : : Is there an easy way to clear all controls on a form or should I write code for each and every individual control and clear its contents...
    : :
    : :
    : I think you may be able to use a control collection
    :
    : For Each Control in FormName.Controls
    : Process Object
    : Next Control
    :

    Thanks for the reply.. I tried doing that but the above for loops doesnt work.. its says control is a type and cannot be used as an expression in the for statement...
    can u advice me what to do... also my controls are textboxes, comboboxes, checkboxes and datetime picker... is there a way to set a default value for each of these...when a clear button is pressed on the form??
  • RSlaydenRSlayden Posts: 22Member
    : : : Is there an easy way to clear all controls on a form or should I write code for each and every individual control and clear its contents...
    : : :
    : : :
    : : I think you may be able to use a control collection
    : :
    : : For Each Control in FormName.Controls
    : : Process Object
    : : Next Control
    : :
    :
    : Thanks for the reply.. I tried doing that but the above for loops doesnt work.. its says control is a type and cannot be used as an expression in the for statement...
    : can u advice me what to do... also my controls are textboxes, comboboxes, checkboxes and datetime picker... is there a way to set a default value for each of these...when a clear button is pressed on the form??
    :

    This was a fun one. The solution works, but not with that syntax. The problem I wasn't able to resolve was clearing the checkboxes. Code:

    Dim myCtrl as Control
    For Each myCtrl in Controls
    If TypeOf myCtrl Is CheckBox Then
    'myCtrl.Checked = False
    Else
    myCtrl.Text = ""
    End If
    Next

    Since the DateTimePicker, TextBoxes, and ComboBoxes use the Text property you don't have to test for them. The CheckBoxes are different. CheckBoxes have a CheckedState property that can be checked to see the current status and a Checked property to make assignments. But in the loop above I could not get to those properties. The loop works and all but the checkboxes are cleared (I had this code in a button OnClick sub). In place of 'myCtrl.Checked = False' you could list all of your controls (CheckBox1.Checked = False, CheckBox2.Checked = False,...) but it defeats the ultimate purpose. Maybe someone else can point out the error I'm missing.
  • JustinElamJustinElam Posts: 1Member
    I know this is an 11 year old post, but I just stumbled across it. You could do something like the following in order to access the .checked of the check box

    Dim myCtrl As Control
    For Each myCtrl In Controls
    If TypeOf myCtrl Is TextBox Then
    myCtrl.Text = ""
    ElseIf TypeOf myCtrl Is CheckBox Then
    Dim tmpChkBox As CheckBox
    tmpChkBox = myCtrl
    tmpChkBox.Checked = False
    End If
    Next
Sign In or Register to comment.