DEVTOME.COM HOSTING COSTS HAVE BEGUN TO EXCEED 115$ MONTHLY. THE ADMINISTRATION IS NO LONGER ABLE TO HANDLE THE COST WITHOUT ASSISTANCE DUE TO THE RISING COST. THIS HAS BEEN OCCURRING FOR ALMOST A YEAR, BUT WE HAVE BEEN HANDLING IT FROM OUR OWN POCKETS. HOWEVER, WITH LITERALLY NO DONATIONS FOR THE PAST 2+ YEARS IT HAS DEPLETED THE BUDGET IN SHORT ORDER WITH THE INCREASE IN ACTIVITY ON THE SITE IN THE PAST 6 MONTHS. OUR CPU USAGE HAS BECOME TOO HIGH TO REMAIN ON A REASONABLE COSTING PLAN THAT WE COULD MAINTAIN. IF YOU WOULD LIKE TO SUPPORT THE DEVTOME PROJECT AND KEEP THE SITE UP/ALIVE PLEASE DONATE (EVEN IF ITS A SATOSHI) TO OUR DEVCOIN 1M4PCuMXvpWX6LHPkBEf3LJ2z1boZv4EQa OR OUR BTC WALLET 16eqEcqfw4zHUh2znvMcmRzGVwCn7CJLxR TO ALLOW US TO AFFORD THE HOSTING.

THE DEVCOIN AND DEVTOME PROJECTS ARE BOTH VERY IMPORTANT TO THE COMMUNITY. PLEASE CONTRIBUTE TO ITS FURTHER SUCCESS FOR ANOTHER 5 OR MORE YEARS!

Programming Microsoft Word - 03 - Running the program from the tray

Giving our program a place to start

In the previous post, we made a windows program that uses a Windows form. The form contains our controls including buttons with which the user can work with the program. We could also use this form to link (with a button) to other forms, providing for a starting point to access several parts of our program. We could to the same by inserting tabs or using a top side menu.

In this 3rd post, we will approach this a little different.

Our program - which will gain more functions in the future - will be used in a typical setting whereby the user works with Word, typing in documents. Part of our program will be fully automated document generation, but another part will be all about inserting of text blocks (fixed or completed with user input). We want to avoid that the user has to open our program and keep it open as a window, but tucking it away only to revive it once in a while, when he wants to generate and insert text blocks in his active document.

Sounds confusing? Don't worry. It will be clear to you soon.

What are we going to build?

We are going to build a regular Windows application using a standard windows form. On this form, we will place and build a context menu (a menu that can have multiple layers). Now, instead of running this form upon start up in the regular manner, we will tell Windows to start it minimized and to only show an icon in the Windows tray. Our aim is to let the user open our context menu by right clicking the icon in the tray. Exactly the behavior we are looking for in order to let our user have easy access to all our - soon to build - Word automation power.

The steps to make it happen

Step 1

First, we start Visual Studio and start a new project (you can download Visual Studio Express for Desktop for free here - requires a Windows live/hotmail account -). It all starts by going to the top left screen and accessing the 'File' tab.

Step 2

Go from File → New Project.

Step 3

In the window that pops up, select Visual Basic → Windows on the left side and choose 'Windows Forms Application. You can use the name suggested in the illustration or choose your own name. Click 'OK'.

Step 4

Just as a standard operation, we rename the form's visible name from “Form1” to something more applicable. Make sure you select the form with a mouse click so you can look up the 'Text' property in the 'Properties' window.

Step 5

Next, from the 'Toolbox' on the left, select and drag the NotifyIcon to the form.

Step 6

If done right, the NotifyIcon will show up as 'NotifyIcon1' in the lower part of the form's design window. This will happen if you drop a control onto the form that does not show on the form (e.g., a database control or this notifyicon control).

Step 7

Now, we will insert some code to tell the program to minimize the form from start. Double click the form in the design view (please ignore the 'ContextMenuStrip1 in the picture, we will get to that in a moment).

Step 8

Double clicking the form will bring the code behind the form. Please note the line on top at the end. the 'Load' element in Mybase.Load tells the program to do something when the form is loaded. Since we want the form to minimize on start, that is exactly where we want to add code. Take over the code shown in the illustration and make sure that it is placed between the Private Sub Form 1 … and End Sub.

In code:

Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False

With notifyIcon1
.Icon = Me.Icon
.Visible = True
.Text = "Task Tray"
End With

This is the code that makes the program behave like we want. Start minimized and show an icon in the task tray. The only thing left to do is building a menu and enabling access through the icon in the task tray.

Step 9

Select and drag the control 'ContextMenuStrip' from the Toolbox on the left to the form. If you cannot find the ContextMenuStrip, please make sure that you expand 'All Windows Forms' in the Toolbox.

Step 10

If done right, a 'ContextMenuStrip1' will appear next to the NotifyIcon1 in the form's design view. In the form itself, a ContextMenuStrip is added in which you can fill in menu strips, both in the main strip as in sub strips. Please finalise the strip as you deem fit.

Step 11

This is my example of filling out the menu strip. This is the menu your user will see and work with. Therefore, make sure that the menu items are descriptive and logical.

Step 12

Hang on, we are almost there. You have build the menu, but the menu needs to be accessible from the task bar. We will do this by manipulating a property of the notifyicon. Select the 'NotifyIcon1' and look for the property 'ContextMenuStrip' in the Properties window. You can select the down arrow and choose the ContextMenuStrip1 we just dragged onto our form.

Now, this menu is accessible with a right click of the mouse on the task tray icon.

Step 13

Finally, we want to attach actions to the various menu items. We will not do this in this post; this is reserved for the next post where we will start to work on the first elements of our Word automation code. Just to finalize this post, we will add code so the user can exit the program via the menu.

For this purpose, we add an 'exit' element to the menu's main part.

Step 14

Double click the 'Exit' menu item, so Visual Basic brings you to the code section. Please note the end of the top line. The 'Click' item of the ExitToolstripMenuItem.Click phrase tells us that the code we will insert, will be executed upon a 'click' on the Exit part of our menu. Exactly what we want.

The code itself is simple. Just add 'End' telling the program to stop execution and terminate.

Step 15

The end result: an icon in the task tray, complete with several layers of menu. Very easy for a user to access when working on a Word document for a quick dose of Word Automation.

We are now in an excellent position to start writing the first part of the actual Word automation elements. In the next post, we will build a simple form to insert the details of a party to a Word document.

Computing A-Z | Programming | Software


QR Code
QR Code programming_microsoft_word_-_03_-_run_program_from_the_tray (generated for current page)
 

Advertise with Anonymous Ads