Solved

Making HashTable DataSource for Combobox in Windows Forms

Posted on 2006-06-16
7
1,792 Views
Last Modified: 2008-01-09
Hi, I have the following information:

Dim monthTable As New Hashtable
        monthTable.Add(1, "January")
        monthTable.Add(2, "February")
        monthTable.Add(3, "March")
        monthTable.Add(4, "April")
        monthTable.Add(5, "May")
        monthTable.Add(6, "June")
        monthTable.Add(7, "July")
        monthTable.Add(8, "August")
        monthTable.Add(9, "September")
        monthTable.Add(10, "October")
        monthTable.Add(11, "November")
        monthTable.Add(12, "December")

       The following does not work.
        ComboBox1.DataSource = monthTable

How do I make the HashTable the datasource of my combobox so I can access the key or values. I was reading that HashTable is not part of List or Ilist and hence, it cannot be a datasource. What is the workaround? Has anybody tried something like this. I'm sure it's simply for you guys.

Sanna

0
Comment
Question by:Sanmarie
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Assisted Solution

by:fffej78
fffej78 earned 40 total points
ID: 16922787
Not the answer you wanted, but why don't you just use a string array.  Not sure on the syntax for VB.NET, but in C# I'd just do

String[] months = new String[]{"January","Feb","Mar" }; //and so on!

ComboBox1.DataSource = months;

There is little point using a map here when the keys are sequential integers.  The mapping from 1-12 to 0-11 shouldn't pose any issues :)
0
 

Author Comment

by:Sanmarie
ID: 16922867

Thanks but I was hoping to avoid this.
0
 
LVL 4

Expert Comment

by:fffej78
ID: 16922883
Why?  What is the rationale for using a map?  What is it buying you?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Sanmarie
ID: 16923018

I'm not saying your answer is wrong. There are many ways of doing something. In asp.net, you can set a hashtable as the datasource, I'm just wondering if there is something similar in windows forms. That's all.
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 16926683
Hi,

what exactly are you trying to achieve?, i'm just asking so i can try and think of a workaround.

Cheers

BCS
0
 

Author Comment

by:Sanmarie
ID: 16926811
I'm building a so called 'crosstab' datagrid user control and depending on the month that is selected from a combobox, I want to do some stuff. So, I will be displaying the month name in the combobox and behind the scenes I will be using the index of the month. Example, January is index 0. However, there are if cases where if the month is at a particular index then I want to select another month at another index. These things I won't know until runtime. Here, I will have to use for loops to determine what I want. I was trying to avoid the for loops but it seems like it's a crime to do so.

Since I did something similar in asp.net using a hashtable, I just have it in the back of my mind that maybe there is an alternative in windows forms than using arrays. I guess there isn't an alternative.

The previous answer suggested one way. He did not state that it was the only way. If this is the only way, then I will accept it as an answer.
0
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 85 total points
ID: 16927567
Hi there,

do you actually use the hashtable anywhere else? it just that it's a bit expensive given that it's so sparsely populated. Unless you absolutely need to use the hashtable i'd suggest using one of the collections that implements IList, such as an Array or ArrayList or simply add the items to your combo. Alternatively you could create a custom hashtable with an IList although i'd steer clear of this.

BCS
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question