2014-09-30 54 views
-1

不知道定義的WithEvents變量爲什麼即時得到這個錯誤或如何解決它..感謝所有幫助Handles子句需要包含類型或它的基類型之一錯誤

Imports System.IO 

Public Class Form1 
    ' Class-level variables 
    Private strFilename As String = String.Empty ' Document filename 
    Dim blnIsChanged As Boolean = False    ' File change flag 

    Sub ClearDocument() 
     ' Clear the contents of the text box. 
     txtDocument.Clear() 

     ' Clear the document name. 
     strFilename = String.Empty 

     ' Set isChanged to False. 
     blnIsChanged = False 
    End Sub 

    ' The OpenDocument procedure opens a file and loads it 
    ' into the TextBox for editing. 

    Sub OpenDocument(ByVal strFilename As String) 
     Dim inputFile As StreamReader    ' Object variable 

     ' If called without an argument, browse a file using 
     ' Windows Forms 
     If strFilename = Nothing Then 
      If ofdOpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then 
       ' Retrive selected filename 
       strFilename = ofdOpenFile.FileName 
      End If 
     End If 

     Try 
      ' Open the file. 
      inputFile = File.OpenText(strFilename) 

      ' Read the file's contents into TextBox. 
      txtDocument.Text = inputFile.ReadToEnd() 

      ' Close the file. 
      inputFile.Close() 

      ' Update isChanged variable. 
      blnIsChanged = False 

      ' Display the name of the file. 
      Me.Text = "Simple Text Editor - " & strFilename 

     Catch 
      ' Error message for file open error. 
      MessageBox.Show("Error opening the file.") 
     End Try 

    End Sub 


    ' The SaveDocument procedure saves current document. 

    Sub SaveDocument() 
     Dim outputFile As StreamWriter  ' Object varibale 

     Try 
      ' Create the file. 
      outputFile = File.CreateText(strFilename) 

      ' Write the TextBok to file. 
      outputFile.Write(txtDocument.Text) 

      ' Close the file. 
      outputFile.Close() 

      ' Update isChanged varibale. 
      blnIsChanged = False 
     Catch 
      ' Error message for file creation error. 
      MessageBox.Show("Error creating the file.") 
     End Try 
    End Sub 

    Private Sub txtDocument_TextChanged(sender As Object, e As EventArgs) Handles txtDocument.TextChanged 
     ' Indicate the text has changed. 
     blnIsChanged = True 
    End Sub 

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

     ' Has the current document changed? 
     If blnIsChanged = True Then 
      ' Confirm before clearing the document. 
      If MessageBox.Show("The current document is not saved." & "Are you sure?", 
            "Confirm", MessageBoxButtons.YesNo) = 
            Windows.Forms.DialogResult.Yes Then 
       ClearDocument() 
      End If 
     Else 
      ' Document has not changed, so clear it. 
      ClearDocument() 
     End If 
    End Sub 

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

     ' Has the current document changed? 
     If blnIsChanged = True Then 
      ' Confirm before clearing and replacing 
      If MessageBox.Show("The current document is not saved." & "Are you sure?", 
            "Confirm", MessageBoxButtons.YesNo) = 
            Windows.Forms.DialogResult.Yes Then 
       ClearDocument() 
       OpenDocument(Nothing) 
      End If 
     Else 
      ' Document has not changed, so replace it. 
      ClearDocument() 
      OpenDocument(Nothing) 
     End If 
    End Sub 

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

     ' Does the current document have a filename? 
     If strFilename = String.Empty Then 
      ' The Document has not been saved, so 
      'use Save As dialog box 
      If sfdSaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then 
       strFilename = sfdSaveFile.FileName 
       SaveDocument() 
      End If 
     Else 
      ' Save the document with the current filename. 
      SaveDocument() 
     End If 
    End Sub 

    Private Sub mnuFileSaveAs_Click(sender As Object, e As EventArgs) Handles mnuFileSaveAs.Click 
     ' Save the current document under a new filename. 
     If sfdSaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then 
      strFilename = sfdSaveFile.FileName 
      SaveDocument() 
     End If 
    End Sub 

    Private Sub mnuFileExit_Click(sender As Object, e As EventArgs) Handles mnuFileExit.Click 
     ' Close form 
     Me.Close() 
    End Sub 

    Private Sub mnuHelpAbout_Click(sender As Object, e As EventArgs) Handles mnuHelpAbout.Click 
     ' Display an about box. 
     MessageBox.Show("Simple Text Editor version 1.0") 
    End Sub 

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 

     ' If the document has been modified, confirm 
     ' before editing. 
     If blnIsChanged = True Then 
      Dim result = MessageBox.Show("The current document is not saved. Do you wish to save " & 
           "your changes?", "Confirm", MessageBoxButtons.YesNoCancel) 

      If result = Windows.Forms.DialogResult.Yes Then 
       e.Cancel = False ' Close, and 
       SaveDocument()  ' Save current changes 
      ElseIf result = Windows.Forms.DialogResult.No Then 
       e.Cancel = False ' Close without saving. 
      Else 
       e.Cancel = True  ' Cancel closing. 
      End If 
     End If 
    End Sub 


    Public Sub New() 

     InitializeComponent() 

     ' Desktop path. 
     Dim desktopPath = My.Computer.FileSystem.SpecialDirectories.Desktop 



     ' Open the default file upon opening program. 
     strFilename = desktopPath & "/contacts_hw2.csv" 
     OpenDocument(strFilename) 

    End Sub 

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk 

    End Sub 
End Class 
+2

我們猜猜那個代碼轉儲的錯誤在哪裏? http://stackoverflow.com/help/how-to-ask – Plutonix 2014-09-30 03:43:18

回答

0

子:

Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk 

End Sub 

處理OpenFileDialog.FileOk事件,但您的OpenFileDialog似乎被稱爲ofdOpenFile。

改變子的定義爲:

Private Sub ofdOpenFile_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ofdOpenFile.FileOk 

End Sub 

或刪除它。

相關問題