Solved

How to select only one value in the ComboxBox List

Posted on 2015-02-19
5
54 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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