combobox

This forum is meant for examples of X# code.

Post Reply
ngpollarolo
Posts: 20
Joined: Mon Feb 29, 2016 3:51 pm
Location: Nicaragua

combobox

Post 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#?
User avatar
wriedmann
Posts: 3759
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: combobox

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
ngpollarolo
Posts: 20
Joined: Mon Feb 29, 2016 3:51 pm
Location: Nicaragua

Re: combobox

Post 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
User avatar
wriedmann
Posts: 3759
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: combobox

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4926
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: combobox

Post 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})
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply