vb.net 为计算器编写退格按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22974904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:19:56  来源:igfitidea点击:

Writing a backspace button for calculator

vb.net

提问by Jose M.

I am working on a form with a Numbers keyboard and a backspace. The user enters the numbers on a textbox using the numbers provided in the form. I have a backspace button that I want to program. I wrote the following:

我正在处理带有数字键盘和退格键的表单。用户使用表单中提供的数字在文本框中输入数字。我有一个要编程的退格按钮。我写了以下内容:

            Private Sub btnBackSpace_Click_1(sender As Object, e As EventArgs) Handles btnBackSpace.Click

    'The procedure works as a backspace for the
    'cash box

    If txtCash.Text < " " Then

        txtCash.Text = Mid(txtCash.Text, 1, Len(txtCash.Text) - 1 + 1)

    Else

        txtCash.Text = Mid(txtCash.Text, 1, Len(txtCash.Text) - 1)

    End If

End Sub

The only problem is that it goes from the last character and back, which is fine, but I wanted to also go from the cursor placement.

唯一的问题是它从最后一个字符开始并返回,这很好,但我也想从光标位置开始。

How can I get that done.

我怎样才能做到这一点。

采纳答案by Hanlet Esca?o

This should do the trick:

这应该可以解决问题:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    txtCash.Focus()
    SendKeys.Send("{BACKSPACE}")
End Sub

回答by jmoreno

I would go with Hanlet's solution, but...the SelectionStartproperty of a textbox tells you where the caret sits. From there, you should be able to do the manipulation by hand if so desired.

我会使用 Hanlet 的解决方案,但是...文本框的SelectionStart属性会告诉您插入符号所在的位置。从那里,如果需要,您应该能够手动进行操作。