I have an editable ComboBox with a validation on the Text
property to make sure manually entered info is valid.
EDIT: All I want to do is populate the .Text
property with the ValueMember
of a selection rather than the DisplayMember
I also have the .Items
populated 开发者_开发技巧with valid entries having the DisplayMember
and ValueMember
set.
My DisplayMember
is a caption along with the data, and the ValueMember
is the data itself.
So Items
might be:
(DisplayMember, ValueMember)
"Foo - 1ab2" , "1ab2"
"Bar - 3cd4" , "3cd4"
I had a validation on the text which can also validate manual user input like "5ef6"
The problem I'm having is that if the user selects an item from the combobox it populates the text field with the DisplayMember
property (ex: "Foo - 1ab2") which will fail validation.
I have tried to manually set the .Text
property with the SelectedValue
or the SelectedItem.Value
on each of the three relevant combobox events to no avail.
I would like that the .Text
of the ComboBox get populated with the .ValueMember
of the item when selected rather than the .DisplayMember
EDIT: I cannot validate by trying to extrapolate the value from the caption. I send the Text
off to a service to be validated.
void FillMyCombo
{
KeyValuePair<string, string> listValue1 = new KeyValuePair<string, string>("Foo - 1ab2" , "1ab2")
KeyValuePair<string, string> listValue2 = new KeyValuePair<string, string>("Bar - 3cd4" , "3cd4")
myCombo.Items.Add(listValue1);
myCombo.Items.Add(listValue2);
myCombo.DisplayMember = "Key";
myCombo.ValueMember = "Value";
}
...
void myCombo_TextUpdated
{
if(!myValidationService.Validate(myCombo.Text))
{
do error stuff
}
}
The user can manually enter something like "5ef6" which will pass validation.
But when they select an item from the list, rather than manually entering it, the .Text
property gets filled with the caption and not the value ... so it will contain "Foo - 1ab2" and that will fail validation.
EDIT: In response to an answer posted: I cannot change the validation method to "infer" the value from the caption. I have no control over that service. All I'm after is the displayed value
EDIT: Say a user selects "Foo - 1ab2" from the dropdown list, I want the text in the box to say "1ab2"
EDIT: I have also tried to set the .Text
property in code but I can't seem to make it work in any of the ComboBox events. If anybody can answer how to programmaticly set the .Text
property (and make it commit!) on a selection event they will also answer this question.
What about http://nickstips.wordpress.com/2010/11/19/c-datagridviewcomboboxcolumn-displaying-different-values-in-drop-down-list/ -- they change ValueMember
and DisplayMember
on the fly upon dropdown opening/closing.
Example is for DatagridViewComboBox
. A ComboBox has those events, too, no?
Give it a try and downvote if it does not help :)=
** Update **
Another good-looking solution might be ArgumentException when adding ComboBox column to DataGridView with same DataSource, there look at the not-accepted answer.
Simply validate the .SelectedText
property of the ComboBox
instance. .Text
will always contain what the user sees, i.e. the display property's value:
void myCombo_TextUpdated
{
if(!myValidationService.Validate(myCombo.SelectedText))
{
do error stuff
}
}
SelectedText
gets or sets the text that is selected in the editable portion of a ComboBox
, I think even if SelectedIndex=-1/SelectedValue=Null.
精彩评论