|
-
Mar 17th, 2007, 07:45 PM
#1
Thread Starter
New Member
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
-
Mar 17th, 2007, 07:59 PM
#2
Fanatic Member
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
-
Mar 17th, 2007, 08:07 PM
#3
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:
-
Mar 17th, 2007, 08:13 PM
#4
Thread Starter
New Member
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
-
Mar 17th, 2007, 08:18 PM
#5
Thread Starter
New Member
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
-
Mar 17th, 2007, 08:28 PM
#6
Fanatic Member
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.
-
Mar 17th, 2007, 08:42 PM
#7
Thread Starter
New Member
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
-
Mar 17th, 2007, 09:14 PM
#8
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.
-
Mar 17th, 2007, 09:23 PM
#9
Thread Starter
New Member
Re: Writing Menu Code
Ok That Makes Sense
I will start at that point now and re-do this.
Thanks
-
Mar 18th, 2007, 04:41 PM
#10
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:
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
'Add your code here to create a new document.
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
'Add your code here to use OpenFileDialog1.FileName.
End If
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
If <the file has not yet been saved> Then
Me.SaveAs()
Else
Me.Save()
End If
End Sub
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
Me.SaveAs()
End Sub
Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
Me.PrintDocument1.Print()
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
Me.PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub SaveAs()
If Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
'Create a file at SaveFileDialog1.FileName.
Me.Save()
End If
End Sub
Private Sub Save()
'Save the file to the current file path here.
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Add your code here to print a page.
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|