473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Specified cast is not valid

I know what that message means, but I sure can't figure out why I'm getting
it in this program. I have five GroupBoxes on Form1 each containing 3 to 5
radio buttons. The following code works fine for the first three groupboxes,
then gives me the error on the fourth. I must be missing SOMETHING, but I
sure don't see it.
*** Code****
Public Sub clearForm()
Dim rad As RadioButton
Dim grp As New GroupBox
Try
For Each grp In Me.Controls
For Each rad In grp.Controls
rad.Checked = False
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
*** End Code ***
This is VB.Net 2003 Professional
Nov 21 '05 #1
5 5093
In Catch, change
MsgBox(ex.Message)
to
MsgBox(grp.tostring & vblf & rad.tostring & vblf & ex.Message)
to see what is happening. My guess is that me.controls contains other
things besides groupboxes, but you won't know until you look.

"Ron DelPorto" wrote:
I know what that message means, but I sure can't figure out why I'm getting
it in this program. I have five GroupBoxes on Form1 each containing 3 to 5
radio buttons. The following code works fine for the first three groupboxes,
then gives me the error on the fourth. I must be missing SOMETHING, but I
sure don't see it.
*** Code****
Public Sub clearForm()
Dim rad As RadioButton
Dim grp As New GroupBox
Try
For Each grp In Me.Controls
For Each rad In grp.Controls
rad.Checked = False
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
*** End Code ***
This is VB.Net 2003 Professional

Nov 21 '05 #2
AMercer,
I will try that, but I know for a fact that it DOES contain other
controls. How can i deal with that? I thi=ought it would skip anything that
was not a groupbox.

"AMercer" wrote:
In Catch, change
MsgBox(ex.Message)
to
MsgBox(grp.tostring & vblf & rad.tostring & vblf & ex.Message)
to see what is happening. My guess is that me.controls contains other
things besides groupboxes, but you won't know until you look.

"Ron DelPorto" wrote:
I know what that message means, but I sure can't figure out why I'm getting
it in this program. I have five GroupBoxes on Form1 each containing 3 to 5
radio buttons. The following code works fine for the first three groupboxes,
then gives me the error on the fourth. I must be missing SOMETHING, but I
sure don't see it.
*** Code****
Public Sub clearForm()
Dim rad As RadioButton
Dim grp As New GroupBox
Try
For Each grp In Me.Controls
For Each rad In grp.Controls
rad.Checked = False
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
*** End Code ***
This is VB.Net 2003 Professional

Nov 21 '05 #3
check the type of the control to see if it is a groupbox before proceeding.

--> If TypeOf grp Is GroupBox Then

Rgds,
Anand
http://www.dotnetindia.com

"Ron DelPorto" wrote:
AMercer,
I will try that, but I know for a fact that it DOES contain other
controls. How can i deal with that? I thi=ought it would skip anything that
was not a groupbox.

"AMercer" wrote:
In Catch, change
MsgBox(ex.Message)
to
MsgBox(grp.tostring & vblf & rad.tostring & vblf & ex.Message)
to see what is happening. My guess is that me.controls contains other
things besides groupboxes, but you won't know until you look.

"Ron DelPorto" wrote:
I know what that message means, but I sure can't figure out why I'm getting
it in this program. I have five GroupBoxes on Form1 each containing 3 to 5
radio buttons. The following code works fine for the first three groupboxes,
then gives me the error on the fourth. I must be missing SOMETHING, but I
sure don't see it.
*** Code****
Public Sub clearForm()
Dim rad As RadioButton
Dim grp As New GroupBox
Try
For Each grp In Me.Controls
For Each rad In grp.Controls
rad.Checked = False
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
*** End Code ***
This is VB.Net 2003 Professional

Nov 21 '05 #4
Ron,

You are testing all controls on your form and in that when it are
parentcontrols the childs as well. Therefore you have two possibilities.
When you are sure that a groupbox only contains radiobuttons you don't have
to do the second, however that much time it will not cost to do both and
prevents errors when you change something.

You are iterating through a collection of controls not a collection of
groupboxes. Therefore at the first moment it will throw and error if the
control is not a groupbox.

I changed it below for you (not tested of course)

You don't need that try and catch because when this goes ones right it goes
forever right. There are no external influences that can change that.
\\\
For Each grp As Control In Me.Controls
If TypeOf grp Is GroupBox Then
For Each rad As Control In grp.Controls
If TypeOf rad Is RadioButton Then
DirectCast(rad, RadioButton).Checked = False
End If
Next
End If
Next
///

I hope this helps,

Cor
Nov 21 '05 #5
Many thanks to AMercer, Anand and Cor Ligthert for your help on this. I now
see where I was going wrong. Many, many thanks!

"Ron DelPorto" wrote:
I know what that message means, but I sure can't figure out why I'm getting
it in this program. I have five GroupBoxes on Form1 each containing 3 to 5
radio buttons. The following code works fine for the first three groupboxes,
then gives me the error on the fourth. I must be missing SOMETHING, but I
sure don't see it.
*** Code****
Public Sub clearForm()
Dim rad As RadioButton
Dim grp As New GroupBox
Try
For Each grp In Me.Controls
For Each rad In grp.Controls
rad.Checked = False
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
*** End Code ***
This is VB.Net 2003 Professional

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
4
by: Tyro | last post by:
Can someone shed some light on my error here? Thanks! Specified cast is not valid. Exception Details: System.InvalidCastException: Specified cast is not valid. Source Error: Stack Trace:
3
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
3
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots' Application....
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
0
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks....
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
2
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.