[英]C#, Windows Forms: NotifyIcon with ShowBalloonTip in Click event no longer fires the DoubleClick event
[英]click and doubleclick event in c# not working
我在C#應用程序中有以下代碼
第一個決定用戶雙擊圖片時會發生什么。
private void pictureDoubleClick(Object sender, EventArgs e)
{
PictureBox picture = (PictureBox)sender;
Console.WriteLine(picture.ImageLocation);
MessageBox.Show("Test");
}
另一個單擊:
private void picutureClick(Object sender, EventArgs e)
{
PictureBox picture = (PictureBox)sender;
if (picture.BorderStyle == BorderStyle.None)
{
picture.BorderStyle = BorderStyle.Fixed3D;
picture.BackColor = Color.Red;
}
else
{
picture.BorderStyle = BorderStyle.None;
picture.BackColor = Color.White;
}
}
我把這兩個函數稱為:
box.Click += new System.EventHandler(this.picutureClick);
box.DoubleClick += new System.EventHandler(this.pictureDoubleClick);
但是,我面臨一個奇怪的錯誤,DoubleClick事件將不會被激活,唯一的方法是讓我的注釋單擊。 無論我是否評論或取消評論Doubleclick事件,單擊都有效。 我四處尋找解決方案,但我找不到任何解決了我的問題。
這是一種奇怪的行為,更改圖片框的BorderStyle
將阻止點擊傳播( Click
事件將始終在DoubleClick
事件之前提升)。
我真的不知道如何正確處理它,但我們可以做一些黑客來使功能工作。 我們可以在Click
和DoubleClick
之間引入“延遲”,以便在Click
之前檢查DoubleClick
。
這里我們使用Timer
來完成這項工作:
private Timer _timer;
private PictureBox _sender;
private int _clicks;
public Form1()
{
InitializeComponent();
pictureBox.Click += picutureClick;
pictureBox.DoubleClick += (s, e) =>
{
// do your double click handling
_clicks = 0;
};
// this Interval comes from trail and error, it's a balance between lag and
// correctness. To play safe, you can use SystemInformation.DoubleClickTime,
// but may introduce a bit long lagging after you click and before you
// see the effects.
_timer = new Timer {Interval = 75};
_timer.Tick += (s, e) =>
{
if (_clicks < 2)
{
ClickHandler(_sender);
}
_clicks = 0;
_timer.Stop();
};
}
private void picutureClick(Object sender, EventArgs e)
{
_clicks++;
_sender = (PictureBox) sender;
if (_clicks == 1)
{
_timer.Start();
}
}
private void ClickHandler(PictureBox picture)
{
if (picture.BorderStyle == BorderStyle.None)
{
// this line is not thread safe, but you could comment out the .InvokeIfRequire()
// and uncomment this line to have a look at your form's behavior
//picture.BorderStyle = BorderStyle.Fixed3D;
picture.InvokeIfRequired(c => (c as PictureBox).BorderStyle = BorderStyle.Fixed3D);
picture.BackColor = Color.Red;
}
else
{
// same for this
//picture.BorderStyle = BorderStyle.Fixed3D;
picture.InvokeIfRequired(c => (c as PictureBox).BorderStyle = BorderStyle.None);
picture.BackColor = Color.White;
}
}
在上面的代碼中,我使用了一個擴展方法來處理跨線程屬性更新:
public static void InvokeIfRequired(this Control c, Action<Control> action)
{
if (c.InvokeRequired)
{
c.Invoke(new Action(() => action(c)));
}
else
{
action(c);
}
}
編輯:
我上面使用的擴展方法是簡化為執行跨線程屬性更新而編寫的代碼。 有關此主題的更多詳細信息,請參見此處: 自動調用InvokeRequired代碼模式
以下是擴展方法的一些細節:
擴展方法僅在非泛型非嵌套靜態類中聲明時才有效。 要使其工作,您需要聲明一個新的public static class
來保存該方法。
// your form class declared here
public partial class Form1 : Form
{
// code omitted here
}
// declare the extension method in this extension class
public static class ControlExtensions
{
public static void InvokeIfRequired(this Control c, Action<Control> action)
{
if (c.InvokeRequired)
{
c.Invoke(new Action(() => action(c)));
}
else
{
action(c);
}
}
}
當你更改圖片框的邊框時,你有點重置點擊事件。 這就是為什么它沒有工作,只切換一次點擊事件,如果你評論邊界的變化,它將開始工作...抱歉,但我不知道為什么會發生這種情況,不知道這是否有用我的信息= /
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.