473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Help creating custom ComboBox control

Hi,

I'm populating a ComboBox with objects which contain two properties -
IntegerValue and StringValue. I display StringValue in the ComboBox
form and can retrieve IntegerValue from the SelectItem.IntegerValue
property. This is all good but now I want to get a little bit more
complicated.

I want to be able to set the ComboBox's SelectedItem property by
setting IntegerValue, so for instance if I have the following items in
my ComboBox:

IntegerValue StringValue
3 Apple
4 Banana
1 Carrot
5 Eggplant
2 Fig

By doing something like ComboBox.SelectedItem.IntegerValue = 1 the
ComboBox would then display Carrot.

I imagine I would need to create a custom ComboBox control and add a
function which is kicked off when the IntegerValue is Set, or perhaps
just override an existing function.

Has anyone done this or something similar before or have any
suggestions?

Regards,
Mong

Feb 20 '06 #1
8 3502
Mong,

If you use a datatable as datasource this is in my idea dead simple.

If you try it using this sample on our website

http://www.vb-tips.com/default.aspx?...c-f0b560f43e7c

And than add in this sample,

'ComboBox1.DataSource = dt
'ComboBox1.DisplayMember = "FullName"
ComboBox1.DisplayValue = "LName"
Combobox1.SelectedText = "Cor Ligthert"
(In your case a number of course)

I hope this helps.

Cor
<mo*********@yahoo.com.au> schreef in bericht
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi,

I'm populating a ComboBox with objects which contain two properties -
IntegerValue and StringValue. I display StringValue in the ComboBox
form and can retrieve IntegerValue from the SelectItem.IntegerValue
property. This is all good but now I want to get a little bit more
complicated.

I want to be able to set the ComboBox's SelectedItem property by
setting IntegerValue, so for instance if I have the following items in
my ComboBox:

IntegerValue StringValue
3 Apple
4 Banana
1 Carrot
5 Eggplant
2 Fig

By doing something like ComboBox.SelectedItem.IntegerValue = 1 the
ComboBox would then display Carrot.

I imagine I would need to create a custom ComboBox control and add a
function which is kicked off when the IntegerValue is Set, or perhaps
just override an existing function.

Has anyone done this or something similar before or have any
suggestions?

Regards,
Mong

Feb 20 '06 #2
Thanks Cor,

I was able to get this working using your suggestion and building an
arraylist which I then bind the combo to. Only problem is because the
control is bound (I assume) it defaults to selecting index 0 rather
than -1, even if the last thing I do is set SelectedItem = -1 it will
still display the first item in the list. Is there any way around this?

Feb 21 '06 #3
Mongphong,

Bephore we try other things there is a bug in version 1.x that you have to
set the selecteditem twice to -1. I have the idea it is in the way you
describe it.

Cor
Feb 21 '06 #4
Cor,

If I use the follwoing:

cbo.SelectedIndex = -1
cbo.SelectedIndex = -1

the result is still the same, the first item is displayed. However if I
place a button the form with the following in it's click event (and
click the button):

cbo.SelectedIndex = -1

the combobox does correctly clear with no item displayed.

Why do you think it matters where the code is placed?

The only difference between the two is that the form has not been shown
when the first attempt to set SelectedIndex to -1 is run. Is this the
problem? Is it a bug? Is there a fix?

Thanks again,
Mong

Feb 22 '06 #5
Mong,

Did you place it as the last instructions in the Load Event?

Cor
Feb 22 '06 #6
No it's in the initialisation of the form, that's what I meant in my
last paragraph when I said the form hadn't been shown yet hence the
Load event had not been run. So it's a bug then, is there a fix?

Obviously I can add cbo.SelectedIndex = -1 to the Load event of the
form, but it shouldn't matter if the control is visible or not.

Feb 23 '06 #7
Monghponhg,

No it is not a bug, by settting the datasource will the index as well be
set.

Therefore if you with this, than have you to do this after that setting of
the datasource, by instance in the load event of the form.

I hope this helps,

Cor
Feb 23 '06 #8
Cor,

As previously stated the datasource has been set, the combo list has
been populated, and the index has been set to -1. However as I also
previously stated if this is done before the form has been visually
shown ie form.show() has been called and the form load event fired,
setting the index to -1 does not work. It will only work if the index
is set to -1 after form load has run.

As the object has been created during form initialisation (meaning an
object of the class has been instantiated), whether it is visible or
not should be irrelavent as the object exists. However this situation
shows that although other functionality of the ComboBox class works as
expected ie. setting the datasource, building the list of items etc,
setting the index does not. This my friend has the hallmarks of a bug.

I'm more than happy to hear from you any evidence to disprove that this
is a bug. I only ask could you please get an english speaking colleague
to proof read your response as I have difficulty deciphering your posts
and am beginning to think you're from Dagobah.

Regards,
Mong

Cor Ligthert [MVP] wrote:
Monghponhg,

No it is not a bug, by settting the datasource will the index as well be
set.

Therefore if you with this, than have you to do this after that setting of
the datasource, by instance in the load event of the form.

I hope this helps,

Cor


Mar 6 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
0
by: metamedia | last post by:
How do I get a datagrid to register a data change from a custom control cell (combobox) I've got a Windows Application with a Windows Form Datagrid that has a custom combobox column. When the user...
0
by: bell | last post by:
I was playing around with making a custom control very similar to the standard ComboBox: a TextBox combined with a ListBox. After trying it out, I ran into the following problem: When drawing my...
3
by: Flack | last post by:
Hello, I am beginning to write a custom combobox control. Right now it doesn't do anything special. I created a class that extends ComboBox: public class TimeSelector :...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
1
by: philipl | last post by:
hi, i have a datagrid which display custom controls when user clicks edit, changing data. I am using customvalidator to validate this control. I can get validation of user input to work no...
4
by: guy | last post by:
i am trying to create a custom Combobox from scratch. this requires that i create the list that the combobox opens. i've used the ListBox control to do this. my problem is that i don't know how...
2
by: Try Guy | last post by:
I'm not sure if I really understand this whole ownderdraw thing... i want to create my own Windows Forms control (NOT USERCONTROL)...is it ownerdrawing I should start learning or something else?...
6
by: kberry | last post by:
I am clearing Textboxes on a form... this is loop I have came up with but was wondering if it can be shorter or not as long... Can anyone help? Dim controlOnForm As Control 'Places a control...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.