473,382 Members | 1,386 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,382 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 5091
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; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.