Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a toolStripComboBox. I want to make selectedindexchanged event, but only when mouse click, also when pressing up and down is not done. People can help me? Thanks
Posted
Comments
syed shanu 24-Nov-14 21:12pm    
You can use some status variable and chek for mouse click event for exampole declare one global Boolean variable ,bool IsMouseclicked=false;
and in key prese set the IsMouseclicked=false and in muce click event set it to true IsMouseclicked=true; in selected index change event check for the
if(IsMouseclicked)
{
your code .....
}
Kit1624 24-Nov-14 23:21pm    
Thank you
Kit1624 24-Nov-14 23:31pm    
You can not give me one example? And Code Function IsMouseClick how so?
syed shanu 24-Nov-14 23:32pm    
not understand what you mean
Kit1624 24-Nov-14 23:37pm    
private void toolStripComboBoxLeft_SelectedIndexChanged(object sender, EventArgs e)
{
//Code.....
}

I want an example.

How to completely disable use of Arrow Keys in a ComboBox (the easy way).

A very powerful feature in .NET is the ability to create custom Components by inheritance that modify the behavior and functionality of existing Controls. In the case of the built-in Microsoft Controls you can simply create a new Component and make it inherit from an existing Control.

How is inheriting from a Component different from a UserControl: a Component uses the existing user-interface of the existing Control it inherits from, a UserControl requires you design a user-interface.

Note that Components can also be used to define Controls that have no user-interface but which provide behavior and functionality to a WinForm; the Timer Control is an example of this type of Component.

A UserControl lets you create a compound Control by drag-dropping other Controls into its design-time surface, or creating and adding them in code. While (strangely enough) Visual Studio does allow you to drag-drop Controls onto the "blank" design-view of a Component, I have yet to see a case where there is an organic need for this, and, in the case of inheriting from something like a ComboBox, you would just waste your time doing this as well as create a bizarre non-feature.

1. Create a new Component:

Visual Studio / Project / Add Component => ComboBoxNoArrowKeys.cs => save

2. Open the code window for the new Component, clear it, and paste in this code:
C#
using System.ComponentModel;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class ComboBoxNoArrowKeys : ComboBox
    {
        public ComboBoxNoArrowKeys()
        {
            InitializeComponent();
        }

        public ComboBoxNoArrowKeys(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
 
        public bool DisableArrowKeys { set; get; }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if(DisableArrowKeys) 
            {
                switch (keyData)
                {
                    case Keys.Up:
                    case Keys.Down:
                    case Keys.Left:
                    case Keys.Right:
                        return true;
                    default:
                        break;
                }
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
} 
Note there's a Public Proerty that implements a boolean flag here that Controls whether Arrow Keys are enabled or disabled, and I've set it to 'true by default. You can change that in your run-time code, or at design-time as you wish.
 
Share this answer
 
Hi check this code Hope this will help you.

C#
 Boolean ibMouseClicked = false;
//Toolstrip Combobox selected index change event.Here i check for the boolean value if  its false it will exit from the method with out executing the remaining code.
        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!ibMouseClicked)
            {
                return;
            }
            MessageBox.Show("Now the Mouse is presed");
            if (toolStripComboBoxLeft.Text != "...")
            {
            }
        }
//Toolstrip Key up event i set status as false
        private void toolStripComboBox1_KeyUp(object sender, KeyEventArgs e)
        {
            ibMouseClicked = false;
        }
       //Toolstrip Key down event i set status as false
        private void toolStripComboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            ibMouseClicked = false;
        }
//Toolstrip Key press event i set status as false
        private void toolStripComboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            ibMouseClicked = false;
        }
//Toolstrip Mouse Down event i set status as false
        private void toolStripComboBox1_MouseDown(object sender, MouseEventArgs e)
        {
            ibMouseClicked = true;
        }
 
Share this answer
 
Comments
Kit1624 25-Nov-14 2:12am    
thank you very much!
BillWoodruff 25-Nov-14 2:40am    
Have you actually tested this and confirmed it disables selection by using the arrow-keys in the DropDown ? I believe this will not work, even if the one obvious error in the code is fixed.
syed shanu 25-Nov-14 2:45am    
I think his aim is not to disables selection of Arrow-keys using the arrow key .His aim was not to do any process during arrow key press and he want to perform action only in mouse click. My sample will work in that case. Since I did few conversations with him and get for what he need and then made this sample.
BillWoodruff 25-Nov-14 3:33am    
My vote of #1: Not only will this code not compile as it is, it DOES allow selection via the arrow keys. The Mouse MUST go up to trigger the SelectedItemChanged event of the ComboBox.
syed shanu 25-Nov-14 3:35am    
The answer is accepted by the user.Its no meaning of your vote here

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900