rekam Posted October 13, 2003 Posted October 13, 2003 Hellooo! Well, here's the point. Everybody knows what's a TextBox. My goal is to have something like a TextBox, but in which we can only put numbers. I've done a KeyPress method which does all the controls I want, the problem is not here. I'd like to have a component in the ToolBar, which I can drag 'n drop onto the Form, just like a TextBox. But this NumericBox component would have the capacity to accept only numbers. Someone has an idea ? I checked the MSDN documentation and forums too, but I didn't find a single clue.... Quote
*Experts* Bucky Posted October 13, 2003 *Experts* Posted October 13, 2003 What you want to do is create a new Windows Control project. Go to the code of the control and change it to inherit TextBox instead of UserControl. Then create a subroutine that overrides the base class's OnKeyPress event. So, in your component, something like this: Public Overrides Sub OnKeyPress(sender As Object, e As KeyPressEventArgs) MyBase.OnKeyPress(sender, e) ' Call the base class method ' Add your code here that validates the key press End Sub Add a Windows Forms project to your solution, then reference the component project and test it out. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
rekam Posted October 14, 2003 Author Posted October 14, 2003 Yeah, ok :) ! I tried to do so. It seems that we can't override a KeyPress method, but we can overload it. I really don't the difference, the fact is, in the end, that doesn't work. However, I have build my new component and I succeded in putting it into the toolbar. When I inherits it from a textBox, it takes the properties of the textBox, great :) ! But the overload keyPress method I add isn't interpreted by the program. Hum, here's a part of the class : Public Class NumericBox Inherits System.Windows.Forms.TextBox Public Overloads Sub OnKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) If Me.controleNombre(Me, e) = False Then e.Handled = True End If End Sub Private Function controleNombre(ByVal textBox As System.Windows.Forms.TextBox, ByVal e As System.Windows.Forms.KeyPressEventArgs) As Boolean Dim estUnNombre As Boolean estUnNombre = True If (e.KeyChar < "0" Or e.KeyChar > "9") Then estUnNombre = False End If Return estUnNombre End Function End Class The function "controleNombre" works well. But even if I put System.Windows.Forms.MessageBox.Show("Hello") in the KeyPress method, nothing happens...Where's the problem ? Quote
AlexCode Posted October 14, 2003 Posted October 14, 2003 I think you're complicating this a little bit... Use this code: Public Class myTextBox Inherits System.Windows.Forms.TextBox #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'UserControl overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() ' 'myTextBox ' Me.Name = "myTextBox" End Sub #End Region Private Sub myTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then e.Handled = True End If End Sub End Class Quote Software bugs are impossible to detect by anybody except the end user.
rekam Posted October 14, 2003 Author Posted October 14, 2003 Great, really great ! It works at last !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Thank you very much:D Quote
AlexCode Posted October 14, 2003 Posted October 14, 2003 Anything else... you know... Just ask :D Quote Software bugs are impossible to detect by anybody except the end user.
Leaders dynamic_sysop Posted October 14, 2003 Leaders Posted October 14, 2003 although you dont need to specify the < 48 and > 57, you could use the inbuilt method that .net hold for the job ;) Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar.IsNumber(e.KeyChar) Then '/// if it's numeric allow it. e.Handled = False Else e.Handled = True End If End Sub Quote
AlexCode Posted October 14, 2003 Posted October 14, 2003 :D I've forgot that... Thanx ! Quote Software bugs are impossible to detect by anybody except the end user.
*Experts* Volte Posted October 14, 2003 *Experts* Posted October 14, 2003 You also have to handle the backspace key. The ASCII code is 8, I believe, so you can useIf Char.IsNumber(e.KeyChar) OrElse KeyChar = Convert.ToChar(8) Then Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.