How to select only one value in the ComboxBox List

Posted on 2015-02-19
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
  • 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 33

Accepted Solution

it_saige earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

864 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now