//the printing functionality is taken from https://gist.github.com/albertbaldi/7b8d86a20430e101299fe8a4eccf5deb and //modified to suit our use case private void printButton_Click(object sender, RoutedEventArgs e) { //ensure no area nor specimen empty field is chosen if (specimenList.SelectedItem == null || areaList.SelectedItem == null) { MessageBox.Show("Area and Specimen should be selected!"); return; } PrintDocument pd = new PrintDocument(); pd.DefaultPageSettings.PrinterSettings.PrinterName = printerName; pd.DefaultPageSettings.Landscape = false; pd.DefaultPageSettings.PaperSize = new PaperSize("PaperA4", 826, 1169); pd.DefaultPageSettings.Margins = new Margins(15, 15, 15, 15); pd.OriginAtMargins = true; pd.PrinterSettings.Copies = (short)specimenUsed[specimenList.SelectedItem.ToString()];//# of copies to be printed pd.PrintPage += Pd_PrintPage; try { pd.Print(); //if sampleID textbox != the id in the currentID object //and the updateSampleBox is checked, then update sampleID.json file with whatever was in the field bool update = !currentID_used.getId().Equals(sampleID.Text) && updateSampleIdBox.IsChecked == true; currentID_used.setId((update?int.Parse(sampleID.Text) + 1 : int.Parse(currentID_used.getId()) + 1));//increase the ID number by one setDefaults(); } catch (InvalidPrinterException excep) { MessageBox.Show("Printer name is invalid. Set another printer name!"); } }
//Used on area list field selection private void setFieldsOnAreaListOptionSelection(IDictionary <string, int> newSpecimenUsed, SampleID newCurrentID_used) { specimenList.ItemsSource = newSpecimenUsed.Keys; specimenUsed = newSpecimenUsed; sampleID.Text = newCurrentID_used.getId(); currentID_used = newCurrentID_used; }