Setting the Text property of a DropDownList ComboBox to an invalid value doesn't raise exception? -


I'm looking at ways to solve a problem with the Winforms application, which uses the combo box control special As a form, the combo box (style = dropdown list) is bound to a data source and, because the user navigates through any other data, the "text" property of the combo box property is set - And the user can choose some other value.

Trouble starts when I have not specified the "text" attribute in the list of available items. It seems that nothing happens, take the following simple examples:

  Public Partial Sections Form 1: Form {Public Form 1 () {Initial Group (); MyComboBox1.DropDownStyle = ComboBoxStyle.DropDownList; //myComboBox1.Items.AddRange (new [] ("one", "two", "three"}; list & lt; keyValuePair & lt; int32, string & gt; & gt; value = new list & lt; KeyvaluePair & lt; Int32, string & gt; & gt; (); Values.Add (New Key Val Peer & lt; Int32, String & gt; (1, "one"); Values.Add (New KeyWeeker & lt ;); Values.Add (new keyweeper  (3, "three"); MyComboBox1.DataSource = value; MyComboBox1.ValueMember = "Key"; MyComboBox1.DisplayMember = "Value"; Button 1. Click = (S, E) => {MyComboBox1.Text = "Four";}; Button 2. Click + = (S, E ) = & Gt; {MyComboBox1.SelectedIndex - = 1;};}} Public Works C MyComboBox: System.Windows.Forms.ComboBox {public override string text {get {return base.Text;} set {MessageBox.Show (value); Base.Text = value;}}}  

This technique is used across a large application, so when it was seen that (in the example above) "text" is not "four" "nothing, I thought It was possible that I could frustrate it and throw an exception. In reality, such code is pasted in such a way:

  if (myDataRow.IsBlahNull ()) myComboBox1.Text = ""; Other myComboBox1.Text = myDataRow.Blah;  

Now, when I appreciate the setting of the "SelectIndex = 1" setting, the "IsNull" case would be better, the fact is that myDataRow.Blah can not be a valid value is. In addition, the app is written (and live) so that fewer changes are better.

So, my immediate idea was to "Override text property setter and check that the value is in the list" that, it has been found, it is not as easy as it is easy Will happen. The problem is that the "text" property is set in all sorts of situations, in all circumstances, for example, it is set when the datasource property is assigned, or when the selected index is set to 1, besides it Represents the string of the selected item - so if you have the combo box control that is connected to a list of key value pairs, you'll get "[key, value ] "To get something like" text "property set. If it is bound for datatable / datvice, then you get the representation of string of data arrows, and it becomes difficult to find out.

At this time I thought that there could be another way to get the desired result (which is to find the text property setting at some invalid value - which does nothing) .

Any thoughts?

After reflection, is this a fair job?

  /// & lt; Summary & gt; /// sets or sets the text associated with this control /// & lt; / Summary & gt; Public override string text {return base. Text; } Set {base.Text = value; If ((value! = Null) & amp; (base.Text! = Value)) if (value == "") this.SelectedIndex = -1; And express new logic (text property of "string {format} can not be set to" {1} \ ".", This.Name, value)); }}  

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -