Advertisement

02.14.2007 at 05:12AM PST, ID: 22388214
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Add new item to combobox, dynamically

Tags: combobox, add, item, dynamically
Im using VS.NET 2005... Windows App
I need to programatically add new items in the combobox ... its like...
i'll pass the item text and its value to a funtion and the ref of the combobox..
this function should add the new item (with its value) to the combobox...

and later in some other function i need to be able to get the value of the selected item say like
combobox1.selectedvalue.toString();

i search over the net... and the methods suggested will only add the text to the combo item but it will not add the respective value to it... i need to be able to  retrieve the combox's selectd item's value....

any help will be appreciated...
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: deepak_tyco
Solution Provided By: Dabas
Participating Experts: 3
Solution Grade: A
Views: 500
Translate:
Loading Advertisement...
02.14.2007 at 06:05AM PST, ID: 18531058

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.14.2007 at 09:39AM PST, ID: 18533357

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.14.2007 at 09:44AM PST, ID: 18533407

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.14.2007 at 01:42PM PST, ID: 18535825

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.14.2007 at 01:49PM PST, ID: 18535890

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.14.2007 at 10:08PM PST, ID: 18538227

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.14.2007 at 11:52PM PST, ID: 18538489

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.14.2007 at 11:53PM PST, ID: 18538493

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2007 at 01:10AM PST, ID: 18538764

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.14.2007 at 06:05AM PST, ID: 18531058
Hi there,
I've found that the best way to add values dynamically  to a dropdown is by adding them to a datatable first, then binding the combo box to the datatable. So something like this:

'Build the table first
Private t as table
wth t
.Colums.Add("value_column",GetType(Integer))
.Colums.Add("text_column",GetType(String))
End With

'Add the rows
with t
Dim r as DataRow
r =.NewRow()
r.Item(0) = your new value
r.Item(1) = your new text
.Rows.Add(r)
End With

'Bind the combo box to the new table
your_combobox.DataSource = t
your_combobox.DisplayMember = "value_column"
your_combobox.ValueMember = "text_column"

Chad





 
02.14.2007 at 09:39AM PST, ID: 18533357
Ok, here's the way I'd handle it:

Create a System.Collections.Hashtable that will store values related to your list items:

System.Collections.Hashtable ItemValues = new System.Colllections.Hashtable();

...then add the new control to the ComboBox and the Value to the Hashtable:

ComboBox.Items.Add("NameOfItem");
ItemValues.Add("NameOfItem", "ValueOfItem");

to figure out the value of your selected item do this:

string foundValue = ItemValues[ComboBox.SelectedValue.ToString()];

foundValue will then be the string representation of the value you're looking for.

Hope that helps,

Joshua James
DotNet.Thinker@gamil.com
 
02.14.2007 at 09:44AM PST, ID: 18533407
Correction:

I meant

string foundValue = ItemValues[ComboBox.SelectedItem].ToString();

rather than

string foundValue = ItemValues[ComboBox.SelectedValue.ToString()];
 
02.14.2007 at 01:42PM PST, ID: 18535825
I prefer an unbound solution

Try this:
Create a project with two files. Class1.vb and Form1.vb (I am adapting this from a vb project of mine, but I assume the translation to C# will be easy for you, otherwise ask)

Public Class Class1
    Private mText As String
    Dim mIndex As Integer
    Dim m_AnyOtherObject As Object

    Public Sub New(ByVal NewText As String, ByVal NewIndex As Integer)
        Text = NewText
        Index = NewIndex
    End Sub

    Public Property Text() As String
        Get
            Return mText
        End Get
        Set(ByVal value As String)
            mText = value
        End Set
    End Property

    Public Property Index() As Integer
        Get
            Return mIndex
        End Get
        Set(ByVal value As Integer)
            mIndex = value
        End Set
    End Property

    Public Property AnyOtherObject() As Object
        Get
            Return m_AnyOtherObject
        End Get
        Set(ByVal value As Object)
            m_AnyOtherObject = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return Me.Text
    End Function

End Class

Public Class Form1

    Private Sub btnFillCombo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillCombo.Click
        Dim c As Class1
        For I As Integer = 1 To 3
            c = New Class1("Row " & I.ToString, 999 + I)
            cboTest.Items.Add(c)
        Next
        'Application.DoEvents()
        cboTest.SelectedIndex = 0
    End Sub

    Private Sub cboTest_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTest.SelectedIndexChanged
        'Dim c As Class1 = CType(cboTest.SelectedItem, Class1)
        Dim c As Class1 = cboTest.Items(cboTest.SelectedIndex)
        txtIndex.Text = c.Index.ToString

    End Sub

End Class
Accepted Solution
 
02.14.2007 at 01:49PM PST, ID: 18535890
The way it works is that the items in a combobox do not need to be strings. They can be objects
In the example above, the object I am adding to cboTest is of type Class1 (You can create your own object in a similar way)
The string that is displayed is the one that the class overrides in its ToString() function

The advantage of this way of doing things, is that you are not limited to just a text and a value, but you can have a text to display and then as many other values as you want.

Dabas
 
02.14.2007 at 10:08PM PST, ID: 18538227
@ ChadMarsh and kpcurley: both your solutions are almost similar... i either have to use a datatable or a hashtable.

@ Dabas: It dint solve the problem. I dont want the text part of the combobox that is displayed. I am specifically interested in the value part of it. I need to use my_combobox.SelectedValue.ToString(); And this does not happen via your solution. For Eg. If i pass say Employee name and Employee Id to a function, and this funtion need to add Employee Name as text part and Employee Id as value part.

And later in another fnction... when i do my_combobox.SelectedValue.ToString();  i shud get the Employee Id and not the name...

I'll keep this Q open for another day. If we cant figure out a better soln... i'll accept the first two

thanks a lot ...
 
02.14.2007 at 11:52PM PST, ID: 18538489
deepak_tyco,
It works if you do it like this:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim c As Class1 = CType(cboTest.Items(cboTest.SelectedIndex), Class1)
        MessageBox.Show(String.Format("Text:  {0}, Values: {1}", c.Text, c.Index))
    End Sub

In my class Index is the value. You can change the class to have EmployeeId and EmployeeName instead. It still will work.

Dabas
 
02.14.2007 at 11:53PM PST, ID: 18538493
deepak_tyco,
It works if you fill the combo the way I did in the btnFill_Click.
Here is an example on how I extract both the text AND the ID.
Please try it, because it works for me perfectly

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim c As Class1 = CType(cboTest.Items(cboTest.SelectedIndex), Class1)
        MessageBox.Show(String.Format("Text:  {0}, Values: {1}", c.Text, c.Index))
    End Sub

In my class Index is the value. You can change the class to have EmployeeId and EmployeeName instead. It still will work.

Dabas

 
02.15.2007 at 01:10AM PST, ID: 18538764
@ Dabas: Hey sorry! I had a typo... So i dint get the Value properly. Your first soln itself worked. thnx.
 
 
20080236-EE-VQP-29