【问题标题】:issue in creating Backspace key behaviour in wpf在 wpf 中创建退格键行为的问题
【发布时间】:2012-07-08 13:08:49
【问题描述】:
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个空格,然后在222 和 333 通过点击 TAB 按钮 (btn_t) 2 次。

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

【问题讨论】:

    标签: c# wpf


    【解决方案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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 2014-11-19
      • 2012-08-16
      • 2012-01-27
      • 1970-01-01
      • 2013-11-10
      相关资源
      最近更新 更多