0
public partial class Backspace : Window
{
    Control TextBoxDetails;
    TextBox BehaveTextbox;
    public Backspace()
    {
        this.InitializeComponent();

        // Insert code required on object creation below this point.
    }

    private void btn_t_Click(object sender, RoutedEventArgs e)
    {
        BehaveTextbox = TextBoxDetails as TextBox;
        if (TextBoxDetails != null)
        {
            var _CareIndex = BehaveTextbox.CaretIndex;
            BehaveTextbox.Text = BehaveTextbox.Text.Insert(_CareIndex, "      ");
            BehaveTextbox.Focus();
            BehaveTextbox.CaretIndex = _CareIndex + 6;
        }
    }

    private void btn_s_Click(object sender, RoutedEventArgs e)
    {
        BehaveTextbox = TextBoxDetails as TextBox;
        if (TextBoxDetails != null)
        {
            var _CareIndex = BehaveTextbox.CaretIndex;
            BehaveTextbox.Text = BehaveTextbox.Text.Insert(_CareIndex, " ");
            BehaveTextbox.Focus();
            BehaveTextbox.CaretIndex = _CareIndex + 1;
        }
    }

    private void btn_bs_Click(object sender, RoutedEventArgs e)
    {
        BehaveTextbox = TextBoxDetails as TextBox;

        if (TextBoxDetails != null)
        {

            string _CurrentValue = BehaveTextbox.Text;
            var _CareIndex = BehaveTextbox.CaretIndex;

            if (_CareIndex > 0)
            {
                string _Backspace = _CurrentValue.Remove(_CareIndex - 1, 1);
                BehaveTextbox.Text = _Backspace;
                BehaveTextbox.Focus();
                BehaveTextbox.CaretIndex = _CareIndex - 1;
            }
        }
    }

    private void txt_result_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBoxDetails = (Control)sender;
    }
}

设计

在上图中的文本框中有一些文本值。我通过单击 SPACE 按钮 (btn_s) 3 次在111222之间放置 3 个空格,然后通过单击 TAB 按钮 (btn_t) 2 次在222333之间放置 2 个选项卡。

当我单击退格按钮(btn_bs)时,每次只会清除一个空格或字母。但是我想要做的是,当单击退格按钮(btn_bs)时,如果文本框中有标签,那应该删除。如果文本框中有空格,那将删除。

4

1 回答 1

1

试试这个:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        Control TextBoxDetails;
        TextBox BehaveTextbox;

        private void btn_t_Click(object sender, RoutedEventArgs e)
        {
            BehaveTextbox = TextBoxDetails as TextBox;
            if (TextBoxDetails != null)
            {
                var _CareIndex = BehaveTextbox.CaretIndex;
                BehaveTextbox.Text = BehaveTextbox.Text.Insert(_CareIndex, "\t");
                BehaveTextbox.Focus();
                BehaveTextbox.CaretIndex = _CareIndex + 1;
            }
        }

        private void btn_s_Click(object sender, RoutedEventArgs e)
        {
            BehaveTextbox = TextBoxDetails as TextBox;
            if (TextBoxDetails != null)
            {
                var _CareIndex = BehaveTextbox.CaretIndex;
                BehaveTextbox.Text = BehaveTextbox.Text.Insert(_CareIndex, " ");
                BehaveTextbox.Focus();
                BehaveTextbox.CaretIndex = _CareIndex + 1;
            }
        }

        private void btn_bs_Click(object sender, RoutedEventArgs e)
        {
            BehaveTextbox = TextBoxDetails as TextBox;

            if (TextBoxDetails != null)
            {

                string _CurrentValue = BehaveTextbox.Text;
                var _CareIndex = BehaveTextbox.CaretIndex;

                if (_CareIndex > 0)
                {
                    string _Backspace = _CurrentValue.Remove(_CareIndex - 1, 1);
                    BehaveTextbox.Text = _Backspace;
                    BehaveTextbox.Focus();
                    BehaveTextbox.CaretIndex = _CareIndex - 1;
                }
            }
        }

        private void txt_result_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBoxDetails = (Control)sender;
        }
    }
于 2012-07-08T13:30:45.677 回答