Click here to Skip to main content
15,896,359 members
Articles / Programming Languages / C#

ComboBox with Read Only Behavior

Rate me:
Please Sign up or sign in to vote.
4.66/5 (24 votes)
25 Apr 2005CPOL5 min read 400.5K   5K   63  
An article about developing a ComboBox with the same read only functionality as the TextBox
using System.Drawing;
using System.Windows.Forms;
using ProjectMentor.Windows.Controls;

namespace TestReadOnlyComboBox
{
	/// <summary>
	/// Summary description for DifferentCombos.
	/// </summary>
	public class DifferentCombos : System.Windows.Forms.Form
	{
    private ReadOnlyComboBox _combo1;
    private ReadOnlyComboBox _combo2;
    private ReadOnlyComboBox _combo3;
    private Label _label1;
    private Label _label2;
    private Label _label3;

    /// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public DifferentCombos()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

      _label1 = new Label();
      _label1.Text = "ComboBox:";
      _label1.Location = new Point(10, 20);
      this.Controls.Add(_label1);

      _label2 = new Label();
      _label2.Text = "ComboBox disabled:";
      _label2.Width = 110;
      _label2.Location = new Point(10, 50);
      this.Controls.Add(_label2);
      
      _label3 = new Label();
      _label3.Text = "ComboBox readonly:";
      _label3.Width = 110;
      _label3.Location = new Point(10, 80);
      this.Controls.Add(_label3);

      _combo1 = new ReadOnlyComboBox();
      _combo1.Location = new Point(140, 20);
      _combo1.Size = new Size(100, 21);
      _combo1.TabIndex = 1;
      _combo1.Items.AddRange(new object[] { "ComboBox", "blue", "green", "yellow" });
      this.Controls.Add(_combo1);

      _combo2 = new ReadOnlyComboBox();
      _combo2.Location = new Point(140, 50);
      _combo2.Size = new Size(100, 21);
      _combo2.TabIndex = 1;
      _combo2.Enabled = false;
      _combo2.Items.AddRange(new object[] { "Enabled = false", "blue", "green", "yellow" });
      this.Controls.Add(_combo2);
      _combo2.SelectedIndex = 0;

      _combo3 = new ReadOnlyComboBox();
      this.Controls.Add(_combo3);
      _combo3.Location = new Point(140, 80);
      _combo3.Size = new Size(100, 21);
      _combo3.TabIndex = 1;
      _combo3.ReadOnly = true;
      _combo3.Items.AddRange(new object[] { "ReadOnly = true", "blue", "green", "yellow" });
      _combo3.SelectedIndex = 0;

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(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()
		{
      // 
      // DifferentCombos
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(272, 115);
      this.Name = "DifferentCombos";
      this.Text = "DifferentCombos";

    }
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions