2016-04-23 35 views
0

我對c#比較新,但我正在創建一個具有編輯器窗口的Windows窗體。儘管我正在努力使用粘貼按鈕,但我有2個文本框字段,一個用於註釋的標題,另一個用於註釋本身。我希望能夠從剪貼板粘貼到任何一個文本框中。如何使用按鈕將剪貼板中的文本粘貼到選定的文本框中

我已經嘗試使用基於noteText.FocusedtitleText.Focused的if語句,但顯然這不起作用,因爲只要您單擊它,粘貼按鈕就會變爲焦點。

任何建議都會有很大的幫助。

+0

所以問題是關於獲取剪貼板數據或獲取焦點文本框? – Shaharyar

+0

只需將最後一個固定的文本框存儲在__variable__類中。 – TaW

+0

問題是關於獲取剪貼板數據並將其粘貼到「最後聚焦」文本框中。 –

回答

1

創建一個局部變量並將最後聚焦的文本框保存在其中。

//subscribe both textBoxes with same GotFocus event handler 
textBox1.GotFocus += textBox_GotFocus; 
textBox2.GotFocus += textBox_GotFocus; 

//local variable 
TextBox lastSelected; 

//GotFocus 
private void textBox_GotFocus(object sender, EventArgs e) 
{ 
    //save last Selected textBox 
    lastSelected = sender as TextBox; 
} 

private void button1_Click_1(object sender, EventArgs e) 
{ 
    //on click get value from clipboard 
    if(lastSelected != null) 
     lastSelected.Text = Clipboard.GetText(); 
} 
+0

非常感謝,這個作品很棒! –

相關問題