none
Unable to cast object of type 'System.Windows.Forms.ToolStripMenuItem' to type 'System.Windows.Forms.Control'.?

    Question

  •  I am creating a MDI and am trying to create copy cut and paste functions but i keep recieving this error message

    Unable to cast object of type 'System.Windows.Forms.ToolStripMenuItem' to type 'System.Windows.Forms.Control'.

    this is my child form code-


    Public Class frmChild

    Public blnHasChanged As Boolean

    Private Sub frmChild_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

    If ((My.Application.OpenForms.Count) = 1) Then

    frmMDIParent.mnuWindow.Visible = False

    End If

    ' User wants to unload this form. Call the CloseFile method in the MDI parent form. The Me argument provides a reference to the current form to be closed.

    frmMDIParent.CloseFile(Me)

    End Sub

    'The user is trying to close the form. Query whether or not unsaved changes should be abandoned.

    Private Sub frmChild_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Dim intCurrent As Integer

    Dim dr As DialogResult

    Dim Cancel As Boolean = e.Cancel

    If blnHasChanged Then

    intCurrent = CInt(Me.Tag)

    dr = MessageBox.Show("Do you want to save changes made to Child Form " & intCurrent & "?", _

    "Some changes detected since last Save", _

    MessageBoxButtons.YesNoCancel, _

    MessageBoxIcon.Exclamation, _

    MessageBoxDefaultButton.Button1)

    Select Case dr.ToString

    Case "Yes"

    MessageBox.Show("A routine to Save documents not currently available." & ControlChars.NewLine & ControlChars.NewLine & _

    "When the code becomes available, a call to it should " & ControlChars.NewLine & "replace this message box.", _

    "No Save available", _

    MessageBoxButtons.OK, _

    MessageBoxIcon.Information)

    Case "No"

    frmMDIParent.CloseFile(Me)

    Case "Cancel"

    Cancel = True

    End Select

    End If

    e.Cancel = Cancel

    End Sub

    Private Sub frmChild_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    RichTextBox1.Top = 0

    RichTextBox1.Left = 0

    RichTextBox1.Height = ClientSize.Height

    RichTextBox1.Width = ClientSize.Width

    End Sub

    Private Sub frmChild_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

    RichTextBox1.Top = 0

    RichTextBox1.Left = 0

    RichTextBox1.Height = ClientSize.Height

    RichTextBox1.Width = ClientSize.Width

    End Sub

    Private Sub mnuFileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click

    frmMDIParent.NewFile()

    End Sub

    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click

    Call TestClick("mnuOpenClose")

    End Sub

    Private Sub mnuFileClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileClose.Click

    Me.Close()

    End Sub

    Private Sub mnuFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click

    Call TestClick("mnuFileSave")

    End Sub

    Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click

    frmMDIParent.Close()

    End Sub

    'Whenever the user changes the contents of the text box the blnHasChanged flag should be set to true indicating changed data.

    Private Sub txtDocument_TextChanged(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

    blnHasChanged = True

    End Sub

    Public Sub StateHandler(ByVal sender As System.Object, Optional ByVal strEvent As String = "")

    Select Case CType(sender, Control).Name

    Case "frmPlainText"

    Call SetCopyAndCut("ToDisabled")

    Select Case Clipboard.ContainsText

    Case True

    Call SetPasteAndClearClipBoard("ToEnabled")

    Case False

    Clipboard.Clear()

    Call SetPasteAndClearClipBoard("ToDisabled")

    'Call SetClearAllAndSelectAll("ToDisabled")

    End Select

    Select Case Len(Trim(RichTextBox1.Text))

    Case Is > 0

    Call SetClearAllAndSelectAll("ToEnabled")

    Case Is = 0

    Call SetClearAllAndSelectAll("ToDisabled")

    End Select

    Case "btnCopy"

    Call SetPasteAndClearClipBoard("ToEnabled")

    Case "btnCut"

    Call SetCopyAndCut("ToDisabled")

    Call SetPasteAndClearClipBoard("ToEnabled")

    Case "btnPaste"

    Call SetCopyAndCut("ToDisabled")

    'Call SetClearAllAndSelectAll("ToEnabled")

    Case "btnClearAll"

    Call SetCopyAndCut("ToDisabled")

    Case "btnSelectAll"

    Call SetCopyAndCut("ToEnabled")

    Case "btnClearClipboard"

    Call SetPasteAndClearClipBoard("ToDisabled")

    Case "btnExit"

    End

    Case "txtEditor"

    Select Case strEvent

    Case "TextChanged"

    Select Case Len(Trim$(RichTextBox1.Text))

    Case Is > 0

    Call SetClearAllAndSelectAll("ToEnabled")

    Case Else

    Call SetClearAllAndSelectAll("ToDisabled")

    End Select

    Case "MouseMove"

    Select Case Len(Trim$(RichTextBox1.SelectedText))

    Case Is > 0

    Call SetCopyAndCut("ToEnabled")

    Case Else

    Call SetCopyAndCut("ToDisabled")

    End Select

    End Select

    End Select

    End Sub





    Private Sub SetCopyAndCut(ByVal strEnabledOrDisabled As String)

    Call SetCopy(strEnabledOrDisabled)

    Call SetCut(strEnabledOrDisabled)

    End Sub

    Private Sub SetPasteAndClearClipBoard(ByVal strEnabledOrDisabled As String)

    Call SetPaste(strEnabledOrDisabled)

    Call SetClearClipBoard(strEnabledOrDisabled)

    End Sub

    Private Sub SetClearAllAndSelectAll(ByVal strEnabledOrDisabled As String)

    Call SetClearAll(strEnabledOrDisabled)

    Call SetSelectAll(strEnabledOrDisabled)

    End Sub

    Private Sub SetCopy(ByVal strEnabledOrDisabled As String)

    frmMDIParent.mnuEditCopy.Enabled = blnTrueOrFalse(strEnabledOrDisabled)

    End Sub

    Private Sub SetCut(ByVal strEnabledOrDisabled As String)

    frmMDIParent.mnuEditCut.Enabled = blnTrueOrFalse(strEnabledOrDisabled)

    End Sub

    Private Sub SetPaste(ByVal strEnabledOrDisabled As String)

    frmMDIParent.mnuEditPaste.Enabled = blnTrueOrFalse(strEnabledOrDisabled)

    End Sub

    Private Sub SetClearClipBoard(ByVal strEnabledOrDisabled As String)

    frmMDIParent.mnuEditClearclipboard.Enabled = blnTrueOrFalse(strEnabledOrDisabled)

    End Sub

    Private Sub SetClearAll(ByVal strEnabledOrDisabled As String)

    frmMDIParent.mnuEditClearall.Enabled = blnTrueOrFalse(strEnabledOrDisabled)

    End Sub

    Private Sub SetSelectAll(ByVal strEnabledOrDisabled As String)

    frmMDIParent.mnuEditSelectall.Enabled = blnTrueOrFalse(strEnabledOrDisabled)

    End Sub

    Private Function blnTrueOrFalse(ByVal strEnabledOrDisabled) As Boolean

    Select Case strEnabledOrDisabled

    Case "ToEnabled"

    Return True

    Case "ToDisabled"

    Return False

    End Select

    End Function

    Public Sub CopyFromTextBoxToClipboard()

    'The CopyFromTextBoxToClipboard, CutFromTextBoxToClipboard and the PasteFromClipboardToTextBox

    'sub procedures currently expose Version 1, of 4 versions. The results have only been tested for situations where

    'the 3 sub procedures have each been converted to the same version number.

    '

    'Otherwise results have not been tested and results may in such cases be unpredictable.

    '===============================

    'Version 1 Start of Copy operation

    '===============================

    RichTextBox1.Copy()

    '===============================

    'Version 1 End of Copy operation

    '===============================

    '===============================

    'Version 2 Start of Copy operation

    '===============================

    'Clipboard.SetText(txtEditor.SelectedText)

    '===============================

    'Version 2 End of Copy operation

    '===============================



    '===============================

    'Version 3 Start of Copy operation

    '===============================

    'Select Case txtEditor.SelectionLength

    ' Case Is > 0

    ' Clipboard.SetDataObject(txtEditor.SelectedText)

    'End Select

    '===============================

    'Version 3 End of Copy operation

    '===============================



    '===============================

    'Version 4 Start of Copy operation

    '===============================

    'Dim data As New DataObject()

    'Select Case txtEditor.SelectionLength

    ' Case Is > 0

    ' data.SetData(DataFormats.Text, txtEditor.SelectedText)

    ' Clipboard.SetDataObject(data)

    'End Select

    '===============================

    'Version 4 End of Copy operation

    '===============================

    End Sub

    Public Sub CutFromTextBoxToClipboard()

    'The CopyFromTextBoxToClipboard, CutFromTextBoxToClipboard and the PasteFromClipboardToTextBox

    'sub procedures currently expose Version 1, of 4 versions. The results have only been tested for situations where

    'the 3 sub procedures have each been converted to the same version number.

    '

    'Otherwise results have not been tested and results may in such cases be unpredictable.

    '===============================

    'Version 1 Start of Cut operation

    '===============================

    RichTextBox1.Cut()

    '===============================

    'Version 1 End of Cut operation

    '===============================

    '===============================

    'Version 2 Start of Cut operation

    '===============================

    'Clipboard.SetText(txtEditor.SelectedText)

    'txtEditor.SelectedText = ""

    '===============================

    'Version 2 End of Cut operation

    '===============================

    '===============================

    'Version 3 Start of Cut operation

    '===============================

    'Select Case txtEditor.SelectionLength

    ' Case Is > 0

    ' Clipboard.SetDataObject(txtEditor.SelectedText)

    ' txtEditor.SelectedText = ""

    'End Select

    '===============================

    'Version 3 End of Cut operation

    '===============================

    '===============================

    'Version 4 Start of Cut operation

    '===============================

    'Dim data As New DataObject()

    'Select Case txtEditor.SelectionLength

    ' Case Is > 0

    ' data.SetData(DataFormats.Text, txtEditor.SelectedText)

    ' Clipboard.SetDataObject(data)

    ' txtEditor.SelectedText = ""

    'End Select

    '===============================

    'Version 4 End of Cut operation

    '===============================

    End Sub

    Public Sub PasteFromClipboardToTextBox()

    'The CopyFromTextBoxToClipboard, CutFromTextBoxToClipboard and the PasteFromClipboardToTextBox

    'sub procedures currently expose Version 1, of 4 versions. The results have only been tested for situations where

    'the 3 sub procedures have each been converted to the same version number.

    '

    'Otherwise results have not been tested and results may in such cases be unpredictable.

    '===============================

    'Version 1 Start of Paste operation

    '===============================

    RichTextBox1.Paste()

    '===============================

    'Version 1 End of Paste operation

    '===============================

    '===============================

    'Version 2 Start of Paste operation

    '===============================

    'If Clipboard.ContainsText() Then

    ' txtEditor.SelectedText = Clipboard.GetText()

    'End If

    'txtEditor.SelectionLength = 0

    '===============================

    'Version 2 End of Paste operation

    '===============================

    '===============================

    'Version 3 and 4 Start of Paste operation

    '===============================

    'Dim data As IDataObject

    'data = Clipboard.GetDataObject()

    'If data.GetDataPresent(DataFormats.Text, True) Then

    'RichTextBox1.SelectedText = Data.GetData(DataFormats.Text, True).ToString

    'End If

    '===============================

    'Version 3 and 4 End of Paste operation

    '===============================

    End Sub

    End Class



    the red part is where the error message is coming frm anybody got any ideas how to sort it out

    thanks

    Tuesday, December 30, 2008 1:20 PM

Answers

  • Not being sure of where this sub is going it doesn't appear that you are using the sender for anthing except  it's name so why not change it to

    Public Sub StateHandler(ByVal ControlName As StringOptional ByVal strEvent As String = "")  
        Select Case ControlName  
            Case "frmPlainText" 
                'bla bla bla... 

    • Marked as answer by Xingwei Hu Monday, January 05, 2009 7:51 AM
    Tuesday, December 30, 2008 2:23 PM
  • You can't cast a Component to a Control. 
    • Marked as answer by Xingwei Hu Monday, January 05, 2009 7:50 AM
    Tuesday, December 30, 2008 2:28 PM

All replies

  • Not being sure of where this sub is going it doesn't appear that you are using the sender for anthing except  it's name so why not change it to

    Public Sub StateHandler(ByVal ControlName As StringOptional ByVal strEvent As String = "")  
        Select Case ControlName  
            Case "frmPlainText" 
                'bla bla bla... 

    • Marked as answer by Xingwei Hu Monday, January 05, 2009 7:51 AM
    Tuesday, December 30, 2008 2:23 PM
  • You can't cast a Component to a Control. 
    • Marked as answer by Xingwei Hu Monday, January 05, 2009 7:50 AM
    Tuesday, December 30, 2008 2:28 PM