c# - Textbox not displaying value after selecting from ComboBox Dropdown -
so have combo box
, have list made using keyvaluepair<int, decimal>
. want textbox have selected show value according key when select drop-down textbox.
relevant code:
// make list of truck weight , mpg. list<keyvaluepair<int, decimal>> weightmpg = new list<keyvaluepair<int, decimal>>(); private void mainform_load(object sender, eventargs e) { decimal k = 7; (int = 20000; < 40000; i+=1000){ weightmpg.add(new keyvaluepair<int, decimal>(i, k)); k -= 0.1m; } (int = 40000; < 45000; i+=1000){ weightmpg.add(new keyvaluepair<int, decimal>(i, 5)); } weightmpg.add(new keyvaluepair<int, decimal>(46000, 4.9m)); weightmpg.add(new keyvaluepair<int, decimal>(47000, 4.8m)); weightmpg.add(new keyvaluepair<int, decimal>(48000, 4.7m)); truckweight2.datasource = weightmpg; truckweight2.valuemember = "value"; truckweight2.displaymember = "key"; } private void truckweight2_selectedindexchanged(object sender, eventargs e) { truckmpg2.text = truckweight2.valuemember; }
for code, shows dropdown 20,000 48,000 when click control. when select one, textbox (truckmpg2
) doesn't update reflect value, rather displays word "value."
i've looked @ other stack-overflow answers when making code, i'm not sure going wrong.
you're reading .valuemember
property:
truckmpg2.text = truckweight2.valuemember;
which set literal string:
truckweight2.valuemember = "value";
it sounds want .selectedvalue
property instead:
truckmpg2.text = truckweight2.selectedvalue;
or, if type doesn't match value can directly represented string, might need append .tostring()
value:
truckmpg2.text = truckweight2.selectedvalue.tostring();
Comments
Post a Comment