Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ch As C1CommandHolder = C1CommandHolder.CreateCommandHolder(Me)
' In this sample, we use a single event handler for all commands,
' and use a switch statement inside that handler to select the specific
' action. Alternatively, each command can be assigned its own Click
' event handler. Or, a mix of the two approaches is possible.
AddHandler ch.CommandClick, New CommandClickEventHandler(AddressOf CommandClickHandler)
' use the image list for command images
ch.ImageList = Me.ImageList1
' modern look:
ch.LookAndFeel = LookAndFeelEnum.Office2003
Dim mm As New C1MainMenu
Me.Controls.Add(mm)
' For the main menu, its CommandHolder property should be set
' (this is not required for builds 1.0.20041.47 or later of C1Command).
mm.CommandHolder = ch
' Main menu - File
Dim mFile As C1CommandMenu = CType(ch.CreateCommand(GetType(C1CommandMenu)), C1CommandMenu)
mFile.Text = "&File"
mm.CommandLinks.Add(New C1CommandLink(mFile))
' create commands for file ops
Dim cNew As C1Command = ch.CreateCommand()
cNew.Text = "&New"
' UserData is arbitrary data associated with commands;
' In this example we use text labels to recognize specific commands
' in the single command handler. Alternatively, we could have commands
' as members of our class, and compare objects to recognize commands.
cNew.UserData = "file_new"
cNew.Shortcut = Shortcut.CtrlN
cNew.ImageIndex = 1
Dim cOpen As C1Command = ch.CreateCommand()
cOpen.Text = "&Open"
cOpen.UserData = "file_open"
cOpen.Shortcut = Shortcut.CtrlO
cOpen.ImageIndex = 0
Dim cExit As C1Command = ch.CreateCommand()
cExit.Text = "E&xit"
cExit.UserData = "exit"
cExit.Shortcut = Shortcut.CtrlX
mFile.CommandLinks.Add(New C1CommandLink(cNew))
mFile.CommandLinks.Add(New C1CommandLink(cOpen))
mFile.CommandLinks.Add(New C1CommandLink)
mFile.CommandLinks(mFile.CommandLinks.Count - 1).Text = "-"
mFile.CommandLinks.Add(New C1CommandLink(cExit))
' Main menu - Window
Dim mWindow As C1CommandMenu = CType(ch.CreateCommand(GetType(C1CommandMenu)), C1CommandMenu)
mWindow.Text = "&Window"
mm.CommandLinks.Add(New C1CommandLink(mWindow))
Dim cNewWindow As C1Command = ch.CreateCommand()
cNewWindow.Text = "New &Window"
cNewWindow.UserData = "window_new"
cNewWindow.Shortcut = Shortcut.CtrlW
cNewWindow.ImageIndex = 2
mWindow.CommandLinks.Add(New C1CommandLink(cNewWindow))
' For toolbars to be dockable/floatable, we must put them
' in a C1CommandDock:
Dim dock As New C1CommandDock
Me.Controls.Add(dock)
dock.Dock = DockStyle.Top
' Add a toolbar, link it to the (already existing) commands:
Dim tb As New C1ToolBar
' add file commands and the window menu to the toolbar
tb.CommandLinks.Add(New C1CommandLink(cNew))
tb.CommandLinks.Add(New C1CommandLink(cOpen))
Dim cl As C1CommandLink
cl = New C1CommandLink(mWindow)
tb.CommandLinks.Add(cl)
' because we did not provide an image for the window menu,
' make it show as text
cl.ButtonLook = ButtonLookFlags.Text
cl = New C1CommandLink(cExit)
tb.CommandLinks.Add(cl)
' ditto for the exit command
cl.ButtonLook = ButtonLookFlags.Text
' add the new toolbar to the dock
dock.Controls.Add(tb)
End Sub
Private Sub CommandClickHandler(ByVal sender As Object, ByVal e As CommandClickEventArgs)
Dim cmdData As String = CType(e.Command.UserData, String)
Select Case (cmdData)
Case "file_new"
Me.RichTextBox1.Text = String.Empty
Me.Text = "New document"
Case "file_open"
Dim ofd As New OpenFileDialog
ofd.Filter = "Text files|*.txt|Rich text files|*.rtf|All files|*.*"
If (ofd.ShowDialog() = DialogResult.OK) Then
Select Case (ofd.FilterIndex)
Case 1
Me.RichTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText)
Case 2
Me.RichTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText)
Case Else
Me.RichTextBox1.LoadFile(ofd.FileName)
End Select
Me.Text = ofd.FileName
End If
Case "exit"
Dim f As Form
For Each f In s_forms
f.Close()
Next
Case "window_new"
Dim f1 As New Form1
f1.Show()
Case Else
' unknown command - should not happen
End Select
End Sub
这个是实例里的代码。我试了下就是上面截图的错误。
如果不使用上述代码,只是引用是不会报错的。 |