Hi, i am having problem with my vb project to clear all item in my form. Below is the code that i am using :

Dim ctrl, obj as control

For Each ctrl in Me.controls
  If typeof ctrl is GroupBox then
     For Each obj In ctrl.Controls
       If typeof obj is TextBox or typeof is ComboBox or typeof is ListBox then
         obj.text =""
  End if
Next
  Elseif typeof ctrl is TextBox then 
     ctrl.text = ""
  End if
Next

The error is :

InvalidCastException was unhandled
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional Information : Conversion from string "" to type 'Double' is not valid.

Please Enlighten me. I really do not understand it.

Recommended Answers

All 4 Replies

did you try the .Clear() method? I'm not sure because I'm not testing this at them moment but perhaps

obj.Clear()

'clear' is not a member of 'System.Windows.Forms.Control'

please try
---> If typeof obj is TextBox or typeof is ComboBox or typeof is ListBox then
case 1.change to
---> If (typeof obj is TextBox) or (typeof obj is ComboBox) or (typeof obj is ListBox) then
case 2.change to
If typeof obj is TextBox then
........
end if
if typeof obj is ComboBox then
........
end if

and so on

I think Pollatep is correct, your if statement is wrong. you have to add the object with each condition:

If typeof obj is TextBox or typeof obj is ComboBox or typeof obj is ListBox then
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.