2012-09-17 23 views
1

在WindowsForms我用這個samlpe代碼:WindowsForms TextBox方法也粘貼在WPF文本框?

textBox.Paste("some text"); 

是否有與WPF相同功能的文本框的方法? 或者有一個很好的解決方法?

+0

莫比此變通辦法使用WPF中的Textox幫助:HTTP:// WWW .c-sharpcorner.com/uploadfile/mahesh/using-windows-forms-controls-in-wpf/ – CloudyMarble

回答

0
public static void Paste(this TextBox textbox, string textToInsert) 
    { 
     int caretIndex = textbox.CaretIndex; 
     string textBoxContent; 

     if (textbox.SelectedText.Length > 0) 
     { 
      textBoxContent = textbox.Text.Remove(caretIndex, textbox.SelectedText.Length); 
     } 
     else 
     { 
      textBoxContent = textbox.Text; 
     } 

     textbox.Text = textBoxContent.Insert(caretIndex, textToInsert); 
     textbox.CaretIndex = caretIndex + textToInsert.Length; 
    } 
1

使用Clipboard類:

textBox1.Text = Clipboard.GetText(); 

或使用文本框的SelectedText屬性:

textBox1.SelectedText = "some text"; 
+0

也許這是不一樣的,因爲['Paste'](http://msdn.microsoft.com/zh-cn/ us/library/ms160635.aspx)d oes替換當前選定的文本,而您的解決方案將替換所有文本。 –

+0

此外,您使用剪貼板,而原始海報不使用剪貼板。 –