2

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

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

3 回答 3

6

希望是 WinForms

var textboxes = (from textbox in this.Controls.OfType<TextBox>()
                    where textbox.SelectedText != string.Empty
                    select textbox).FirstOrDefault();
if (textboxes != null)
{
    textboxes.Cut();
}
于 2012-06-23T03:43:40.597 回答
4

循环遍历控件以找到具有选定文本的控件:

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.
        }
    }
}

希望这可以帮助!

于 2012-06-23T03:48:50.010 回答
1

尝试使用 commonEnterLeaveevents 设置最后一个具有焦点的 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;
        }
    }
}
于 2012-06-23T04:01:26.987 回答