[英]mouse clickable backspace button in c#
我想在 windows 中為我的計算器應用程序制作一個退格按鈕,問題是它刪除了 cursor 右側的字符,而不是左側我該怎么辦?
private void DeleteTextValue()
{
//if we don't have any value to delete, return
if (this.UserInput.Text.Length < this.UserInput.SelectionStart + 1)
return;
//remember cursor location
var selectionStart = this.UserInput.SelectionStart;
//delete the char to the right of the cursor
this.UserInput.Text = this.UserInput.Text.Remove(this.UserInput.Text.Length-1, 1);
//restore the cursor location
this.UserInput.SelectionStart = selectionStart;
//unhighlight the text
this.UserInput.SelectionStart = 0 ;
}
試試這個,我使用 substring 而不是刪除。
private void DeleteTextValue()
{
//if we don't have any value to delete, return
if (this.UserInput.Text.Length < this.UserInput.SelectionStart + 1)
return;
//remember cursor location
var selectionStart = this.UserInput.SelectionStart;
//delete the char to the right of the cursor
//this.UserInput.Text = this.UserInput.Text.Remove(this.UserInput.Text.Length - 1, 1);
if (UserInput.SelectedText.Length > 0)
{
UserInput.Text = this.UserInput.Text.Substring(0, this.UserInput.SelectionStart) + "" + this.UserInput.Text.Substring(this.UserInput.SelectionStart + this.UserInput.SelectionLength);
selectionStart = selectionStart - this.UserInput.SelectionLength;
}
else
{
UserInput.Text = this.UserInput.Text.Substring(0, this.UserInput.SelectionStart - 1) + "" + this.UserInput.Text.Substring(this.UserInput.SelectionStart);
selectionStart = selectionStart - 1;
}
UserInput.Focus();
//restore the cursor location
this.UserInput.SelectionStart = selectionStart;
//unhighlight the text
this.UserInput.SelectionLength= 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.