473,508 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\Program Files\MyApp\MyData.mdb")

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

cn.Open() 'Open the connection

Dim cmd As New OleDbCommand(strSQL, cn)

Dim rdr As OleDbDataReader = cmd.ExecuteReader

cboSample.Items.Clear()

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

While (rdr.Read())

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

End While

rdr.Close()
--
------------------------------------
Wayne Wengert
wa***@wengert.org
Jul 19 '05 #1
3 7469

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" <wa***@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: <uR**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.general
| NNTP-Posting-Host: skybeam143.skybeam.frii.net 216.17.229.143
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.phx.gbl!TK2MSFTNGP08
phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:103592
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| 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=Microsoft.Jet.OLEDB.4.0; Data
| Source=c:\Program Files\MyApp\MyData.mdb")
|
| strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"
|
| cn.Open() 'Open the connection
|
| Dim cmd As New OleDbCommand(strSQL, cn)
|
| Dim rdr As OleDbDataReader = cmd.ExecuteReader
|
| cboSample.Items.Clear()
|
| cboSample.Items.Add("Make a Selection")
|
| While (rdr.Read())
|
| cboSample.Items.Add(rdr.GetString(0)) ' Name is the only field returned
|
| End While
|
| rdr.Close()
|
|
| --
| ------------------------------------
| Wayne Wengert
| wa***@wengert.org
|
|
|

Jul 19 '05 #2
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-*****@online.microsoft.com> wrote in message
news:8V****************@cpmsftngxa06.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" <wa***@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: <uR**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.general
| NNTP-Posting-Host: skybeam143.skybeam.frii.net 216.17.229.143
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.phx.gbl!TK2MSFTNGP08 phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:103592
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| 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=Microsoft.Jet.OLEDB.4.0; Data
| Source=c:\Program Files\MyApp\MyData.mdb")
|
| strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"
|
| cn.Open() 'Open the connection
|
| Dim cmd As New OleDbCommand(strSQL, cn)
|
| Dim rdr As OleDbDataReader = cmd.ExecuteReader
|
| cboSample.Items.Clear()
|
| cboSample.Items.Add("Make a Selection")
|
| While (rdr.Read())
|
| cboSample.Items.Add(rdr.GetString(0)) ' Name is the only field returned
|
| End While
|
| rdr.Close()
|
|
| --
| ------------------------------------
| Wayne Wengert
| wa***@wengert.org
|
|
|

Jul 19 '05 #3

Hi Wayne,

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

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// 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.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

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

private void Form1_Load(object sender, System.EventArgs e)
{
SqlDataAdapter adapter=new SqlDataAdapter ("select * from titles where
price!=0","server=localhost;database=pubs;uid=sa;p wd=");

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" <wa***@wengert.org>
| References: <uR**************@TK2MSFTNGP12.phx.gbl>
<8V**************@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: <eM**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.general
| NNTP-Posting-Host: skybeam143.skybeam.frii.net 216.17.229.143
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:103674
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| 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-*****@online.microsoft.com> wrote in message
| news:8V****************@cpmsftngxa06.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" <wa***@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: <uR**************@TK2MSFTNGP12.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.general
| > | NNTP-Posting-Host: skybeam143.skybeam.frii.net 216.17.229.143
| > | Path:
| >
|
cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.phx.gbl!TK2MSFTNGP08
| > phx.gbl!TK2MSFTNGP12.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:103592
| > | X-Tomcat-NG: microsoft.public.dotnet.general
| > |
| > | 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=Microsoft.Jet.OLEDB.4.0; Data
| > | Source=c:\Program Files\MyApp\MyData.mdb")
| > |
| > | strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"
| > |
| > | cn.Open() 'Open the connection
| > |
| > | Dim cmd As New OleDbCommand(strSQL, cn)
| > |
| > | Dim rdr As OleDbDataReader = cmd.ExecuteReader
| > |
| > | cboSample.Items.Clear()
| > |
| > | cboSample.Items.Add("Make a Selection")
| > |
| > | While (rdr.Read())
| > |
| > | cboSample.Items.Add(rdr.GetString(0)) ' Name is the only field
returned
| > |
| > | End While
| > |
| > | rdr.Close()
| > |
| > |
| > | --
| > | ------------------------------------
| > | Wayne Wengert
| > | wa***@wengert.org
| > |
| > |
| > |
| >
|
|
|

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6362
by: Rachel Suddeth | last post by:
When I put a ComboBox on a form, I can enter strings through the designer to fill Items collections. I have put a ComboBox in a UserControl, and exposed the the Items collections with the same...
5
25829
by: James P. | last post by:
Hello, For my new Windows application, all I want is to create an initial form to demo to the user to show them how it looks like with some data on it. So I figure the fastest way is to create...
6
7792
by: Sakharam Phapale | last post by:
Hi All, How to fill one ComboBox from other ComboBox control? 1) Only setting the reference does the trick but doesn't show items in control. If you see in immediate window, it shows...
3
370
by: Wayne Wengert | last post by:
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...
8
26385
by: | last post by:
I am sure this has been asked and answered, but here goes anyway... VS.Net 2005, VB.Net How can you display more than one field in the displaymember property of a combobox inside the...
7
1896
by: samoore33 | last post by:
I am trying to dynamically add items to a listbox or combobox. The items add to either, but when I look through those items, there is nothing there. If I choose an item, it shows up. Not sure...
5
6779
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I want to create a customized ComboBox where the methods ComboBox.Items.Add and ComboBox.Items.Insert will behave thusly: -- If the item is not present, add it. -- If the item is present, set the...
4
6643
by: Jerad Rose | last post by:
I'm baffled by this -- is there not a typed object used for ComboBox Items? Best I can tell, all of the methods for ComboBox that accept an Item are of type Object. Why in the world is a...
5
18623
by: Kevinp | last post by:
I've spent the last four hours Google searching for a way to disable items in a Combobox. I found one example in C++ which I can't get to work and another in C# that I couldn't get to work either....
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.