Skip to content

Commit

Permalink
simplified code + exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
limenet committed Jun 4, 2017
1 parent 70a6432 commit b33b7a9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 44 deletions.
19 changes: 4 additions & 15 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,10 @@ private void Form1_Load(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();

if (args.GetLength(0) == 3)
if (args.GetLength(0) >= 3)
{
(new PrinterAPI(args[1], args[2])).print();
Close();
}
else if (args.GetLength(0) == 4)
{
(new PrinterAPI(args[1], args[2], args[3])).print();
Close();
}
else if (args.GetLength(0) == 5)
{
(new PrinterAPI(args[1], args[2], args[3], Int32.Parse(args[4]))).print();
Close();
int exitCode = (new PrinterAPI(args[1], args[2], args.Length > 3 ? args[3] : null, args.Length > 4 ? Int32.Parse(args[4]) : 1).print()) ? 0 : 1;
Program.ExitApplication(exitCode);
}
}

Expand All @@ -41,8 +31,7 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
Close();
return true;
Program.ExitApplication(0);
}
else if (keyData == Keys.Enter)
{
Expand Down
31 changes: 9 additions & 22 deletions PrinterAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,18 @@ class PrinterAPI
{
private string title;
private string barcode;
private string timestamp = System.DateTime.Now.ToString("dd.MM.yyyy HH:mm");
private int count = 1;
private string timestamp;
private int count;

public PrinterAPI(string title, string barcode)
public PrinterAPI(string title, string barcode, string timestamp = null, int count = 1)
{
this.title = title;
this.barcode = barcode;
}

public PrinterAPI(string title, string barcode, string timestamp)
{
this.title = title;
this.barcode = barcode;
this.timestamp = timestamp;
}

public PrinterAPI(string title, string barcode, string timestamp, int count)
{
this.title = title;
this.barcode = barcode;
this.timestamp = timestamp;
this.timestamp = timestamp != null ? timestamp : DateTime.Now.ToString("dd.MM.yyyy HH:mm");
this.count = count;
}

public void print()
public bool print()
{
bpac.DocumentClass doc = new DocumentClass();
string templatePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + doc.Printer.GetMediaId().ToString() + ".lbx";
Expand All @@ -47,11 +34,11 @@ public void print()
doc.PrintOut(count, printOptions);
doc.EndPrint();
doc.Close();
return true;
}
else
{
MessageBox.Show("Open() Error: " + doc.ErrorCode + "\nMedia ID" + doc.Printer.GetMediaId().ToString());
}

MessageBox.Show("Open() Error: " + doc.ErrorCode + "\nMedia ID" + doc.Printer.GetMediaId().ToString());
return false;
}
}
}
18 changes: 13 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BpacBarcode
{
static class Program
{
/// <summary>
/// Main entry point of application.
/// </summary>
static int exitCode = 0;

[STAThread]
static void Main()
static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new BarcodeForm());
string[] args = Environment.GetCommandLineArgs();

return exitCode;
}

public static void ExitApplication(int exitCode = 0)
{
Program.exitCode = exitCode;
Application.Exit();
}

}
}
7 changes: 5 additions & 2 deletions bpac-barcode.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
# Visual Studio 15
VisualStudioVersion = 15.0.26430.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bpac-barcode", "bpac-barcode.csproj", "{774FC6F2-E85F-4A44-AF1C-CD8765F35BC1}"
EndProject
Global
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
Expand Down

0 comments on commit b33b7a9

Please sign in to comment.