Solved

How to select only one value in the ComboxBox List

Posted on 2015-02-19
5
48 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
  • 2
  • 2
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
can you please share me the sample syntax, please
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
>>>can you please share me the sample syntax, please

cmboBox.Enabled = false;
0
 

Author Closing Comment

by:chokka
Comment Utility
Thank you it_saige
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

15 Experts available now in Live!

Get 1:1 Help Now