Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The Combos and Listbox Issues 1

Status
Not open for further replies.
It seems we have lost a lot of functionality and ease with Combos and Listboxes.

Looking at the List of Properties, Methods and Events we used to have in VB6 and is no longer supported in VB.Net, I was rather sad. These PMEs have not been replaced with something equivalent to make it easy.

What are the BASIC uses for Listboxes/Combos ANY developer should be able to do?
1) Populate the control with Data from some lookup table in a DB
2) Set the initial value held in the control to -1 so it is blank to begin with
3) Let the users select a value/listed choice in the List
4) Store that selected value in a DB Table.
5) Retrieve the stored value again at a later point

THIS IS what 3rd Party Control vendors andeven Microsoft keep in mind when creating controls. If one cannot do any of the above/all of the above the control is a waste of time and should not be used.

I wish they would LISTEN to us!

In the Controls collection of VB6 we used to have the ItemData property which we used to store the ID Value of the LookupTable in. Now we do not have it anymore.

So they try and tell you that yes, their control still works and is even better, just bind it to the Dataset! Set the DisplayMember and the Valuemember, then retrieve it again with the SelectedValue Property or the SelectedIndex Property. But then you cannot set the SelectedIndex to -1, which gives your users a nice blank combo to start with!

You cannot set the myCombo.Text = "" property either! So back to the beginning and in the end we all, like suckers settle for a measly second best.

sorry Microsoft, give us back the properties we had in the Combos! Your controls are not worth much without it!

Anyone else struggling with Itemdatas and some missing PMEs?

Let us know!

Maybe someone an help us with some REAL solutions!

Rgds
Len




Quiet as a bulldozer Mate!
 
There are two ways to fill the ComboBox and ListBox controls.

For example, you can add objects to the ComboBox by using the Add method. You can also add objects to a ComboBox by the using the DataSource, DisplayMember, and ValueMember properties to fill the ComboBox.
The DataSource property of ComboBox accepts an object that implements the IList interface, such as a DataSet or an Array.

Although, when the DataSource property is set, a user cannot modify the items collection, you can actually add a blank row of data in DataSet which is bound to ComboBox. That’s the trick, the new blank row will be shown in the ComboBox.

Here’s the code:

'Populate ComboBox.
ComboBox1.DataSource = myDS.Tables(0)
ComboBox1.ValueMember = "ColumnName"
ComboBox1.DisplayMember = "ColumnName"

'Add a blank row to the ComboBox.
DR = myDS.Tables(0).NewRow
DR(1) = ""
myDS.Tables(0).Rows.Add(DR)

'Initially show blank row in ComboBox.
ComboBox1.SelectedIndex = ComboBox1.FindStringExact("")


 
I couldn't understand what do you mean by "Store that selected value in a DB Table."

Well, if your ComboBox is already populated with values from DB Table, why would you store them again"





 
The Combobox is a list from a Lookup Table in the Database:
i.e. ContactType with items like HomePhone, WorkPhone,Mobile.

You store these values in another table, like Clients. Where you have a ClientTelNo and a ID as a foreign key to the Lookup Table, like ContactTypeID = 1, for HomePhone, or 2 for WorkPhone.

So you look it up from the Lookup Table, but store it as part of a record in a Clients Table. Do you understand now?

Len

Quiet as a bulldozer Mate!
 
Len,

Are you sure that was an option with the VB6 ComboBox? I know it was with the DataCombo, which was more or less a specialized control.

If you really wanted to, you could extend ComboBox and make your own class to gain the functionality.
 
Hi Riverguy
Yip - I have been programming with the VB6 Combos / Listboxes for over 5 yrs. I never really got into Datacombos.

I also attempted to use a class to add such functionality but there was one little snag that came back to bite me.

The Post by Pankaj Banga helped a lot.

I have one issue with a Combo where I need to have 2 fields concatenated to become one item in a Combo. Here I cannot set DisplayMember and Valuemember Properties. This one is going to come and bite me too.

B-)

Len

Quiet as a bulldozer Mate!
 
To get two fields for display...all you need to do is have a source in your DataSource to display such a thing. If you are binding to a ArrayList of custom objects, then add a property to display both. If binding to a DataTable, then add a DataColumn and set it's values.

Otherwise, if you are trying to mimic the Microsoft Access ComboBox approach, you can derive your own class, make it Owner-Drawn, and show two items. I did this once, but I only got it to work with a certain style. I don't remember if it was DropDownList or what. But I had it working so that it you would get a vertical line between the two fields, and could set the width of the fields as well.
 
Now that I got the Combos more or less done, I need to deal with the darn Listboxes!

I can use the code as follows:
'Populate ComboBox.
ComboBox1.DataSource = myDS.Tables(0)
ComboBox1.ValueMember = "ColumnName"
ComboBox1.DisplayMember = "ColumnName"
=======

to populate combo boxes with datasets, but for some mysterious reason beyond my understanding, it will not work for Listboxes.
I do not need worry about setting default selections, but at least try get it to bind to a datasource...

The errors I get is
1) Cannot modify the Items collection when the Datasource property is set.
2)Cast from DatarowView to Integer Invalid ..

I desperately need a magic wand...Harry Potter come and help!!!

Len

Quiet as a bulldozer Mate!
 

My combobox does not populate with data. I know i am missing something,but am not sure what it is.

Any help is appreciated.....



Me.ComboBox1.AllowDrop = True
Me.ComboBox1.Cursor = System.Windows.Forms.Cursors.SizeAll
Me.ComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.Contacts1, "Contacts.FirstName"))
Me.ComboBox1.DataSource = Me.Contacts1.Contacts
Me.ComboBox1.DisplayMember = "LastName"
Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox1.Location = New System.Drawing.Point(168, 88)
Me.ComboBox1.MaxLength = 5
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(136, 21)
Me.ComboBox1.TabIndex = 0
Me.ComboBox1.ValueMember = "LastName"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top