繁体   English   中英

剪切,复制和粘贴C#?

[英]Cut, Copy, and paste in C#?

我有很多文本框。 我有一个按钮,可以剪切聚焦文本框的选定文本。 我怎么做? 我试过这个:

        if (((TextBox)(pictureBox1.Controls[0])).SelectionLength > 0)
        {
            ((TextBox)(pictureBox1.Controls[0])).Cut();
        }

希望它是WinForms

var textboxes = (from textbox in this.Controls.OfType<TextBox>()
                    where textbox.SelectedText != string.Empty
                    select textbox).FirstOrDefault();
if (textboxes != null)
{
    textboxes.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.
        }
    }
}

希望这可以帮助!

尝试使用常见的EnterLeave事件来设置最后一个具有Focus的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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM