2021年1月23日星期六

Visual Basic: Limit Textbox to specific characters

What im trying to do, is to have a Windows Form Applications that, once a number is written in textbox and the accept button is pressed, will open another windows form where another number is written in another textbox and the second button will pressed, it will show a third form, will should only accept four digits "+" "-" "*" "/" each representing a basic math operation, however, im facing two problems at the moment.

For the first two boxes, I was able to made it so it only allows numbers, the problem is, I cant delete a number once its written, and I can type several "." for a floating number, which means that I can write numbers like "5136....5.25" which will obviously lead to an error when the calculation at the end is done. I havent found a way to limit that "." to only one, and I cant fix the delete, or backstep button either. This is the code im using.

'Button  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Form4.n1 = TextBox1.Text      Me.Hide()      Form2.Show()  End Sub  'Textbox  Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress      e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or Asc(e.KeyChar = "8"))  End Sub  

Now with the third windows form I have another problem, I need to limit it to only accept "+" "-" "*" or "/" (only one of them, not more, and no numbers or characters either)

I feel the solution for these problems is the same, I need to limit somehow the numbers of "." and simbols to only one, and I need to limit the simbols in the third box to only those four.

Any ideas? Also, im quite a newbie, so any tips or explication will be quite appreciated, even if it makes the answer longer

https://stackoverflow.com/questions/65866203/visual-basic-limit-textbox-to-specific-characters January 24, 2021 at 09:05AM

没有评论:

发表评论