G
Guest
Converting an app. that prints on several different printers for differnt
purposes. Like Invoices go to this printer, reports to that one, etc.
Figured to keep these in My.Settings, and provide a simple dialog to update
them. Put 4 ComboBoxes on a dialog (all configured as DropDownLists). In
the form load event, build an array with all the printers known to the system
and set the datasource for all 4 comboboxes to this array. I really don't
understand why when I select a given printer in one of the comboboxes, the
other 3 change also. Of course I can always create 3 more identical arrays
and bind each combobox to a differnt one, but once again- why? Just in case,
I have included the code:
Public Class PrintersDialog
Private Printers() As String
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles OK_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
My.Settings.BigLabelPrinter = BigLabelComboBox.SelectedItem.ToString
My.Settings.InvoicePrinter = InvoiceComboBox.SelectedItem.ToString
My.Settings.ReportPrinter = ReportComboBox.SelectedItem.ToString
My.Settings.SmallLabelPrinter =
SmallLabelComboBox.SelectedItem.ToString
Me.Close()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Private Sub PrintersDialog_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim PrinterCount As Integer =
System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count
ReDim Printers(PrinterCount) ' leave an extra place for <None>
Dim i As Integer
For i = 0 To
System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count - 1
Printers(i) =
System.Drawing.Printing.PrinterSettings.InstalledPrinters.Item(i)
Next
Printers(PrinterCount) = "<None>"
Array.Sort(Printers)
ReportComboBox.DataSource = Printers
SelectItem(ReportComboBox, My.Settings.ReportPrinter)
InvoiceComboBox.DataSource = Printers
SelectItem(InvoiceComboBox, My.Settings.InvoicePrinter)
BigLabelComboBox.DataSource = Printers
SelectItem(BigLabelComboBox, My.Settings.BigLabelPrinter)
SmallLabelComboBox.DataSource = Printers
SelectItem(SmallLabelComboBox, My.Settings.SmallLabelPrinter)
End Sub
Private Sub SelectItem(ByVal cb As ComboBox, ByVal s As String)
Dim i As Integer
i = Array.IndexOf(Printers, s)
If i = -1 Then
cb.SelectedIndex = 0
Else
cb.SelectedIndex = i
End If
End Sub
End Class
purposes. Like Invoices go to this printer, reports to that one, etc.
Figured to keep these in My.Settings, and provide a simple dialog to update
them. Put 4 ComboBoxes on a dialog (all configured as DropDownLists). In
the form load event, build an array with all the printers known to the system
and set the datasource for all 4 comboboxes to this array. I really don't
understand why when I select a given printer in one of the comboboxes, the
other 3 change also. Of course I can always create 3 more identical arrays
and bind each combobox to a differnt one, but once again- why? Just in case,
I have included the code:
Public Class PrintersDialog
Private Printers() As String
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles OK_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
My.Settings.BigLabelPrinter = BigLabelComboBox.SelectedItem.ToString
My.Settings.InvoicePrinter = InvoiceComboBox.SelectedItem.ToString
My.Settings.ReportPrinter = ReportComboBox.SelectedItem.ToString
My.Settings.SmallLabelPrinter =
SmallLabelComboBox.SelectedItem.ToString
Me.Close()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Private Sub PrintersDialog_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim PrinterCount As Integer =
System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count
ReDim Printers(PrinterCount) ' leave an extra place for <None>
Dim i As Integer
For i = 0 To
System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count - 1
Printers(i) =
System.Drawing.Printing.PrinterSettings.InstalledPrinters.Item(i)
Next
Printers(PrinterCount) = "<None>"
Array.Sort(Printers)
ReportComboBox.DataSource = Printers
SelectItem(ReportComboBox, My.Settings.ReportPrinter)
InvoiceComboBox.DataSource = Printers
SelectItem(InvoiceComboBox, My.Settings.InvoicePrinter)
BigLabelComboBox.DataSource = Printers
SelectItem(BigLabelComboBox, My.Settings.BigLabelPrinter)
SmallLabelComboBox.DataSource = Printers
SelectItem(SmallLabelComboBox, My.Settings.SmallLabelPrinter)
End Sub
Private Sub SelectItem(ByVal cb As ComboBox, ByVal s As String)
Dim i As Integer
i = Array.IndexOf(Printers, s)
If i = -1 Then
cb.SelectedIndex = 0
Else
cb.SelectedIndex = i
End If
End Sub
End Class