Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InvalidCastException Newbie 1

Status
Not open for further replies.

jadams0173

Technical User
Feb 18, 2005
1,210
This is my first time trying to make a 2005 program. So far I've been able to use the help files to get me by. But I'm stuck here.

I'm getting this error:
Unable to cast object of type 'System.EventArgs' to type 'System.ComponentModel.CancelEventArgs'.

When I try to call this

Code:
 Private Sub cmdConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvert.Click
        Dim myObj As New System.ComponentModel.CancelEventArgs
        myObj = System.EventArgs.Empty

        Select Case CInt(txtNumber.Text)
            Case 88 To 100
                txtLetter.Text = "A"
            Case 80 To 87
                txtLetter.Text = "B"
            Case 76 To 79
                txtLetter.Text = "C"
            Case 60 To 66
                txtLetter.Text = "D"
            Case 0 To 59
                txtLetter.Text = "E"
            Case Else
                Me.txtNumber_Validating(sender, myObj)

        End Select

        Me.txtNumber.Select(0, txtNumber.Text.Length)

Code:
 Private Sub txtNumber_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtNumber.Validating
        Dim ErrMsg As String

        If ActiveControl.Name = "cmdExit" Then Exit Sub

        ErrMsg = vbNullString

        ' Cancel the event and select the text to be corrected by the user
        If Not ValidateGrade(CInt(txtNumber.Text), ErrMsg) Then
            e.Cancel = True
            txtNumber.Select(0, txtNumber.Text.Length)
            ' Set the ErrorProvider error with the text to display.
            Me.ErrorProvider1.SetError(txtNumber, ErrMsg)
        End If

    End Sub


I tried to have patience but it took to long! :) -DW
 
Theres your issue...

Code:
 Private Sub cmdConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvert.Click
        [b]Dim myObj As New System.ComponentModel.CancelEventArgs
        myObj = System.EventArgs.Empty[/b]

your code doesn't show you using it, so I can't figure out why your have it...

If later in your sub you are using myObj then you might want to declare it as the correct type...

-The answer to your problem may not be the answer to your question.
 
Thanks for the reply Qik3Coder. I'm still a little on the confused side.

I do use the object.
Code:
 Private Sub cmdConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvert.Click

    Dim myObj As New System.ComponentModel.CancelEventArgs

      [blue]   error here
        myObj = System.ComponentModel.CancelEventArgs.Empty[/blue]

        Select Case CInt(txtNumber.Text)
            Case 88 To 100
                txtLetter.Text = "A"
            Case 80 To 87
                txtLetter.Text = "B"
            Case 76 To 79
                txtLetter.Text = "C"
            Case 60 To 66
                txtLetter.Text = "D"
            Case 0 To 59
                txtLetter.Text = "E"
            Case Else
         [b][red]      Me.txtNumber_Validating(sender, myObj)[/red][/b]

        End Select

        Me.txtNumber.Select(0, txtNumber.Text.Length)

I tried to have patience but it took to long! :) -DW
 
ahh, i c...

your cast issue WAS because of:
Dim myObj As New System.ComponentModel.CancelEventArgs
myObj = System.EventArgs.Empty

But is now:
Me.txtNumber_Validating(sender, myObj)

which should probably be instead:
Me.txtNumber_Validating(sender, New System.EventArgs)
or:
Me.txtNumber_Validating(sender, e)

depending on whether you want to "pass" the data through, to a different function, or if you want to call the function with no data, which, it doesn't look like your function handles.


-The answer to your problem may not be the answer to your question.
 
Thanks for your help Qik3Coder

This change fixed my error in my Select...Case...Else

Code:
Case Else
                Me.txtNumber_Validating(sender,[red] New System.ComponentModel.CancelEventArgs[/red])


Have a pinky on me! [thumbsup2]

Thanks again for getting me headed in the right direction!

I tried to have patience but it took to long! :) -DW
 
dang, i have got to read slower.
I kept seeing the "Private Sub txtNumber_" and assumed it was a _Click and so took a regulare .Args and didnt notice the _Validating...

-The answer to your problem may not be the answer to your question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top