combo-box binding trouble

  • Thread starter Thread starter Alan Baljeu
  • Start date Start date
A

Alan Baljeu

comboBox1.DataSource = choices;
Binding MTBinding = new TypedBinding("Text", source, member);
comboBox1.DataBindings.Clear();
comboBox1.DataBindings.Add(MTBinding);

a) When I do the above, the comboBox1 will receive updates if "source.member" changes.
But if I change the Text (by typing), the object doesn't update (Binding.OnParse never
fires).

b) If I drop the first line, the binding works fine.

c) If I create a new property, everything works:
public string Froggy
{
get { return comboBox1.Text; }
set { comboBox1.Text = value; }
}
....
Binding MTBinding = new TypedBinding("Froggy", ....


d) What's wrong with the original as it is?

Alan
 
Alan,,

Hmmm ... don't know what I'm missing, but I can't find "TypedBinding"
anywhere. Is this your own class? Anyway, ComboBoxes should be DataBound to
the SelectedValue property, not the Text. I don't know if this what your
problem is, but try changing it and see if it helps.

~~Bonnie
 
Bonnie Berent said:
Alan,,

Hmmm ... don't know what I'm missing, but I can't find "TypedBinding"
(Sorry, "class TypedBinding : Binding { ... }" is part of my code. It doesn't matter
much.)
anywhere. Is this your own class? Anyway, ComboBoxes should be DataBound to
the SelectedValue property, not the Text. I don't know if this what your
problem is, but try changing it and see if it helps.

Using SelectedValue does the trick IF someone picks from the list. But a combo-box allows
people to type something not in the list. SelectedValue doesn't pick up on this at all.
"Text" works to handle typing, but not selection. Surely there is a property which
reflects what you see in the TextBox part of the combobox, whether it happened due to
selection or typing.

I expected Text would do it, but that seems to have problems. The fact that it works
one-way only leads me to suspect that maybe having one of my combos embedded in a
UserControl is causing a conflict.

Anyhow, a working solution is to bind custom property Foggy on the UserControl, where
Foggy sets and gets the combobox's Text property. A faulty solution is to bind Text on
the UserControl where Text sets and gets the combobox's Text property. I don't know why
the name of the property matters, but it obviously does.

Alan
 
Alan,

OK, sorry for the confusion ... I'm so used to using Combos with
DropDownStyle = DropDownList (where the user *can't* type in Text), that I
didn't notice that you were talking about the user entering Text.

So, you found a solution that works for you (by binding to a property), but
I wonder if instead you could do something with the .TextChanged event
handler. Are you using the framework version of a ComboBox or have you
sub-classed most of the UI controls? (which I recommend doing, BTW).

~~Bonnie
 
Hi Bonnie,
Can you discuss more about sub-classing the UI controls...what this
means and why you recommend it? I am new to OOP and .Net so would
appreciate any light you might shed on this subject.

TIA,
John
 
Hi John,

There are always little quirks in behavior in some of the base class
controls that you want to get around for every instance of an object.

Say, as an example, that you've developed a few forms for your application.
You've used the base class controls. At some point, you find something's not
quite acting right and you want to change that behavior or you may just want
to add something (some properties or something) to every ComboBox that you
use. Now, since you used the base class controls to begin with ... guess
what? You've outta luck!! You've got a lot of work ahead of you to change all
those. Whereas, if you had sub-classed all the controls first (even if you
haven't as yet put code in those sub-classes) and used the sub-classed
controls on your forms, then you'll have no extra work to do when you make
changes to your sub-classed control. (and yes, even the form should be
sub-classed).

Basically, you'll want a class library that contains your sub-classed UI
controls, like textbox, button, etc. Something like this:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyCompany.WinUI.MyClasses
{
public class MyComboBox : System.Windows.Forms.ComboBox
{
// code here
}

public class MyTextBox : System.Windows.Forms.TextBox
{
// code here
}

public class MyButton : System.Windows.Forms.Button
{
// code here
}
}

That's it. These controls can't be sub-classed visually, but as you can see,
it's easy enough to do it in code. I have all the basic controls sub-classed
in one class library file. Once they're added to the ToolBox, then can be
dragged onto any design surface in the IDE.

I hope that's enough to get you started, but feel free to ask me to explain
it better if you feel I haven't gotten my point across. =)

~~Bonnie
 
Hi Bonnie,
Thank you for that explaination. You have fueld my interest to study
more on sub-classing. I dont know enough to ask an intelligent
question at this time but I am sure I will as I study.

I do have one question, once I create the class as you described, how
are the sub classed components added to the tool box? And do I use
them just as I do the standard components? (i.e. drag drop, size,
align, etc)

TIA,
John
 
Darn John ... I *knew* you were gonna ask that!! <g> I should have included
it in my post. Anyway:

When you have the IDE open in the designer view (not the code view), go to
the ToolBox, right-click anywhere and choose the Add/Remove Items, click on
Browse and find the .DLL where your sub-classed controls are (you obviously
needed to have compiled the class library after you created it). That's it
.... they'll then be added to your ToolBox.

~~Bonnie
 
Hi Bonnie,
Well at least you sent me a Darn John and not a Dear John letter LOL.
Thanks for those instructions. I am eager to try it out.

John
 
Alan,
OK, sorry for the confusion ... I'm so used to using Combos with
DropDownStyle = DropDownList (where the user *can't* type in Text), that I
didn't notice that you were talking about the user entering Text.

So, you found a solution that works for you (by binding to a property), but
I wonder if instead you could do something with the .TextChanged event
handler. Are you using the framework version of a ComboBox or have you
sub-classed most of the UI controls? (which I recommend doing, BTW).

~~Bonnie
The problem is that Text doesn't change the data source when I type in the combo in the
user-control. Perhaps a TextChanged event handler could force the data source to be
updated. Another thought that comes to me - perhaps it's that BindingContext issue. I'm
guessing something about embedding the combo box isolates it from the update-binding
message that occurs when I switch forms. I don't know.

P.S. Yes, I'm subclassing these controls.
 
Back
Top