Solved

How to select only one value in the ComboxBox List

Posted on 2015-02-19
5
53 Views
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 ?
0
Comment
Question by:chokka
[X]
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
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
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)
0
 

Author Comment

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

Accepted Solution

by:
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()
		{
			InitializeComponent();
		}

		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))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#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();
			this.SuspendLayout();
			// 
			// 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.Controls.Add(this.lblBoston);
			this.Controls.Add(this.cmbBOS);
			this.Controls.Add(this.lblNewJersey);
			this.Controls.Add(this.cmbNJ);
			this.Controls.Add(this.lblNewYork);
			this.Controls.Add(this.cmbNY);
			this.Controls.Add(this.lblAll);
			this.Controls.Add(this.cmbAll);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.OnLoad);
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		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.

-saige-
0
 
LVL 44

Expert Comment

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

cmboBox.Enabled = false;
0
 

Author Closing Comment

by:chokka
ID: 40657733
Thank you it_saige
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 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