【发布时间】: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次在111和222之间放置3个空格,然后在222 和 333 通过点击 TAB 按钮 (btn_t) 2 次。
当我单击退格按钮(btn_bs)时,每次只会清除一个空格或字母。但是我想要做的是,当单击退格按钮(btn_bs)时,如果文本框中有标签,那应该删除.如果文本框中有空格,那将删除。
【问题讨论】: