Windows Forms ComboBox

This forum is meant for examples of X# code.

Post Reply
leighproman
Posts: 60
Joined: Tue Oct 11, 2016 8:56 pm
Location: UK

Windows Forms ComboBox

Post by leighproman »

Have started converting some VO forms to Windows Forms and am having trouble with ComboBox.

Could someone point me to an example of how to set values for ComboBox items?
(equivalent of myComboBox:AddItem(name,0,value) or myComboBox:FillUsing({name,value}) in VO)

Thanks

Leigh
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Windows Forms ComboBox

Post by Phil Hepburn »

Hi Leigh,

Try the method shown in the images - I did these last week for my printing stuff.
LAquery_01.jpg
LAquery_01.jpg (59.97 KiB) Viewed 739 times
LAquery_02.jpg
LAquery_02.jpg (25.72 KiB) Viewed 739 times
Cheers and Good Luck,
Phil.
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Windows Forms ComboBox

Post by Chris »

Hi Leigh,

The ComboBox/ListBox in .Net does not have the context of separate "value" properties, but the items can encapsulate the values themselves. You are not restricted on adding strings, you can add instances of any kind of type as items and the control will show their ToString() representation. For example you can create a class

Code: Select all

CLASS ListItem
	PROTECT _oValue AS OBJECT
	PROTECT _cText AS STRING
	CONSTRUCTOR(cText AS STRING , oValue AS OBJECT)
		SELF:_cText := cText
		SELF:_oValue := oValue
	RETURN
	PROPERTY @@Value AS OBJECT GET SELF:_oValue
	OVERRIDE METHOD ToString() AS STRING
	RETURN SELF:_cText
END CLASS
and add items with combo:Items:Add(ListItem{"string rep" , anyvalue}). When you want to read the "value" of the combobox, get the selected item, cast it to ListItem and get the "Value" property.

Of course you can also create your own ComboBox some class which encapsulates the above and makes it work in a similar way with the ComboBox in VO.

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
leighproman
Posts: 60
Joined: Tue Oct 11, 2016 8:56 pm
Location: UK

Windows Forms ComboBox

Post by leighproman »

Thanks for the help - all working now.

Leigh
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Windows Forms ComboBox

Post by Phil Hepburn »

Hi Chris,

While we are on this subject, can you please inform / enlighten me as to why I had to make some code changes from a working older Vulcan version of an app.

In the Vulcan version the code was <int>{1,2,3 etc.} and also <double>{....} for the Zoom code line.

This would not compile / work ? for me and I had to adjust the code to <object> and then cast the selected item.

Should this be the case - what was different from the old code version I was copying ?

Once I got it up and running I just got on with Printing and Reports, and forgot to ask you or to feed back.

Any ideas for me ?
Cheers,
Phil.
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Windows Forms ComboBox

Post by Chris »

Hi Phil,

Do you mean you needed that when you used AddRange()? This method accepts a parameter of type object[], this is why you got an error. c# reports the same error, apparently Vulcan allowed it, not sure if it was accidental or intentional, but I suspect it was accidental.

Even if you could get away with compiling it with int[], the combobox class still holds its items as objects, so you'd need the cast to int anyway. Or are you talking about WPF and there the combobox class is different?

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Windows Forms ComboBox

Post by Phil Hepburn »

Hi Chris,

You seem to have it right - its WinForms stuff not WPF. The WPF example came after the one I simply converted from the past work I had done. The print Preview tools are both WinForm technology.

It looks like Vulcan did something a bit different, and as you say, we need to cast back the other way anyhow. So no big deal - except that the code failed first off.

I just thought in the spirit of 'community' that I needed to let you know what I found out.

Anyway, since I posted this info I had a VERY serious Windows 10 update, or new driver issue. The blue / green screens of death, happened in a 'blink', and no matter what I tried I could not get a reboot with any safety option - it just went around and around in a cycle ;-0((

Anyway, after a whole day and an evening I removed the boot device and have rebuilt my system without needing to start with a fresh Windows install (thank God) - and as you can see I can now post from Vivaldi.

The good news is that I now have both a working VS 2015 and the latest VS 2017 up and running, and with X# 1.2 as well. My Printing and Reporting apps run correctly as well - phew!

Needless to say that my Cologne material for sessions etc., is safely spread around - here, there, and everywhere. Also a BIG thanks to 'OneDrive' has to be said. What did we do before SkyDrive - now OneDrive ?

I have read the posted news regarding Grafxsoft and feel that it seems like an honest and fair statement of how things appeared to me.

It is also nice to get back more fully to a sense of 'community' and us all trying to help each other.

So onward and upwards :cheer: ;)

I will try and do my bit to contribute and support.

Best regards,
Phil.
Wales, UK.
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Windows Forms ComboBox

Post by Chris »

Hi Phil,

Yes, of coure, it's very good that you reported this! I just explained that this was probably a small bug of vulcan that had allowed it to compile in the first place.

About MS, Windows and its Updates, I think I'd better not comment, you said it all :)

Chris
Chris Pyrgas

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