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

Category: Visual Studio.NET

Creating a better printer GUI with Visual Studio .Net and C#

Welcome, in this tutorial we are going to create a simple but effective gui for your .Net winform application. To begin open up VS and create a new windows application and name it “MyPrinterGUI” and click “OK”. Next resize Form1.cs to 734, 406 by setting the height and width properties or by dragging the bottom right hand edge of the form downward and outward.

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!


Next drag and drop a Listbox control and set its size to 286, 303 and set the location to 12, 12. After that place 6 buttons and 3 textbox controls on the form. Set the button size to 23, 23 for all of the buttons and for the textboxes set their size to 223, 20.(There will be an add and remove button for each printer) Set the location for the 1st textbox to 445, 13 and then place the other 2 directly below. Next add 2 more buttons, set the text for the 1st one to “OK” and “Cancel” for the 2nd. Set the location for the “OK” button to 424, 301 and 561, 301 for the “Cancel” button. Leave their height and width at the default size.  Now add 3 label controls to your form and name them “General”, “Paycheck”, and “WorkOrder” or whatever your situation calls for.
Next, hold down the control(Ctrl) key and left click on each of the 3 textboxes to select them all and then from the File menu click on “Format”, “Vertical Spacing”, “Make Equal” and this will spread the controls equally between each other(space between them will be equal). After that move the labels and buttons a little to align them with your textboxes.(See image below)
 


[Click to see full-size]


The listbox control is where we are going to display our local and network printers. The user will then select a printer for each of the 3 printers to populate the textbox controls.

 

Ok, so far so good. Now lets go ahead and add the namespaces we will be using. Enter these at the top of your Form1.cs file.
 

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Configuration;
using System.Data;

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.


The application configuration file or App.config file for your application needs to have a key value entry which we will need when the application reads the configuration file for our printer gui. So go ahead and place this entry in your app.config file.

 


 


Next create these 3 strings in the Form1.cs class.

private string m_PrinterCfigFile = "";
private string m_PrinterConfigFileName = "";
private string m_Directory = "";



Note:This entry should all be one line but is wrapping here in the document. Ok now let’s create our class for populating the local/network printers which we will display in our listBox1 control.

#region getPrinters
public void getPrinters()
{
PrintDocument prtdoc = new PrintDocument();
foreach (String strPrinter in PrinterSettings.InstalledPrinters)
{
listBox1.Items.Add(strPrinter);
}
m_Directory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
m_PrinterConfigFileName = ConfigurationSettings.AppSettings.Get
("Application.Reports.PrinterConfigurationFileName");
m_PrinterCfigFile = m_Directory + "\\" + m_PrinterConfigFileName;
string input = null;
try
{
if (File.Exists(m_PrinterCfigFile) == true)
{
File.Copy(m_PrinterCfigFile, "PrinterConfig.BAK", true);
StreamReader re = File.OpenText(m_PrinterCfigFile);
while ((input = re.ReadLine()) != null)
{
FillListboxes(input);
}
re.Close();
}
else
{
//Do Nothing
}
}
catch (Exception)
{
//MessageBox.Show("There was an error loading the PrinterConfig.ini");
StreamWriter sw = new StreamWriter(m_PrinterCfigFile);
}
}
#endregion

 

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Next we’ll create the FillListboxes method that is referenced above.

#region FillListboxes
public void FillListboxes(string text)
{
//MessageBox.Show("Doing FillListboxes");
string mysrng = text.Substring(0, 8);
switch (mysrng)
{
case "GENERAL=":
txtGeneral.Text = (text.Remove(0, 8));
break;
case "PAYCHECK":
//string newstring = ( text.Remove(0, 9) );
txtPaycheck.Text = (text.Remove(0, 9));
break;
case "WORKORDE":
txtWorkorder.Text = (text.Remove(0, 10));
break;
}
}
#endregion

As you can see the control names our self descriptive although feel free to name your controls as you wish.

The next thing we need to do is add code to our “OK” button and our “Cancel” button to give them the correct functionality. For the “OK” button add this.
 

SaveFile(m_PrinterCfigFile);
Application.Exit();
And for the “Cancel” button add this code.
Application.Exit();



No matter which button the user clicks on we want to close the application but if they click the “OK” button we first want to save the printer information and then close the app out. (Please note that if this printer config gui were in a multi form application you would just close the form instead of exiting the application)

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

Ok, wow that was quite a bit for now. Congratulations on getting this far and please look for my next tutorial which will be Creating a better printer GUI with Visual Studio .Net and C# Part 2

Continue reading Part 2 of this article

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!