Domanda

I am trying to write a program where I read in a text file and then give the user a few options to view different books that are in the file. I am moving along but I am having a problem figuring out how to let the user pick different options from the menu strip drop down items. For example, I have the exit button that is done in my code and the save button (that doesn't work btw but I am researching how to fix that now), but I will also have the add, delete, and update buttons as well as buttons to display certain things from the text file.

Any sources you can give me to help would be great! thanks!

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
È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top