Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to fill a comboBox with both a value and text.

 

I know that to add an item to the combobox I can use something like this:

Me.cmbRapporten.Items.Add(lsReportTitleShort)

 

(lsReportTitleShort is a String)

 

 

I would however like to add a value/ID to the Item as well so I can reference it more easily further on, but have so far been unable to find how to do this.

 

 

I've used this syntax in VB6 and am basically looking for something similar (if it exists) in VB.Net

 

 

cmb.AddItem itemName

cmb.ItemData(cmb.NewIndex) = itemID

 

 

 

Any tips and/or help is appreciated.

Posted

I don't see anything that allows you to add a key but you can get the index of the items with ComboBox1.SelectedItem and of course you can reference items by their index with ComboBox1.Item(index) as I'm sure you know.

 

Since the ComboBox does accept objects you can probably get a little creative and create your own custom objects with an ID and throw them in there. Then you can loop through the ComboBox's collection and find the correct ID (you'll need to use overrides method ToString to display the text of the object inside the ComboBox).

 

Maybe I missed something, who knows.. but the best way to find out about something in .NET is to look in the object browser. The information for the ComboBox can mostly be found at System.Windows.Forms.System.Windows.Forms.ComboBox.ObjectCollection and within the same namespace also ComboBox.

Gamer extraordinaire. Programmer wannabe.
Posted

Actually I'm not that well versed in .NET (yet).

3 months ago I knew didly about both VB6 and VB.NET but I'm slowly getting some understanding of both (VB6 a lot more so far).

 

I actually thought the help for VB6 objects was lousy, but there I always eventually find what I'm looking for .... with .NET I've not had that much success.

Thanks for the hint on where to find it ... maybe I can make more sense out of that than out of the stuff I have found about the combobox object so far.

 

 

Through trial and error I did indeed find the ComboBox1.Item(index) option, but it's not something I can use in this case.

 

I'm rewriting some stuff that another (much more experienced) programmer wrote to make it more functional in the final application.

The combobox is supposed to allow the user to select which Crystal Report he wants to see in the CRViewer on the same form.

As things are now the user can select from text like 'crpByNumber.rpt' ... now I as one of the developers know which report that's supposed to be but a simple user won't ....... so I'm changing things so that the combo will have text like "List A - Sorted by number" instead.

 

 

 

I understand the gist of what your telling me I can use as a go-around, but have not clue as how to exactly program it.

 

But for now I've found another way to get both the look and functionality I want by using a "select case" to link the report filename to the report title in the combobox and then with the filename open the Crystal Report.

 

 

The only problem I have now is if I change the text to be used in the combobox then I also have to change my select case again ...... so tomorrow I'm going to start fiddling to see if I can connect this whole combobox business with a database table and have the text filled and the reportname selected from there. Then it won't matter what titles I use or how many reports I add to the application, all I have to do is add report and file names to the database and Bob's your uncle ( erm ..... I hope). :)

Posted

VB6 is definitely easier to learn if you're unfamiliar with object oriented programming (OOP). Although once you learn OOP .NET suddenly makes sense and becomes a much more powerful tool at your disposal.

 

What I suggested is actually fairly simple to implement but unfortunatley I have to leave for class. If no one else writes you up a quick little example on how to add custom objects with their own ID (keys) then I will when I get home.

 

Also I have a reference manual for .NET and looked up the ComboBox. It doesn't seem to have any type of .Key property for the Items and the book actually gives an example on how to add objects with their own IDs (exactly what I suggested ironically enough). So unless there's some hidden way to add a Key directly to a ComboBox item, creating your own custom object is the only way to do it in .NET.

 

Just remember that in .NET the object browser is your best friend. :)

Gamer extraordinaire. Programmer wannabe.
Posted

:)

 

I know it's supposed to be my best friend, but so far it's not really been that. Guess I'll just have to get used to how the .NET help works.

(And it might help if I find the time to read at least the basic chapters in the .NET tutorial I bought the other day. *G*)

 

 

 

 

Thanks for the example.

I don't have .NET running at work but have downloaded the file and will forward it to work so I can look at it first thing in the morning.

 

If I have any questions about it I'll let you know.

 

 

Thanks again ... heaps :)

Posted

REEAAAALLLLLYYYYYY great example!

 

Thanks a bundle, this is exactly the kind of thing I was looking for.

 

I've been slaving away all day integrating it into our application and this simple little tidbit is actually allowing me to chuck away 2 or 3 functions/subs that were being used to pass around variables and parameters all over the place.

 

Instead of just a text and an ID I'm actually using the combo to pass around 4 different variables and because of that things are now working a lot more efficiently.

 

All I need to do now is fill the combo from a database table (have hardcoded the data so far) and we'll be able to add modules to our application in the future without needing to adjust the coding.

 

 

The guy who did the original programming is gonna be bummed that I managed to scrap so much of his work with a "simple" combobox. :)

  • 1 month later...
  • 4 months later...
Posted

Well, your example is pretty good, but will be simplier if you do what is in my example... less code and you can actually get the data you're looking for without having to do loops nor counting the item

Sorry for this... :(

Fat kids are harder to kidnap
  • 3 weeks later...
Posted

Have you tried populating your combobox from a dataset

using the following properties...

 

cbo1.DisplayMember = "UserTypeDesc"

cbo1.ValueMember = "UserTypeId"

 

and then set:

 

cbo1.SelectedValue = User.UserTypeId

 

The above reference is a 'User' Object with the Property 'UserTypeId'

 

Seems to get the same results without creating additional classes and uses the inherent object properties

  • 4 weeks later...
Posted

I'm using the same sort of thing, although I found it before you :p

just joking

 

anyway, I just can't get at load an item selected...

how would you do that then? I just the item with member ID = 1 be selected when the form loads

 

greetz

Tomba

 

ps: funny waking up old threads over and over again :p

  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...