Results 1 to 10 of 10

Thread: Writing Menu Code

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    7

    Writing Menu Code

    Hi
    Can someone please tell me where I can get information on how to write standard menu code? Example New (then open a new screen)
    Open (open the C:\ Path) Save save
    I am writing a program in Visual Basic.Net 2005 also must i declare and initilaize the code that i am writing
    Thanks
    Programmer101

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: Writing Menu Code

    add a menu strip to your form
    and in the property window f the strip u can insert the default item

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Writing Menu Code

    There is no such thing as "standard" menu code. You can tell the IDE to add the "standard" menu items like New, Open, Save, etc. sure enough. You then double click each of those menu items to create a Click event handler for each item. Now you write the code in each of those event handlers that you want executed when the user clicks the corresponding menu item. That part is up to you and completely depends on your application. What one application does when the user clicks New would be completely different from what another application does. About the only one that would be the same would be Exit, in which case you'd use:
    vb Code:
    1. Me.Close()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    7

    Re: Writing Menu Code

    Thanks
    This is what i wrote in the event handler in my exit command =>
    Application.Exit()
    And it closes my the Exit command
    So am i heading in the right direction?
    Thanks

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    7

    Re: Writing Menu Code

    Thanks Sir
    Ok I made the MenuStrip1 and in the event of Exit command i put in this
    Application.Exit()
    I like to know is there a way that I can basically go down the menu &F and
    write code in to open a New particular window?
    and Open C:\
    Is there any standard code out there for writing that?
    Thank you for your time
    Programmer101

  6. #6
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: Writing Menu Code

    I'm not sure what question is but I think that you are talking about
    openFileDialog and that you can find in the toolBox as well. By plaing with prperty you can then open a specific path in your computer.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    7

    Re: Writing Menu Code

    Thanks
    I found the Openfiledialog box in the tools just like you said.
    I guess what I am trying to ask is from the user prespective
    How can write or find the code in the handlers for
    New
    Open
    Save
    Save As
    Print
    Print Preview
    Exit
    Application.Exit()

    Also I am on Microsoft website and I found this link to MenuToolStrip
    http://msdn2.microsoft.com/en-us/vbasic/ms789075.aspx
    It has alot of information.
    I downloaded the code, and will run it.
    But you see what I am asking is, is there any standard code, for these commands for menu handlers?
    You can sure tell that I am new at this
    Thanks

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Writing Menu Code

    You need to get out of your head this idea of "standard" code. Only a "standard" application could have "standard" code. What's a "standard" application? There is no such thing.

    Let's take the Open menu item for example. If you want the user to open a file when they select that item then you would display an OpenFileDialog, sure. How you set the properties of that dialogue and what you do with the selected file is completely dependent on your application. Are they opening an image file? ...a text file? ...some custom file format of your own creation? Where and how do you want to display the contents of this file? You see what I'm saying?

    All this has nothing to do with your menu. Once you've created the menu and the event handlers for each item that's the end of the menu's involvement. Each of those jobs is a task in itself and is not related to the others. For example, printing in .NET has nothing to do with menus. If you want to print something then you need to write code to print. If you want to print something when the user selects the Print menu item then you simply call the printing code you wrote from the menu item's Click event handler. The two are separate issues.

    You should be making the bricks before you try to build the house. What do you want to happen when the user wants to create a New document? Write that code and then call it from the New menu item's Click event handler. What code should you write? We can't possibly know because we don't know anything about your app. What do you want to print when the user presses the Print button? Write code to print it and then call that code from the Click event handler of the Print menu item. I can tel you that you'll need to use a PrintDocument object, call its Print method and handle its PrintPage event. Exactly what code you'll need to write to print what you need to print I have no idea because I have no idea what you want to print. There are numerous examples of .NET printing on this forum and else where, including the MSDN documentation.

    Basically what I'm saying is don't ask how to build a house and expect a simple answer. You need to break the problem up into many subproblems and then solve each of those individually. The code behind the menu items is probably about half your application. There is no single command to call for each one. It is completely dependent on your application. You need to analyse what you want to happen in your application and break that up into small chunks that can each be researched by you at MSDN and on the Web or asked about in a forum thread, preferably in that order.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    7

    Re: Writing Menu Code

    Ok That Makes Sense
    I will start at that point now and re-do this.
    Thanks

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Writing Menu Code

    This is about as standard as it gets. As you can see, there is still plenty of code for you to add yourself.
    vb Code:
    1. Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
    2.     'Add your code here to create a new document.
    3. End Sub
    4.  
    5. Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    6.     If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    7.         'Add your code here to use OpenFileDialog1.FileName.
    8.     End If
    9. End Sub
    10.  
    11. Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
    12.     If <the file has not yet been saved> Then
    13.         Me.SaveAs()
    14.     Else
    15.         Me.Save()
    16.     End If
    17. End Sub
    18.  
    19. Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
    20.     Me.SaveAs()
    21. End Sub
    22.  
    23. Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
    24.     Me.PrintDocument1.Print()
    25. End Sub
    26.  
    27. Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
    28.     Me.PrintPreviewDialog1.ShowDialog()
    29. End Sub
    30.  
    31. Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    32.     Application.Exit()
    33. End Sub
    34.  
    35. Private Sub SaveAs()
    36.     If Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    37.         'Create a file at SaveFileDialog1.FileName.
    38.  
    39.         Me.Save()
    40.     End If
    41. End Sub
    42.  
    43. Private Sub Save()
    44.     'Save the file to the current file path here.
    45. End Sub
    46.  
    47. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    48.     'Add your code here to print a page.
    49. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width