ComboBox Items

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wayne  Wengert

    #1

    ComboBox Items

    I am trying to populate a combobox with a lisy of items. I want the 1st item
    to be "Make a Selection" and the following items will be names from a table
    in my DB (See code below).

    When I run the project, the combox box initially displays an empty
    selection. When I click on the drop down arrow I see the "Make a Selection"
    followed by the other choices.

    How can I move everything up so that the "Make a Selection" is the default
    value in the combo box?

    Wayne

    ============= code =============== =
    Dim cn As New OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
    Source=c:\Progr am Files\MyApp\MyD ata.mdb")

    strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"

    cn.Open() 'Open the connection

    Dim cmd As New OleDbCommand(st rSQL, cn)

    Dim rdr As OleDbDataReader = cmd.ExecuteRead er

    cboSample.Items .Clear()

    cboSample.Items .Add("Make a Selection")

    While (rdr.Read())

    cboSample.Items .Add(rdr.GetStr ing(0)) ' Name is the only field returned

    End While

    rdr.Close()


    --
    ------------------------------------
    Wayne Wengert
    wayne@wengert.o rg


  • Jeffrey Tan[MSFT]

    #2
    RE: ComboBox Items


    Hi Wayne,

    The item the combobox initially displays should be set by the combobox's
    text property.

    Hope this helps.

    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.

    --------------------
    | From: "Wayne Wengert" <wayne@wengert. org>
    | Subject: ComboBox Items
    | Date: Tue, 5 Aug 2003 15:49:31 -0600
    | Lines: 44
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    | Message-ID: <uRH63t5WDHA.17 48@TK2MSFTNGP12 .phx.gbl>
    | Newsgroups: microsoft.publi c.dotnet.genera l
    | NNTP-Posting-Host: skybeam143.skyb eam.frii.net 216.17.229.143
    | Path:
    cpmsftngxa06.ph x.gbl!cpmsftngx a10.phx.gbl!TK2 MSFTNGXA05.phx. gbl!TK2MSFTNGP0 8
    phx.gbl!TK2MSFT NGP12.phx.gbl
    | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:103592
    | X-Tomcat-NG: microsoft.publi c.dotnet.genera l
    |
    | I am trying to populate a combobox with a lisy of items. I want the 1st
    item
    | to be "Make a Selection" and the following items will be names from a
    table
    | in my DB (See code below).
    |
    | When I run the project, the combox box initially displays an empty
    | selection. When I click on the drop down arrow I see the "Make a
    Selection"
    | followed by the other choices.
    |
    | How can I move everything up so that the "Make a Selection" is the default
    | value in the combo box?
    |
    | Wayne
    |
    | ============= code =============== =
    | Dim cn As New OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
    | Source=c:\Progr am Files\MyApp\MyD ata.mdb")
    |
    | strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"
    |
    | cn.Open() 'Open the connection
    |
    | Dim cmd As New OleDbCommand(st rSQL, cn)
    |
    | Dim rdr As OleDbDataReader = cmd.ExecuteRead er
    |
    | cboSample.Items .Clear()
    |
    | cboSample.Items .Add("Make a Selection")
    |
    | While (rdr.Read())
    |
    | cboSample.Items .Add(rdr.GetStr ing(0)) ' Name is the only field returned
    |
    | End While
    |
    | rdr.Close()
    |
    |
    | --
    | ------------------------------------
    | Wayne Wengert
    | wayne@wengert.o rg
    |
    |
    |

    Comment

    • Wayne  Wengert

      #3
      Re: ComboBox Items

      I gather this does not apply when you bind the combobox to a dataset? The
      1st item in the dataset shows in the text?

      Wayne

      "Jeffrey Tan[MSFT]" <v-jetan@online.mi crosoft.com> wrote in message
      news:8VeVIs%23W DHA.1536@cpmsft ngxa06.phx.gbl. ..[color=blue]
      >
      > Hi Wayne,
      >
      > The item the combobox initially displays should be set by the combobox's
      > text property.
      >
      > Hope this helps.
      >
      > Jeffrey Tan
      > Microsoft Online Partner Support
      > Get Secure! - www.microsoft.com/security
      > This posting is provided "as is" with no warranties and confers no rights.
      >
      > --------------------
      > | From: "Wayne Wengert" <wayne@wengert. org>
      > | Subject: ComboBox Items
      > | Date: Tue, 5 Aug 2003 15:49:31 -0600
      > | Lines: 44
      > | X-Priority: 3
      > | X-MSMail-Priority: Normal
      > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
      > | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
      > | Message-ID: <uRH63t5WDHA.17 48@TK2MSFTNGP12 .phx.gbl>
      > | Newsgroups: microsoft.publi c.dotnet.genera l
      > | NNTP-Posting-Host: skybeam143.skyb eam.frii.net 216.17.229.143
      > | Path:
      >[/color]
      cpmsftngxa06.ph x.gbl!cpmsftngx a10.phx.gbl!TK2 MSFTNGXA05.phx. gbl!TK2MSFTNGP0 8[color=blue]
      > phx.gbl!TK2MSFT NGP12.phx.gbl
      > | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:103592
      > | X-Tomcat-NG: microsoft.publi c.dotnet.genera l
      > |
      > | I am trying to populate a combobox with a lisy of items. I want the 1st
      > item
      > | to be "Make a Selection" and the following items will be names from a
      > table
      > | in my DB (See code below).
      > |
      > | When I run the project, the combox box initially displays an empty
      > | selection. When I click on the drop down arrow I see the "Make a
      > Selection"
      > | followed by the other choices.
      > |
      > | How can I move everything up so that the "Make a Selection" is the[/color]
      default[color=blue]
      > | value in the combo box?
      > |
      > | Wayne
      > |
      > | ============= code =============== =
      > | Dim cn As New OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
      > | Source=c:\Progr am Files\MyApp\MyD ata.mdb")
      > |
      > | strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"
      > |
      > | cn.Open() 'Open the connection
      > |
      > | Dim cmd As New OleDbCommand(st rSQL, cn)
      > |
      > | Dim rdr As OleDbDataReader = cmd.ExecuteRead er
      > |
      > | cboSample.Items .Clear()
      > |
      > | cboSample.Items .Add("Make a Selection")
      > |
      > | While (rdr.Read())
      > |
      > | cboSample.Items .Add(rdr.GetStr ing(0)) ' Name is the only field returned
      > |
      > | End While
      > |
      > | rdr.Close()
      > |
      > |
      > | --
      > | ------------------------------------
      > | Wayne Wengert
      > | wayne@wengert.o rg
      > |
      > |
      > |
      >[/color]


      Comment

      • Jeffrey Tan[MSFT]

        #4
        Re: ComboBox Items


        Hi Wayne,

        I do not quite understand your meanning, but I think you can do like this:

        using System;
        using System.Drawing;
        using System.Collecti ons;
        using System.Componen tModel;
        using System.Windows. Forms;
        using System.Data;
        using System.Data .SqlClient;

        namespace testcombobox
        {
        public class Form1 : System.Windows. Forms.Form
        {
        private System.Windows. Forms.ComboBox comboBox1;
        private System.Componen tModel.Containe r components = null;

        public Form1()
        {
        InitializeCompo nent();
        }


        protected override void Dispose( bool disposing )
        {
        if( disposing )
        {
        if (components != null)
        {
        components.Disp ose();
        }
        }
        base.Dispose( disposing );
        }

        #region Windows Form Designer generated code

        private void InitializeCompo nent()
        {
        this.comboBox1 = new System.Windows. Forms.ComboBox( );
        this.SuspendLay out();
        //
        // comboBox1
        //
        this.comboBox1. Location = new System.Drawing. Point(64, 152);
        this.comboBox1. Name = "comboBox1" ;
        this.comboBox1. Size = new System.Drawing. Size(152, 21);
        this.comboBox1. TabIndex = 0;
        this.comboBox1. Text = "comboBox1" ;
        //
        // Form1
        //
        this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
        this.ClientSize = new System.Drawing. Size(292, 266);
        this.Controls.A dd(this.comboBo x1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHan dler(this.Form1 _Load);
        this.ResumeLayo ut(false);

        }
        #endregion
        static void Main()
        {
        Application.Run (new Form1());
        }

        private void Form1_Load(obje ct sender, System.EventArg s e)
        {
        SqlDataAdapter adapter=new SqlDataAdapter ("select * from titles where
        price!=0","serv er=localhost;da tabase=pubs;uid =sa;pwd=");

        DataSet ds=new DataSet ();
        adapter.Fill (ds);
        comboBox1.Text ="Please select a item";
        comboBox1.Items .Clear ();
        foreach(DataRow row in ds.Tables [0].Rows)
        {
        comboBox1.Items .Add (row.ItemArray [0]);
        }
        }
        }
        }

        I use sqlserver to add data to comboBox and it works well on my machine.

        Hope it helps

        Jeffrey Tan
        Microsoft Online Partner Support
        Get Secure! - www.microsoft.com/security
        This posting is provided "as is" with no warranties and confers no rights.

        --------------------
        | From: "Wayne Wengert" <wayne@wengert. org>
        | References: <uRH63t5WDHA.17 48@TK2MSFTNGP12 .phx.gbl>
        <8VeVIs#WDHA.15 36@cpmsftngxa06 .phx.gbl>
        | Subject: Re: ComboBox Items
        | Date: Wed, 6 Aug 2003 05:01:45 -0600
        | Lines: 91
        | X-Priority: 3
        | X-MSMail-Priority: Normal
        | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
        | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
        | Message-ID: <eMlRmoAXDHA.23 28@TK2MSFTNGP12 .phx.gbl>
        | Newsgroups: microsoft.publi c.dotnet.genera l
        | NNTP-Posting-Host: skybeam143.skyb eam.frii.net 216.17.229.143
        | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
        | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:103674
        | X-Tomcat-NG: microsoft.publi c.dotnet.genera l
        |
        | I gather this does not apply when you bind the combobox to a dataset? The
        | 1st item in the dataset shows in the text?
        |
        | Wayne
        |
        | "Jeffrey Tan[MSFT]" <v-jetan@online.mi crosoft.com> wrote in message
        | news:8VeVIs%23W DHA.1536@cpmsft ngxa06.phx.gbl. ..
        | >
        | > Hi Wayne,
        | >
        | > The item the combobox initially displays should be set by the combobox's
        | > text property.
        | >
        | > Hope this helps.
        | >
        | > Jeffrey Tan
        | > Microsoft Online Partner Support
        | > Get Secure! - www.microsoft.com/security
        | > This posting is provided "as is" with no warranties and confers no
        rights.
        | >
        | > --------------------
        | > | From: "Wayne Wengert" <wayne@wengert. org>
        | > | Subject: ComboBox Items
        | > | Date: Tue, 5 Aug 2003 15:49:31 -0600
        | > | Lines: 44
        | > | X-Priority: 3
        | > | X-MSMail-Priority: Normal
        | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
        | > | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
        | > | Message-ID: <uRH63t5WDHA.17 48@TK2MSFTNGP12 .phx.gbl>
        | > | Newsgroups: microsoft.publi c.dotnet.genera l
        | > | NNTP-Posting-Host: skybeam143.skyb eam.frii.net 216.17.229.143
        | > | Path:
        | >
        |
        cpmsftngxa06.ph x.gbl!cpmsftngx a10.phx.gbl!TK2 MSFTNGXA05.phx. gbl!TK2MSFTNGP0 8
        | > phx.gbl!TK2MSFT NGP12.phx.gbl
        | > | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:103592
        | > | X-Tomcat-NG: microsoft.publi c.dotnet.genera l
        | > |
        | > | I am trying to populate a combobox with a lisy of items. I want the
        1st
        | > item
        | > | to be "Make a Selection" and the following items will be names from a
        | > table
        | > | in my DB (See code below).
        | > |
        | > | When I run the project, the combox box initially displays an empty
        | > | selection. When I click on the drop down arrow I see the "Make a
        | > Selection"
        | > | followed by the other choices.
        | > |
        | > | How can I move everything up so that the "Make a Selection" is the
        | default
        | > | value in the combo box?
        | > |
        | > | Wayne
        | > |
        | > | ============= code =============== =
        | > | Dim cn As New OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
        | > | Source=c:\Progr am Files\MyApp\MyD ata.mdb")
        | > |
        | > | strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"
        | > |
        | > | cn.Open() 'Open the connection
        | > |
        | > | Dim cmd As New OleDbCommand(st rSQL, cn)
        | > |
        | > | Dim rdr As OleDbDataReader = cmd.ExecuteRead er
        | > |
        | > | cboSample.Items .Clear()
        | > |
        | > | cboSample.Items .Add("Make a Selection")
        | > |
        | > | While (rdr.Read())
        | > |
        | > | cboSample.Items .Add(rdr.GetStr ing(0)) ' Name is the only field
        returned
        | > |
        | > | End While
        | > |
        | > | rdr.Close()
        | > |
        | > |
        | > | --
        | > | ------------------------------------
        | > | Wayne Wengert
        | > | wayne@wengert.o rg
        | > |
        | > |
        | > |
        | >
        |
        |
        |

        Comment

        Working...