Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, June 29, 2013 1:36 AM
Ok, I want to restrict a text box to only allow numbers between 0 and 100 (including decimals), i've tried many many ways, can anyone please shed some light on this problem? thankyou
All replies (7)
Saturday, June 29, 2013 1:52 AM ✅Answered
You should post the code that you have used an explain why it didn't do what you wanted, or what the errors where. Otherwise you are just going to get suggestions that you have already tried and dismissed.
You need to decide on how the user interaction is going to work. Do you want to check the input character by character, or do you want to wait until focus leaves the textbox? Do you want to sflash up a warning, ignore invalid chaacters, or simply adjust the entered value to the allowed range?
If you want to handle it on a character by character basis, us the textbox texxt changed event and examine the contents of the textbox after each change.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx
If you want to handle it when the focus moves on, use the validating events.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx
A better choice might be to use a control specifically designed for the type of input. The options are disucssed here:
http://msdn.microsoft.com/en-us/library/ms229603.aspx
Saturday, June 29, 2013 2:18 AM ✅Answered
I think you have a design problem. More thinking + a familiarity with "the way things are done" is the solution not 'solving' this fiasco.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
Saturday, June 29, 2013 2:40 AM ✅Answered
Try something like this
Option Strict On
Public Class Form1
Friend WithEvents TextBox1 As New TextBox With {.Parent = Me}
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If Char.IsDigit(ChrW(e.KeyCode)) Then
ElseIf e.KeyCode = Keys.Back Then
ElseIf e.KeyCode = Keys.Delete Then
ElseIf e.KeyCode = Keys.Up Then
ElseIf e.KeyCode = Keys.Down Then
ElseIf e.KeyCode = Keys.Left Then
ElseIf e.KeyCode = Keys.Right Then
ElseIf e.KeyCode = Keys.OemPeriod Then
Else
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
Dim Adjusted As String = adjInput(0, 100, TextBox1.Text)
TextBox1.Text = Adjusted
TextBox1.SelectionStart = TextBox1.Text.Length - 1
TextBox1.SelectionLength = 0
End Sub
Sub ShowMessage1()
MessageBox.Show("Your value was not within range!(0-100)", "Input value adjusted!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Sub
Function adjInput(ByVal Min As Decimal, ByVal Max As Decimal, ByVal Input As String) As String
Dim Value As Decimal
If Not Decimal.TryParse(Input, Value) Then Return "0"
If Value > Max Then Value = Max : ShowMessage1()
If Value < Min Then Value = Min : ShowMessage1()
Return Math.Round(Value, 3).ToString
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'When you click this button the textbox will lose focus & the inputted value will be adusted automatically
MsgBox(TextBox1.Text)
End Sub
End Class
“If you want something you've never had, you need to do something you've never done.”
Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles |
*This post does not reflect the opinion of Microsoft, or its employees.
Saturday, June 29, 2013 3:22 AM ✅Answered
Maybe this.
http://msdn.microsoft.com/en-us/library/System.Decimal.aspx
Public Class Form1
Dim i As Decimal = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Testing for Decimal in TextBox"
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Try
i = Decimal.Parse(TextBox1.Text, Globalization.NumberStyles.AllowDecimalPoint)
Catch ex As Exception
Dim ToolTip1 As New ToolTip
ToolTip1.IsBalloon = True
ToolTip1.ToolTipIcon = ToolTipIcon.Error
ToolTip1.ToolTipTitle = "ONLY USE DECIMALS FROM 0 TO 100"
ToolTip1.Show("Style is not a Decimal value", TextBox1, New Point(0, -80), 2500)
TextBox1.Text = ""
End Try
If i >= 0 AndAlso i <= 100 Then
Exit Sub
Else
Dim ToolTip1 As New ToolTip
ToolTip1.IsBalloon = True
ToolTip1.ToolTipIcon = ToolTipIcon.Error
ToolTip1.ToolTipTitle = "ONLY USE DECIMALS FROM 0 TO 100"
ToolTip1.Show("Value was out of range", TextBox1, New Point(0, -80), 2500)
TextBox1.Text = ""
End If
End Sub
End Class
You've taught me everything I know but not everything you know. _________________________________________________________________________________________________________________ This search engine is for MSDN Library and has many features. http://social.msdn.microsoft.com/Search/en-US?query=search%20msdn%20library&beta=0&ac=8
Saturday, June 29, 2013 3:28 AM ✅Answered
JB,
My two cents worth follows: Don't restrict the users to anything -- let them type in whatever they want but use the .Validating Event to then check what they've typed.
If it doesn't pass the tests (what if it's just text that cannot be converted to a type double/decimal?) which would include the range of acceptable values, then advise them and set e.Cancel to true -- BUT -- give them an out lest they can't even close the program.
I won't go into all of that unless you ask but that's how I'd do it, so for what it's worth ... :)
Please call me Frank :)
Saturday, June 29, 2013 4:24 PM ✅Answered
Instead of a Textbox, why not use a NumericUpDown control? The user will only be able to select or enter values between the minimum and maximum. The default setting is for a minimum value of 0 and maximum of 100, which can be changed. It will also work with decimal values if you set the Decimal property to the number of decimal places.
Solitaire
Saturday, June 29, 2013 3:42 AM
Agreed here also.