Binding question, really don't understand this ... once again

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
 
G

Guest

Terry,

One way to make the comboboxes independent of each other is to assign each
combobox a new binding context. For example:

ReportComboBox.BindingContext = New BindingContext
ReportComboBox.DataSource = Printers

Do that for each of the comboboxes and then they should be independent.

Kerry Moorman
 
G

Guest

Thanks, that did the job.
--
Terry


Kerry Moorman said:
Terry,

One way to make the comboboxes independent of each other is to assign each
combobox a new binding context. For example:

ReportComboBox.BindingContext = New BindingContext
ReportComboBox.DataSource = Printers

Do that for each of the comboboxes and then they should be independent.

Kerry Moorman
 
L

Linda Liu [MSFT]

Thanks Kerry for your correct answer!

Hi Terry,

I think you may need a detailed explanation of the whole thing : )

Firstly, in WinForm data binding, the navigation within a data source is
managed by a special object known as binding manager. Binding managers have
the responsibility of managing a set of bindings for a particular data
source and come in two flavors: property managers and currency managers.

A property manager is an instance of the PropertyManager class and is
created for an item data source. A currency manager is an instance of the
CurrencyManager class and is created for a list data source. Both of these
are implementations of the abstract base class BindingManagerBase.

Secondly, the BindingContext class manages the collection of
BindingManagerBase objects for any object that inherits from the Control
class. Each Windows Form has at least one BindingContext object that
manages the BindingManagerBase objects for the form. For each data source
on a Windows Form, there is a single CurrencyManager or PropertyManager.

If you want to have multiple BindingManagerBase instances for the same data
source, create a new BindingContext and set it to the BindingContext
property of an object that inherits from the Control class.

For example, if you have two BindingManagerBase objects (from two different
BindingContext objects), you can set the Position properties of each
BindingManagerBase to different values. This causes each set of data-bound
controls to display different values from the same data source.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Linda,
Thank you very much for the detailed explanation. Prior to Kerry's
explanation, I had mistakingly thought that keeping controls bound to the
same datasource in synch would require some action on my part, as opposed to
it being the default behavior of a form. It makes a lot more sence now.
Thanks again,

Terry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top