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
  Elseif typeof ctrl is TextBox then 
     ctrl.text = ""
  End if

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.

4 Years
Discussion Span
Last Post by kb.net

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


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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.