我有很多文本框。我有一個按鈕可以剪切Focused文本框的選定文本。我怎麼做?我曾經嘗試這樣做:在C#中剪切,複製並粘貼?
if (((TextBox)(pictureBox1.Controls[0])).SelectionLength > 0)
{
((TextBox)(pictureBox1.Controls[0])).Cut();
}
我有很多文本框。我有一個按鈕可以剪切Focused文本框的選定文本。我怎麼做?我曾經嘗試這樣做:在C#中剪切,複製並粘貼?
if (((TextBox)(pictureBox1.Controls[0])).SelectionLength > 0)
{
((TextBox)(pictureBox1.Controls[0])).Cut();
}
環路通過控制來找到一個與所選文本:
foreach (Control x in this.PictureBox1.Controls)
{
if (x is TextBox)
{
if (((TextBox)x).SelectionLength > 0)
{
((TextBox)(x).Cut(); // Or some other method to get the text.
}
}
}
希望這有助於!
希望它的WinForms
var textboxes = (from textbox in this.Controls.OfType<TextBox>()
where textbox.SelectedText != string.Empty
select textbox).FirstOrDefault();
if (textboxes != null)
{
textboxes.Cut();
}
嘗試使用常見的Enter
和Leave
事件來設置具有焦點的最後一個TextBox。
private void textBox_Enter(object sender, EventArgs e)
{
focusedTextBox = null;
}
private void textBox_Leave(object sender, EventArgs e)
{
focusedTextBox = (TextBox)sender;
}
private void button1_Click(object sender, EventArgs e)
{
if (!(focusedTextBox == null))
{
if (focusedTextBox.SelectionLength > 0)
{
Clipboard.SetText(focusedTextBox.SelectedText);
focusedTextBox.SelectedText = "";
focusedTextBox = null;
}
}
}
如果我有10個文本框,我需要連接所有帶有事件的文本框控件嗎?看起來很複雜:( – Anuraj
@Anuraj只需在你的winforms屬性編輯器中爲每個文本框選擇相同的處理程序應該不需要任何時間。我已經在具有100個或更多文本框的表單中使用了常見的事件處理程序。它有助於保持代碼的大小下。 –
我有很多文本框,但是一個按鈕會將所選文本剪切到焦點文本框的文本中。 –
您可能需要循環顯示文本框控件,如果文本框已經集中,則執行該操作。 – Anuraj
你會給我一個循環的例子,我將使用以及如何找到selectedtextBox。謝謝! –