我自己寫的一個ComboBox控件,雙擊事件沒反應,代碼如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace Qx_Mis
{
/// <summary>
/// Use_ComboBox 的摘要描述。
/// </summary>
public class User_ComboBox : System.Windows.Forms.ComboBox
{
private System.Windows.Forms.ToolTip toolTip1;
private System.ComponentModel.IContainer components; public User_ComboBox()
{
// 此為 Windows.Forms 表單設計工具所需的呼叫。
InitializeComponent();
this.DisplayMember="strDisplayMember";
this.ValueMember="strValueMember"; // TODO: 在 InitializeComponent 呼叫之後加入任何初始設定 } /// <summary>
/// 清除任何使用中的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region 元件設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
// 
// toolTip1
// 
this.toolTip1.AutoPopDelay = 5000;
this.toolTip1.InitialDelay = 200;
this.toolTip1.ReshowDelay = 100;
// 
// User_ComboBox
// 
this.toolTip1.SetToolTip(this, "右擊清空內容"); }
#endregion

protected override void OnMouseDown(MouseEventArgs e)
{
//base.OnMouseDown (e);
if(e.Button==MouseButtons.Right)
{
this.SelectedIndex=-1;
}
} protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
string dataMember=this.Name.Trim().Substring(3);
dataMember=dataMember.Substring(0,dataMember.LastIndexOf("_"));
System_Form_Code form=new System_Form_Code(this.DataSource,dataMember);
form.ShowDialog();
}
// protected override void OnClick(EventArgs e)
// {
// base.OnClick(e);
// string dataMember=this.Name.Trim().Substring(3);
// dataMember=dataMember.Substring(0,dataMember.LastIndexOf("_"));
// System_Form_Code form=new System_Form_Code(this.DataSource,dataMember);
// form.ShowDialog();
// }
}
}

解决方案 »

  1.   

    我在form中使用一個System.Windows.Forms.ComboBox的控件,直接注冊其DoubleClick事件,還是沒反應,why?
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace Qx_Mis
    {
    /// <summary>
    /// Form1 的摘要描述。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private Qx_Mis.User_ComboBox cboCode_Duty_Number;
    private System.Windows.Forms.ComboBox comboBox1;
    /// <summary>
    /// 設計工具所需的變數。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows Form 設計工具支援的必要項
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
    //
    } /// <summary>
    /// 清除任何使用中的資源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form 設計工具產生的程式碼
    /// <summary>
    /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
    /// 這個方法的內容。
    /// </summary>
    private void InitializeComponent()
    {
    this.cboCode_Duty_Number = new Qx_Mis.User_ComboBox();
    this.comboBox1 = new System.Windows.Forms.ComboBox();
    this.SuspendLayout();
    // 
    // cboCode_Duty_Number
    // 
    this.cboCode_Duty_Number.DisplayMember = "strDisplayMember";
    this.cboCode_Duty_Number.Location = new System.Drawing.Point(109, 126);
    this.cboCode_Duty_Number.Name = "cboCode_Duty_Number";
    this.cboCode_Duty_Number.Size = new System.Drawing.Size(75, 20);
    this.cboCode_Duty_Number.TabIndex = 9;
    this.cboCode_Duty_Number.ValueMember = "strValueMember";
    this.cboCode_Duty_Number.DoubleClick += new System.EventHandler(this.cboCode_Duty_Number_DoubleClick);
    // 
    // comboBox1
    // 
    this.comboBox1.Location = new System.Drawing.Point(176, 48);
    this.comboBox1.Name = "comboBox1";
    this.comboBox1.Size = new System.Drawing.Size(121, 20);
    this.comboBox1.TabIndex = 10;
    this.comboBox1.Text = "comboBox1";
    this.comboBox1.DoubleClick += new System.EventHandler(this.cboCode_Duty_Number_DoubleClick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.comboBox1);
    this.Controls.Add(this.cboCode_Duty_Number);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion private void cboCode_Duty_Number_DoubleClick(object sender, System.EventArgs e)
    {
    MessageBox.Show("aa");
    }
    }
    }
      

  2.   

    不是双击事件没反应,而是当前combobox获得焦点后,捕获双击事件的是combobox的textbox,而不是combobox。如果比较好的实现这个功能,你需要重写combobox,然后在绑定doubleclick事件时,把它传递给其的textbox即可。
      

  3.   

    combobox不支持DoubleClick事件!可通过Timer实现: