C# - how to populate a combobox


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: C# - how to populate a combobox

  1. #1
    Join Date
    Aug 2007
    Location
    NYC
    Posts
    60

    C# - how to populate a combobox

    Please could someone give me the syntax/code to populate a combobox.

    The combo may get values either from a table or
    hard-coded values as you do in a listbox.

    Thanks.
    Giftx.

  2. #2
    Join Date
    Aug 2007
    Location
    NYC
    Posts
    60
    can someone please give me a hand on how to populate comboboxes. Below is a sample of how I started, but I know it's grossly in correct.

    I'm just trying to get the hang of C# for the first time.

    Code:
    sing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace PrepCCardService
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                //Initialize "From" Country combo:
                //----------------------------
                String[] FromList = new String[2];
                FromList[0] = "United States";
                FromList[1] = "United Kingdom";
    
                //Initialize "To" country combo:
                //--------------------------
                String[] ToList = new String[3];
                ToList[0] = "India";
                ToList[1] = "Africa";
                ToList[2] = "Russia";
    
                String[] MinsList = new String[3];
                MinsList[0] = 30;
                MinsList[1] = 60;
                MinsList[2] = 120;
      
            }
    
            private void cmdbtnexitsite_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void cmdbtnnxtform_Click(object sender, EventArgs e)
            {
                //populate From country combo:
                //------------------------------
                int i;
                for (i = 0; i < 2; i++)
                {
                cbocallfrom.Items.AddRange(FromList);
                }
    
                //populate destination country combo:
                //-----------------------------------
                int ii;
                for (ii = 0; ii < 3; ++ii)
                {
                cbocallto.Items.AddRange(ToList);
                }
    
                //cbocallto.Items.Add("United Kingdom");
                //cbocallto.Items.Add("India");
                //cbocallto.Items.Add("Africa");
    
    
                //populate minutes amount combo:
                //------------------------------
    
                Form2 secondfrm = new Form2(); //Create instance of form2  
                secondfrm.Show();              //Go to form2:
            }
        }
    Thanks.
    Giftx.

  3. #3
    Join Date
    Jun 2006
    Posts
    45
    try this:

    for (i = 0; i < 2; i++)
    {
    cbocallfrom.Items.Add(FromList[i]);
    }

    it also seems that your arrays might be out of reach try declaring them outside the method
    Last edited by kodekracker; 09-28-2008 at 10:23 PM.

  4. #4
    Join Date
    May 2013
    Posts
    1
    you can bind a combobox like this

    comboBox1.DataSource = ds.Tables[0];
    comboBox1.ValueMember = "au_id";
    comboBox1.DisplayMember = "au_lname";

    http://csharp.net-informations.com/d...t-combobox.htm

    curos

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Or as hardcoded you can create you own class
    Code:
    Public Class MyStuff
    {
         Public String Name{Get;Set;}
         Public String Value{Get;Set;}
         Public Override String ToString() { return this.Name;}
    }
    Now you can add as many MyStuffs to the combobox as you want.
    Code:
    yourCombobox.Items.Add(new MyStuff{Name="Banana",Value="BA"});
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Replies: 6
    Last Post: 09-06-2013, 04:44 AM
  2. populate chart category from a combobox
    By RipIT in forum VB Classic
    Replies: 27
    Last Post: 02-21-2008, 09:01 AM
  3. VS2005 combobox and databinding issues
    By subrama6 in forum .NET
    Replies: 4
    Last Post: 02-13-2008, 10:19 AM
  4. Replies: 1
    Last Post: 08-28-2006, 07:51 PM
  5. comboBox question
    By Stan Shankman in forum .NET
    Replies: 2
    Last Post: 09-07-2001, 04:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center