vb中的MenuStrip函数

时间:2012-10-04 22:57:45

标签: vb.net menustrip

我正在尝试编写一个我在文本文件中读取的程序,然后为用户提供一些选项来查看文件中的不同书籍。我正在前进,但我有一个问题,想知道如何让用户从菜单条下拉项中选择不同的选项。例如,我有我的代码中的退出按钮和保存按钮(这不起作用,但我正在研究如何解决这个问题),但我也将添加,删除和更新按钮作为以及从文本文件中显示某些内容的按钮。

你可以给我帮助的任何来源都很棒!谢谢!

Sub Main()
    Dim objReader As New StreamReader("C:\Users\HPG62-220US\Documents\Visual Studio 2010\Projects\Assignement 8\Assignement 8\bin\Debug\Books.txt")
    Dim sLine As String = ""
    Dim arrayText As New ArrayList()

    Do
        sLine = objReader.ReadLine()
        If Not sLine Is Nothing Then
            arrayText.Add(sLine)
        End If
    Loop Until sLine Is Nothing
    objReader.Close()

    For Each sLine In arrayText
        Console.WriteLine(sLine)
    Next
    Console.ReadLine()

End Sub

Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()

End Sub

Private Sub SaveToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveToolStripMenuItem.Click

    Using writer As StreamWriter = New StreamWriter("book list.txt", True)
        For Each line As String In lstBooks.Items
            writer.WriteLine(line)
        Next
    End Using
End Sub

Private Sub Delete_Click(sender As System.Object, e As System.EventArgs) Handles Delete.Click


End Sub

1 个答案:

答案 0 :(得分:0)

Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
  'We have MenuStrip1 which has a "Documents" top level menuitem
  'Dynamically add 3 options to it

  Dim doc1 As New ToolStripMenuItem("Doc1")
  doc1.Tag = "Snow White" 'some identifying data so we can tell it apart in the event handler
  DocumentsToolStripMenuItem.DropDownItems.Add(doc1)
  AddHandler doc1.Click, AddressOf Doc_Click

  Dim doc2 As New ToolStripMenuItem("Doc2")
  doc2.Tag = "War and Peace"
  DocumentsToolStripMenuItem.DropDownItems.Add(doc2)
  AddHandler doc2.Click, AddressOf Doc_Click

  Dim doc3 As New ToolStripMenuItem("Doc3")
  doc3.Tag = "Dictionary"
  DocumentsToolStripMenuItem.DropDownItems.Add(doc3)
  AddHandler doc3.Click, AddressOf Doc_Click

  'remove the middle one
  DocumentsToolStripMenuItem.DropDownItems.Remove(doc2)

End Sub


Private Sub Doc_Click(sender As System.Object, e As System.EventArgs)
  Dim tsmi As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
  Select Case tsmi.Tag.ToString
    Case "Snow White"
      MsgBox("Don't eat the apple")
    Case "War and Peace"
      MsgBox("Tolstoy")
    Case "Dictionary"
      MsgBox("Blah")
  End Select
End Sub