Page 1 of 1
combobox
Posted: Tue Dec 05, 2023 10:31 pm
by ngpollarolo
Is it possible to get an example of creation and use of a combobox with items type keyvaluepair in a winform in x#?
Re: combobox
Posted: Wed Dec 06, 2023 3:57 am
by wriedmann
Hi Giorgio,
if you are using an object as element to fill a ComboBox in Windows Forms, the following code should work:
Code: Select all
self:PassSys:DisplayMember := "Value"
self:PassSys:ValueMember := "Key"
self:PassSys:DataSource := oDict
self:PassSys:SelectedValue := oItem:PassSys
where "PassSys" is the ComboBox, "oDict" the dictionary and "oItem" the current dataitem (or model, in case you are thinking in MVVM).
This is standard Windows Forms behavior.
Wolfgang
Re: combobox
Posted: Wed Dec 06, 2023 5:52 pm
by ngpollarolo
Thank Wolfgang,
my problem is to translate from c# to x# the following code:
comboBox1.Items.Add(new KeyValuePair<string, int>("Opción 1", 1));
comboBox1.Items.Add(new KeyValuePair<string, int>("Opción 2", 2));
comboBox1.Items.Add(new KeyValuePair<string, int>("Opción 3", 3));
getting values from the array:
aTab:={}
aadd(aTab,{"giorgio",123})
aadd(aTab,{"Jose",234})
TIA
Giorgio
Re: combobox
Posted: Wed Dec 06, 2023 7:42 pm
by wriedmann
Hi Giorgio,
please forget the comboBox1.Items.Add syntax.
Code: Select all
using System.Collections.Generic
local oValues as List<KeyValuePair<int,string>>
oValues := List<KeyValuePair<int,string>>{}
oValues:Add( 123, "giorgio" )
oValues:Add( 234 "jose" )
comboBox1:DisplayMember := "value"
comboBox1:ValueMember := "key"
comboBox1:DataSource := oValues
You can use every object to fill your combobox, just fill the correct values to the DisplayMember and ValueMember properties.
Wolfgang
Re: combobox
Posted: Thu Dec 07, 2023 7:15 am
by Chris
Giorgio,
ngpollarolo wrote: ↑Wed Dec 06, 2023 5:52 pm
my problem is to translate from c# to x# the following code:
comboBox1.Items.Add(new KeyValuePair<string, int>("Opción 1", 1));
I agree with Wolfgang, his suggested way to do it is much more elegant. But just for completeness, the direct translation of your c# code to X# is this:
Code: Select all
comboBox1:Items:Add(KeyValuePair<STRING, INT>{"Opción 1", 1})