Creating a Better Printer GUI in VS.NET and C# Pt2

Category: Visual Studio.NET

Creating a better printer GUI with Visual Studio .Net and C# (Part 2)

This article is part two in the series. Read Part 1 here

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.
 

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!


[Click to see full-size]



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.

if (listBox1.SelectedIndices.Count > 0)
{
if (txtGeneral.Text.ToString() == "")
{
txtGeneral.Text = listBox1.SelectedItem.ToString();
}
}
Just to make sure you have it right your entire code for that button event should look like this.
private void btnAddGeneral_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndices.Count > 0)
{
if (txtGeneral.Text.ToString() == "")
{
txtGeneral.Text = listBox1.SelectedItem.ToString();
}
}
}


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.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

txtGeneral.Text = "";


And the handler code will now look like this.

private void btnRemGeneral_Click(object sender, EventArgs e)
{
txtGeneral.Text = "";
}


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.

 

 

private void btnAddGeneral_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndices.Count > 0)
{
if (txtGeneral.Text.ToString() == "")
{
txtGeneral.Text = listBox1.SelectedItem.ToString();
}
}
}

        private void btnAddPaycheck_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndices.Count > 0)
{
if (txtPaycheck.Text.ToString() == "")
{
txtPaycheck.Text = listBox1.SelectedItem.ToString();

                }
}
else
{
// Add error code here
}
}

        private void btnAddWorkorder_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndices.Count > 0)
{
if (txtWorkorder.Text.ToString() == "")
{
txtWorkorder.Text = listBox1.SelectedItem.ToString();

                }
}
else
{
// Add error code here
}
}
private void btnRemGeneral_Click(object sender, EventArgs e)
{
txtGeneral.Text = "";
}

        private void btnRemPaycheck_Click(object sender, EventArgs e)
{
txtPaycheck.Text = "";

        }

        private void btnRemWorkorder_Click(object sender, EventArgs e)
{
txtWorkorder.Text = "";
}


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.

 

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

private void Form1_Load(object sender, EventArgs e)
{
getPrinters();
}

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.

#region SaveFile
private void SaveFile(string myfile)
{
StreamWriter sw = null;

 

            try
{
// Check if file exists
if (File.Exists(myfile))
{
//MessageBox.Show("File Exists");
File.Delete(myfile);
}

                // Create a new file for text output
sw = File.CreateText(myfile);
// Add the [Printers] text to the file
string printers = "[Printers]";
sw.WriteLine(printers);
string payCheckprinter = txtGeneral.Text.ToString();
string generalPrinter = txtPaycheck.Text.ToString();
string workOrderPrinter = txtWorkorder.Text.ToString();
if (generalPrinter.Length > 0)
{
//Remove the semi colon from the string
string MyString = lblGeneral.Text.ToString();
char[] MyChar = { ':' };
string NewString = MyString.TrimEnd(MyChar);
//Write the line to file
sw.WriteLine(NewString.ToUpper(System.Globalization.CultureInfo.CurrentCulture) + "=" +      generalPrinter);
}
if (payCheckprinter.Length > 0)
{
//Remove the semi colon from the string
string MyString = lblPaycheck.Text.ToString();
char[] MyChar = { ':' };
string NewString = MyString.TrimEnd(MyChar);
//Write the line to file
sw.WriteLine(NewString.ToUpper(System.Globalization.CultureInfo.CurrentCulture) + "=" + payCheckprinter);

                }
if (workOrderPrinter.Length > 0)
{
//Remove the semi colon from the string
string MyString = lblWorkorder.Text.ToString();
char[] MyChar = { ':' };
string NewString = MyString.TrimEnd(MyChar);
//Write the line to file
sw.WriteLine(NewString.ToUpper(System.Globalization.CultureInfo.CurrentCulture) + "=" + workOrderPrinter);
}

            }
catch (Exception e)
{
MessageBox.Show(e.Message);

            }
finally
{
if (sw != null)
sw.Close();
}
}
#endregion

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

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.

Take care,
JR

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!