我有很多文本框。我有一个按钮可以剪切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。谢谢! –