今天碰到一個(gè)需求:就是鼠標(biāo)雙擊ComboBox后,然后模擬鍵盤(pán)空格鍵按下,測(cè)試發(fā)現(xiàn),在ComboBox可以展開(kāi)下拉框的情況下,鼠標(biāo)雙擊事件是沒(méi)有用的。想要實(shí)現(xiàn)鼠標(biāo)雙擊事件,需要利用到鼠標(biāo)單擊事件,在鼠標(biāo)單擊事件中判斷(當(dāng)前時(shí)間減去上一次單擊的時(shí)間)是否小于某個(gè)值(如200毫秒),則認(rèn)為是用戶進(jìn)行了鼠標(biāo)雙擊。
在做的過(guò)程中又額外增加了一個(gè)要求,某些ComboBox在鼠標(biāo)點(diǎn)擊時(shí)就能展開(kāi)下拉列表,而不用點(diǎn)擊ComboBox后面的下拉小箭頭。要點(diǎn)擊鼠標(biāo)就能展開(kāi)ComboBox的選項(xiàng),這就需要在鼠標(biāo)單擊事件中把ComboBox的屬性DroppedDown設(shè)置為T(mén)rue
注意:其中踩了一個(gè)很冤的坑,計(jì)算兩個(gè)時(shí)間間隔的總毫秒,用了TimeSpan的Milliseconds,其實(shí)是不對(duì)的,Milliseconds計(jì)算的是TimeSpan秒數(shù)部分的值,應(yīng)該是TimeSpan的TotalMilliseconds
測(cè)試環(huán)境:
vistual studio 2017
.net framework? 4.0
測(cè)試步驟如下:
1? ?新增winfrom項(xiàng)目,名為:WindowsFormsApp1
2? 在界面中拖入兩個(gè)ComboBox控件,名稱為comboBox1和comboBox2,布局如下圖:
設(shè)置comboBox1和comboBox2的下拉選項(xiàng)值如下圖:
?
3? 新增類ComboBoxBp并編輯如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public class ComboBoxBp
{
/// <summary>
/// 設(shè)置ComboBox控件鼠標(biāo)雙擊業(yè)務(wù)邏輯
/// </summary>
/// <param name="cmbList"></param>
public void SetComboBoxControlMouseDouble(List<ComboBox> cmbList)
{
if (cmbList == null || cmbList.Count == 0) return;
foreach (var item in cmbList)
{
item.MouseClick += Cmb_MouseClick;
}
}
private DateTime dtCmbDeptLastClick = DateTime.MinValue;
/// <summary>
/// 鼠標(biāo)單擊
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cmb_MouseClick(object sender, MouseEventArgs e)
{
double gapMillises = (DateTime.Now - dtCmbDeptLastClick).TotalMilliseconds;
//SystemInformation.DoubleClickTime的值為300
if (gapMillises < SystemInformation.DoubleClickTime)
{
Cmb_DoubleMouseClick(sender, e);
}
dtCmbDeptLastClick = DateTime.Now;
GetTestMessage(sender, e);
}
/// <summary>
/// 鼠標(biāo)雙擊
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cmb_DoubleMouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("鼠標(biāo)雙擊:"+(sender as ComboBox).Name);
}
/// <summary>
/// ComboBox的屬性信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GetTestMessage(object sender, MouseEventArgs e)
{
//ComboBox comboBox = sender as ComboBox;
//if (comboBox != null)
//{
// int comboWidth = comboBox.Width - 20;
// if (e.Location.X > comboWidth && comboWidth > 0) return;
// int mouseY = e.Y;
// if (mouseY > comboBox.Height) return;
//}
//e.Location.X的值等于comboBox.Width e.Location.Y等于comboBox.Height
}
}
}
?Form1的代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
new ComboBoxBp().SetComboBoxControlMouseDouble(new List<ComboBox>() { this.comboBox1,this.comboBox2});
this.comboBox1.DroppedDown = true;
}
}
}
?目前是批量ComboBox注冊(cè)鼠標(biāo)雙擊事件,代碼比較簡(jiǎn)單,就不解釋了。
4 運(yùn)行結(jié)果如下:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-526294.html
好了,本文到此結(jié)束?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-526294.html
到了這里,關(guān)于解決Winform的ComboBox下拉框鼠標(biāo)雙擊事件無(wú)效的問(wèn)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!