Pages - Menu

2018年8月24日 星期五

C# Windows-Form 取得焦點時,同時反白文字方塊內容

明天颱風天,
今天就索性把前陣子累積再紀錄搖桿驅動程式的紀錄程式改一改
然後,想到了一條一直要做都沒做的功能。
當按Alt + Tab 切換回 視窗時,自動focus在文字方塊上,
方便讓我直接按ctrl+V貼上去查資料。
首先取得焦點的事件是 Form.Activated  ,來源 stack overflow
再來是反白文字方塊,在 Windows Form TextBox 控制項中選取文字
如果不想要反白,只想單純選取的話,stack overflow
整段加起來就是

private void Form1_Activated(object sender, EventArgs e)
{
     txtKeyword.SelectionStart = 0;
     txtKeyword.SelectionLength = txtKeyword.Text.Length ;
     txtKeyword.Select();
}

沒有留言:

張貼留言