How to select only one value in the ComboxBox List

Posted on 2015-02-19
Medium Priority
Last Modified: 2015-03-10
I have a ComboBox which has 3 values

such as - NewYork,NewJersey,Boston.

Combobox.selectedIndex = 1  which is Boston,

On one scenario, i need to set only for Boston. I believe we can do it by ReadOnly or something like that.

I dont want to go for Enable = True / False. How to set this ?
Question by:chokka
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 44

Expert Comment

ID: 40619607
Comboboxs don't have a readonly property.
Either disable it or remove the unwanted selection possibilities (ie. it only has Boston in the list part)

Author Comment

ID: 40619618
can you please share me the sample syntax, please
LVL 34

Accepted Solution

it_saige earned 2000 total points
ID: 40619698
Here is an example:

Form1.cs -
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;

namespace EE_Q28620474
	public partial class Form1 : Form
		private List<string> cities = new List<string>() { "New York", "New Jersey", "Boston" };

		public Form1()

		private void OnLoad(object sender, System.EventArgs e)
			cmbAll.DataSource = cities;
			cmbBOS.DataSource = (from city in cities where city.Equals("Boston") select city).ToList();
			cmbNY.DataSource = (from city in cities where city.Equals("New York") select city).ToList();
			cmbNJ.DataSource = (from city in cities where city.Equals("New Jersey") select city).ToList();

Open in new window

Form1.Designer.cs -
namespace EE_Q28620474
	partial class Form1
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
			if (disposing && (components != null))

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
			this.lblAll = new System.Windows.Forms.Label();
			this.cmbAll = new System.Windows.Forms.ComboBox();
			this.lblNewYork = new System.Windows.Forms.Label();
			this.cmbNY = new System.Windows.Forms.ComboBox();
			this.lblNewJersey = new System.Windows.Forms.Label();
			this.cmbNJ = new System.Windows.Forms.ComboBox();
			this.lblBoston = new System.Windows.Forms.Label();
			this.cmbBOS = new System.Windows.Forms.ComboBox();
			// lblAll
			this.lblAll.AutoSize = true;
			this.lblAll.Location = new System.Drawing.Point(12, 9);
			this.lblAll.Name = "lblAll";
			this.lblAll.Size = new System.Drawing.Size(49, 13);
			this.lblAll.TabIndex = 3;
			this.lblAll.Text = "All Items:";
			// cmbAll
			this.cmbAll.FormattingEnabled = true;
			this.cmbAll.Location = new System.Drawing.Point(14, 25);
			this.cmbAll.Name = "cmbAll";
			this.cmbAll.Size = new System.Drawing.Size(121, 21);
			this.cmbAll.TabIndex = 2;
			// lblNewYork
			this.lblNewYork.AutoSize = true;
			this.lblNewYork.Location = new System.Drawing.Point(141, 9);
			this.lblNewYork.Name = "lblNewYork";
			this.lblNewYork.Size = new System.Drawing.Size(57, 13);
			this.lblNewYork.TabIndex = 5;
			this.lblNewYork.Text = "New York:";
			// cmbNY
			this.cmbNY.FormattingEnabled = true;
			this.cmbNY.Location = new System.Drawing.Point(143, 25);
			this.cmbNY.Name = "cmbNY";
			this.cmbNY.Size = new System.Drawing.Size(121, 21);
			this.cmbNY.TabIndex = 4;
			// lblNewJersey
			this.lblNewJersey.AutoSize = true;
			this.lblNewJersey.Location = new System.Drawing.Point(270, 9);
			this.lblNewJersey.Name = "lblNewJersey";
			this.lblNewJersey.Size = new System.Drawing.Size(65, 13);
			this.lblNewJersey.TabIndex = 7;
			this.lblNewJersey.Text = "New Jersey:";
			// cmbNJ
			this.cmbNJ.FormattingEnabled = true;
			this.cmbNJ.Location = new System.Drawing.Point(272, 25);
			this.cmbNJ.Name = "cmbNJ";
			this.cmbNJ.Size = new System.Drawing.Size(121, 21);
			this.cmbNJ.TabIndex = 6;
			// lblBoston
			this.lblBoston.AutoSize = true;
			this.lblBoston.Location = new System.Drawing.Point(399, 9);
			this.lblBoston.Name = "lblBoston";
			this.lblBoston.Size = new System.Drawing.Size(43, 13);
			this.lblBoston.TabIndex = 9;
			this.lblBoston.Text = "Boston:";
			// cmbBOS
			this.cmbBOS.FormattingEnabled = true;
			this.cmbBOS.Location = new System.Drawing.Point(401, 25);
			this.cmbBOS.Name = "cmbBOS";
			this.cmbBOS.Size = new System.Drawing.Size(121, 21);
			this.cmbBOS.TabIndex = 8;
			// Form1
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(530, 61);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.OnLoad);



		private System.Windows.Forms.Label lblAll;
		private System.Windows.Forms.ComboBox cmbAll;
		private System.Windows.Forms.Label lblNewYork;
		private System.Windows.Forms.ComboBox cmbNY;
		private System.Windows.Forms.Label lblNewJersey;
		private System.Windows.Forms.ComboBox cmbNJ;
		private System.Windows.Forms.Label lblBoston;
		private System.Windows.Forms.ComboBox cmbBOS;


Open in new window

Which produces the following output -Capture.JPGBut this then raises the question of why make the user chose a drop down item if it only has one item to choose?  Just food for thought.

LVL 44

Expert Comment

ID: 40620560
>>>can you please share me the sample syntax, please

cmboBox.Enabled = false;

Author Closing Comment

ID: 40657733
Thank you it_saige

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 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