Click here to Skip to main content
Licence CPOL
First Posted 23 Apr 2009
Views 11,121
Downloads 95
Bookmarked 12 times

Simplest Way to Make Readonly ComboBox

By | 5 May 2009 | Article
Readonly ComboBox
tt.jpg

Introduction

I was looking on this site to find a way to make a read only ComboBox. There is more than one article to do that, but some of them are very huge and some do not work, especially against Delete button. That made me look for a solution for this problem. Finally, I found a very simple way to do that. The idea is to control the input keys.

There are two types of input keys:

  1. Normal keys (letters, numbers…)
  2. Control keys (delete, left…)

Using the Code

The simplest way to control a Normal key is by converting it to null before using it. To do that, we can handle the input keys for the combo box by using: KeyPress event (or simply disable this event), see the code. And now, a slightly harder step, controlling the control keys. First, let us watch the key pressed before doing its work, if it is a control key, we will not pass it to do as an underline control. To do that, the best way is to use the KeyDown Event. 

My ComboBox allows: Cntr+C(copy) and arrows effects. Finally we will create contextMenuStrip to allow only copy, as shown below:

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
}

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData == (Keys)Shortcut.CtrlC)
    {
         Clipboard.SetData(DataFormats.Text, comboBox1.SelectedText);
    }
    else if (e.KeyData == Keys.Left || e.KeyData == Keys.Right || 
		e.KeyData == Keys.Up || e.KeyData == Keys.Down) { }
    else
    {
         e.Handled = true;
    }
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
     Clipboard.SetData(DataFormats.Text, comboBox1.SelectedText );
}

And now you can improve your code as you wish. You can also add your own features. If you have any questions, please post them.

Hints

  1. You can change the style of combobox to list style. (See the code.) But, doing that may remove some features (copy, select text, … ), your choice depends on your application.
    private void Form1_Load(object sender, EventArgs e) 
    {  
        comboBox2.DropDownStyle = ComboBoxStyle.DropDownList; 	// thanks for 
    							// your commands 
    }
  2. If someone wants to delete contextMenuStrip effect "Mouse right-click", you can add the following:
    private void Form1_Load(object sender, EventArgs e)
    { 
        comboBox3.ContextMenu = new ContextMenu();
    }

License

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

About the Author

batati

Engineer
Still Student
Yemen Yemen

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberPatric_J8:01 23 Apr '09  
GeneralRe: My vote of 1 Pinmembersoftsniper8:32 23 Apr '09  
GeneralRe: My vote of 1 PinmemberPatric_J5:12 24 Apr '09  
GeneralRe: My vote of 1 Pinmembersoftsniper10:17 24 Apr '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 6 May 2009
Article Copyright 2009 by batati
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid