只有鼠标点击事件selectedIndexchange of toolStripComboBox [英] Only mouse click event selectedindexchanged of toolStripComboBox

查看:284
本文介绍了只有鼠标点击事件selectedIndexchange of toolStripComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个toolStripComboBox。我想制作selectedindexchanged事件,但只有当鼠标点击时,以及上下按下时都没有。人们可以帮助我吗?谢谢

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

推荐答案

如何完全禁用ComboBox中箭头键的使用(简单方法)。



.NET中一个非常强大的功能是能够通过继承来创建自定义组件,从而修改现有控件的行为和功能。对于内置的Microsoft Controls,您可以简单地创建一个新的Component并使其从现有的Control继承。



如何从一个Component继承而不是一个UserControl:一个Component使用它继承的现有Control的现有用户界面,UserControl要求你设计一个用户界面。



注意组件也可以用于定义没有用户界面但为WinForm提供行为和功能的控件;定时器控件就是这种类型组件的一个例子。



UserControl允许您通过将其他控件拖放到其设计时表面来创建复合控件,或者在代码中创建和添加它们。虽然(奇怪的是)Visual Studio允许您将控件拖放到组件的空白设计视图中,但我还没有看到有这种情况的有机需求,并且在继承的情况下从像ComboBox这样的东西,你只是浪费你的时间来做这个以及创建一个奇怪的非功能。



1.创建一个新组件:



Visual Studio / Project / Add Component => ComboBoxNoArrowKeys.cs =>保存



2.打开新组件的代码窗口,清除它并粘贴此代码:
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:
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);
        }
    }
} 

请注意,这里有一个公共Proerty,它实现了一个布尔标志,用于控制是否启用箭头键,并将其设置为'默认是真的。您可以在运行时代码中或在设计时根据需要更改它。

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.


您好,请查看此代码希望这对您有所帮助。



Hi check this code Hope this will help you.

 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;
        }


这篇关于只有鼠标点击事件selectedIndexchange of toolStripComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆