Welcome, in the last tutorial we began this project (Creating a better printer GUI with Visual Studio .Net and C#) on creating a better gui for handling multiple printers. We are going to just jump right back in where we left off so I hope you’re ready to do some coding again my friend. Below is an image of our printer form up to this point.

(click to enlarge)

Let’s start by renaming all of our add/remove buttons with a descriptive label of the printer they will be referencing. So for the top left button (just to the right of the General label) rename that button to “btnAddGeneral” and the button to the right of it to “btnRemGeneral”. Next just below and to the right of the Paycheck label name that one “btnAddPaycheck” and the remove button to its right “btnRemPaycheck” and finally the Workorder add button to “btnAddWorkorder” and the remove button to “btnRemWorkorder”.
Okay, the next thing we are going to do is to create the button event handlers for adding/removing printers from our listBox to our textbox controls. Lets begin by double clicking on the “btnAddGeneral” and in the code behind type this.

What this code is stating is if we have a listBox item selected and the txtGeneral.Text is empty or nothing then set the controls text equal to the selected printer. The reason we do this is because if the text entry was not empty we don’t want to allow the user to overwrite it unless they first remove the printer name that is already there which leads us to our next bit of code. Double click on the “btnRemGeneral” to create its event handler and then add this code.

And the handler code will now look like this:

All that code is doing is simply removing all the text from the entry. Next, do the same thing for the other 4 text controls and when your done it should look like the code below.

The next thing we need to do is to create the form load event by double clicking on the form frame or by clicking on the lightening bolt in the properties window and going down to the form_Load event and double clicking on the blank line. Once you have the event created add the getPrinters(); code to it. It should now look like this. This will run our method getPrinters when the form is loading and populate our listBox with all available local/network printers.

Fantastic, we are almost done. The next thing we need to do is add the code to save our selected printers to our configuration file(ApplicationName_exe.config)
Now type this code underneath your FillListboxes method from part 1.

Very good, ok don’t let this code scare you. All we are doing is opening a new StreamWriter then checking to see if a printer config file exists. If so we want to delete it because we are saving a new one. Then we are just parsing the label text to remove the semicolon and then taking the result adding an “=” sign and then the printer value of the selected printer. That’s it my friend, that’s all there is to it.

Actually we could take this code much further and I hope you do because we did not add any error handling code plus this code could easily be improved. Maybe next time I will show you how to extract printer value form your config file and how to use it in actually printing a report.

This gui is meant to be used when you are dealing with multiple printers and want an easy way to handle the assigning, storing, and retrieval of all of them. I hope this tutorial helped you and you had a little fun while coding along with it.

Download Source Files