S
S
sensboston2013-05-20 16:14:37
C++ / C#
sensboston, 2013-05-20 16:14:37

Binding ComboBox.SelectedIndex to Properties.Settings.Default

I am doing a small refactoring of the project on WinForms (.NET 4.0 client profile), I want to get rid of ugly pieces like manually remembering SelectedIndex combo boxes in settings on the SelectedIndexChanged event (i.e. “bind” SelectedIndex to Settings in the same way as TextBox.Text or CheckBox.Checked etc.)
For a strange reason, Microsoft doesn't provide a way to "bind" SelectedIndex or SelectedValue through the UI, however if I add code like this:

public MainForm() 
{
    InitializeComponent();
    comboBox1.DataBindings.Add(new Binding("SelectedIndex", Properties.Settings.Default, "SelIndex1", false, DataSourceUpdateMode.OnPropertyChanged));

then everything works as expected (i.e. SelectedIndex is automatically stored in Properties.Settings.Default.SelIndex1), except for one unpleasant thing: each change to the ComboBox generates 6 non-fatal exceptions in system assemblies:
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in
System.Windows.Forms.dll
in System.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System .ArgumentOutOfRangeException' occurred in System.dll

I think that the matter is in the absence of a value converter for the system int (the SelIndex1 property is described as int), but I don’t know how to fix it. I want to do it elegantly (how to do it, as usual - I know :) ). Yes, you still need the solution to work under Mono on Linux.
Those. something like an extension of the standard ValueConverters or some other hack.
Thanks in advance!

PS It is necessary under WinForms , not Silverlight or WPF (there it works "on time").

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
sensboston, 2013-05-20
@sensboston

I found and fixed the problem, but its essence is still beyond my understanding :) I have a NumericUpDown component on the form, it had Minimum == 3. Yes, what’s more important is that I have my own, custom SettingsProvider (for “portability” ). So, when changing the ComboBox, for some reason it fell on

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("0")]
        public int LogLevel {
            get {
                return ((int)(this["LogLevel"]));
            }
            set {
                this["LogLevel"] = value;
            }
        }

Setting NumericUpDown's Minimum to 0 solved the problem. But all this is strange! ..
Here is the complete
stack trace:
System.Windows.Forms.dll!System.Windows.Forms.NumericUpDown.Value.set(decimal value) + 0x1af bytes
[Native to Managed Transition]
System.dll!System.SecurityUtils.MethodInfoInvoke(System.Reflection.MethodInfo method, object target, object[] args) + 0x65 bytes
System.dll!System.ComponentModel.ReflectPropertyDescriptor.SetValue(object component, object value) + 0x11b bytes
System.Windows.Forms.dll!System.Windows.Forms.Binding.SetPropValue(object value) + 0xdb bytes
System.Windows.Forms.dll!System.Windows.Forms.Binding.PushData(bool force) + 0xa0 bytes
System.Windows.Forms.dll!System.Windows.Forms.BindingManagerBase.PushData(out bool success) + 0x5b bytes
System.Windows.Forms.dll!System.Windows.Forms.PropertyManager.OnCurrentChanged(System.EventArgs ea) + 0x1b bytes
System.Windows.Forms.dll!System.Windows.Forms.BindToObject.PropValueChanged(object sender, System.EventArgs e) + 0x29 bytes
System.dll!System.ComponentModel.PropertyDescriptor.OnValueChanged(object component, System.EventArgs e) + 0x54 bytes
System.dll!System.ComponentModel.ReflectPropertyDescriptor.OnValueChanged(object component, System.EventArgs e) + 0x63 bytes
System.dll!System.ComponentModel.ReflectPropertyDescriptor.OnINotifyPropertyChanged(object component, System.ComponentModel.PropertyChangedEventArgs e) + 0x82 bytes
System.dll!System.Configuration.ApplicationSettingsBase.OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) + 0x1a bytes
System.dll!System.Configuration.ApplicationSettingsBase.this[string].set(string propertyName, object value) + 0x96 bytes
> TinyOPDS.exe!TinyOPDS.Properties.Settings.LogLevel.set(int value) Line 239 + 0x37 bytes C#
[Native to Managed Transition]
System.dll!System.SecurityUtils.MethodInfoInvoke(System.Reflection.MethodInfo method, object target, object[] args) + 0x65 bytes
System.dll!System.ComponentModel.ReflectPropertyDescriptor.SetValue(object component, object value) + 0x11b bytes
System.Windows.Forms.dll!System.Windows.Forms.BindToObject.SetValue(object value) + 0x63 bytes
System.Windows.Forms.dll!System.Windows.Forms.Binding.PullData(bool reformat, bool force) + 0x159 bytes
System.Windows.Forms.dll!System.Windows.Forms.Binding.Target_PropertyChanged(object sender, System.EventArgs e) + 0x2c bytes
System.Windows.Forms.dll!System.Windows.Forms.ComboBox.OnSelectedIndexChanged(System.EventArgs e) + 0x9d bytes
System.Windows.Forms.dll!System.Windows.Forms.ComboBox.WmReflectCommand(ref System.Windows.Forms.Message m) + 0x13b bytes
System.Windows.Forms.dll!System.Windows.Forms.ComboBox.WndProc(ref System.Windows.Forms.Message m) + 0x8d8 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x13 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x64 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Control.SendMessage(int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x24 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ReflectMessageInternal(System.IntPtr hWnd, ref System.Windows.Forms.Message m) + 0x3b bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WmCommand(ref System.Windows.Forms.Message m) + 0x2e bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x387 bytes
System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m) + 0x2a bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x13 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x64 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) + 0x6d bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.DefWndProc(ref System.Windows.Forms.Message m) + 0xc bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WmCommand(ref System.Windows.Forms.Message m) + 0x46 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x387 bytes
System.Windows.Forms.dll!System.Windows.Forms.ComboBox.WndProc(ref System.Windows.Forms.Message m) + 0x882 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x13 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x64 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) + 0x287 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x16c bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes
TinyOPDS.exe!TinyOPDS.Program.Main() Line 55 + 0x8 bytes C#
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6d bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2a bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes
[Native to Managed Transition]

S
sensboston, 2013-05-21
@sensboston

Seem to be

But it doesn’t look like it ’s not attached! :) This is incomprehensible.
PS Today or tomorrow I'll commit the code, you can check it yourself (if you're interested, of course).

S
sensboston, 2013-05-21
@sensboston

Update: all figured out. It was the default value for NumericUpDown - it was more than the minimum :D And in the SettingsProvider it was implemented through foreach, so when updating any control bound to settings, an error occurred.

M
mayorovp, 2013-05-20
@mayorovp

I think that the point is the lack of a value converter for the system int

There is a value converter for the system int. Look elsewhere

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question