跳到主要內容

WinForm裏ToolBar/ToolStrip不會先Focus的Bug

同事遇到一個奇怪的Bug,她說用DateTimePicker手動改變時間,在儲存時卻不會生效。原本我以為是DateTimePicker的Bug,仔細一查卻發現是她用的ToolBar的Bug,這情形在較新的ToolStrip元件也會發生。

因為在下很久沒有寫WinForm,所以昨天特地下載Visual C# 2010 Express測試。先拉一個DateTimePicker元件、ButtonToolStrip元件,接著在ToolStrip增加一個Button,雙擊之,再加入兩行。

MessageBox.Show(dateTimePicker1.Text);
MessageBox.Show(dateTimePicker1.Text);

執行後去改dateTimePicker1的值,在日期仍反日時馬上按toolStripButton1,會發現第一次是今天的值,第二次才會更新;但若用button1則不會有這問題。

因此解決就是在toolStripButton1_Click() 加上
toolStrip1.Focus();
此處Focus在dateTimePicker1以外任何其他元件均可。
完整測試程式如下:
using System;
using System.Windows.Forms;

namespace TestDateTimePicker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void MessageTwice()
        {
            MessageBox.Show(dateTimePicker1.Text);
            MessageBox.Show(dateTimePicker1.Text);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            toolStrip1.Focus();
            MessageTwice();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageTwice();
        }
    }
}

執行畫面如下:

留言

這個網誌中的熱門文章

Personal Bookmark

Java SE 6 + Firefox 2 UI 問題 As I do . Google拋棄了了SOAP API,浮想聯翩 https://www.gandi.net/ VS 2005 SP1中文版推出 Windows Vista中文版下載 ASP.NET 2.0網頁執行管線與快取原理 Cache 2.0快取架構與快取資料自動移除架構圖 flickr sync 分享與試用 SUN Looking Glass 3D圖形介面發布1.0 雅虎勵精圖治推動改革 Wait and see 國內某SOC疑遭駭客入侵 大砲開講 Very Important! 微軟公佈Vista安全程式介面草案 一窺Google開原碼庫房乾坤 qing is writing a dig girl net... wait and see

DBeaver 介面語言

DBeaver是我個人頗常用的一套跨平台Database管理工具,最近升級後發現Windows版本居然變成簡體中文,而且無法切換為英文。

自然人憑證讀卡機驅動程式

鳥毅用的是第一代的自然人憑證讀卡機,EZ100PU(後來有同事買EZmini可以讀SIM卡似乎更好),每年報稅時用一次。 本來只是要申請些政府業務,一時之間找不到光碟,沒想到在 驅動程式下載 居然看到Linux和Mac的驅動程式,剩下的就是政府單位的網頁和程式應該改版了吧!!!