anakinsolois Posted July 20, 2004 Share Posted July 20, 2004 Lets say i have this text box in which i need only a numerical input. How do i go about setting a condition which only accepts numeric values? btw, im using Visual Studio 2005 Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted July 20, 2004 Share Posted July 20, 2004 Here's how I would do it. Create handlers for the Validating and Validated events. Add an Error Provider control to your form. Inside your Validating handler, do this: Private Sub ValidatingHandler(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating Dim tb As TextBox If TypeOf sender Is TextBox Then tb = CType(sender, TextBox) Try If tb.Text.Length > 0 Then Integer.Parse(tb.Text) End If Catch ErrorProvider1.SetError(tb, "Input should be numeric only") e.Cancel = True End Try End If End Sub Then in my Validated handler, I'd do this: Private Sub ValidatedHandler(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.Validated If TypeOf sender Is TextBox Then Dim tb As TextBox = CType(sender, TextBox) ErrorProvider1.SetError(tb, "") End If End Sub In your constructor, or New subprocedure, you can add other controls to use the handlers for validating. 'Add any initialization after the InitializeComponent() call AddHandler TextBox2.Validating, AddressOf ValidatingHandler AddHandler TextBox2.Validated, AddressOf ValidatedHandler Link to comment Share on other sites More sharing options...
0 anakinsolois Posted July 21, 2004 Author Share Posted July 21, 2004 Thanks for the helper weenur, nut im not too good with VB. Heres my code: Public Class Temperature_Convertor Dim choice As Boolean Private Sub rbtCentigrade_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtCentigrade.CheckedChanged choice = True End Sub Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtFarenheitToCentigrade.CheckedChanged choice = False End Sub Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click Dim n As String n = txtInput.Text If n <> "" Then 'Add check for non-numeric value here Conversion() Else MessageBox.Show("Enter a value before clicking", "Error") End If End Sub Sub Conversion() Dim n As String n = txtInput.Text If choice = True Then n = (n * 180 / 100) - 32 MessageBox.Show("Value in Fahrenheit is " & n, "Fahrenheit Answer") Else n = (n + 32) * 100 / 180 MessageBox.Show("Value in Centigrade is " & n, "Centigrade Answer") End If End Sub End Class All I want is something that checks of n is a numeric value where it says "'Add check for non-numeric value here". To me this seems a bit complicated just to make a small check like that. If theres no other way, where would i put the code you gave me? Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted July 21, 2004 Share Posted July 21, 2004 You could use the IsNumeric function. '... Dim n As String n = txtInput.Text If n <> "" Then 'Add check for non-numeric value here If Microsoft.VisualBasic.IsNumeric( n ) = True Then Conversion() Else ' Add message End If Else 'etc... It may be a good idea to not use text boxes and instead use a NumericUpDown control. It prevents the entry of non-numeric characters right off the bat. Link to comment Share on other sites More sharing options...
0 anakinsolois Posted July 21, 2004 Author Share Posted July 21, 2004 (edited) Thanks, im using the NumericUpDown Control, its a bit easier. Just one thing, it it possible to make the scroll arrows on the box disappear? EDIT : Also, i click the button, it computes the value and resets to 0 or 100. How do i stop it from happening? Edited July 21, 2004 by anakinsolois Link to comment Share on other sites More sharing options...
0 anthonycara Posted July 22, 2004 Share Posted July 22, 2004 why not use a MaskedEditBox control? It should be on your system. Ctrl + T and look for the Microsoft MaskedEdit Control, and set it's input type to numeric ;) and you could use ### for a certain number of digits if you wanted :) Link to comment Share on other sites More sharing options...
0 anakinsolois Posted July 22, 2004 Author Share Posted July 22, 2004 Thanks, ill try that out now Link to comment Share on other sites More sharing options...
Question
anakinsolois
Lets say i have this text box in which i need only a numerical input. How do i go about setting a condition which only accepts numeric values?
btw, im using Visual Studio 2005
Link to comment
Share on other sites
6 answers to this question
Recommended Posts