Hello,
I have a Form with a UserControl that has a Textbox on it.
I want to be able to enter negative numbers in the Textbox.
However, it will only allow me to input the chars [0-9 and a period '.'
I have added KeyDown, KeyPress, PreviewKeyDown events for the form, UserControl and the Textbox.
None of these receive events for characters other than the chars above.
I am displaying the form with the Application.ShowModalDialog( myForm) call.
I am guessing that the Application object is eating the keypress event.
Is there any workaround for this?
Thanks,
bruce
You will need to show some of your code, but this is an example of how to filter for text.
Public inProcess As Boolean ' Prevent reentering the loop Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged If inProcess = True Then Exit Sub inProcess = True ' Find position of curser. Dim pos As Integer = TextBox1.SelectionStart - 1 If TextBox1.Text.Length > 0 Then Dim chr As Char = TextBox1.Text.Substring(pos, 1) ' Add allowable characters here If (Asc(chr) >= 48 And Asc(chr) <= 57) Or chr = "." Or chr = "-" Then Else ' Split String at the cursers Position Dim strPre As String = TextBox1.Text.Substring(0, pos) ' Check if position is at the end. If pos = TextBox1.Text.Length Then TextBox1.Text = strPre Else Dim strPost As String = TextBox1.Text.Substring(pos + 1, (TextBox1.Text.Length - pos) - 1) ' reassemble text TextBox1.Text = strPre + strPost End If ' Reset curser position TextBox1.SelectionStart = pos End If End If inProcess = False End Sub
Articad,
Thanks for the reply. But I don't think I stated my problem clearly.
In my code the TextBox1_TextChanged method is NOT getting called back for alpha characters.
It DOES get called back for numbers and the decimal character. Basically it allows you to enter a real number, ie: "123.456". But I can't enter a negative number, which is my problem - ie "-123.456". If I try to enter the "-" sign, the TextBox does not change and my TextChanged method is not called back.
I thought this may be associated with the parent form passed into ShowModalDialog. My hypothesis was that the parent form was filtering out the alpha characters. I tried using the desktop window (from GetDesktopWindow() ) but that did not work.
Hast anyone created a form with a Textbox using ApplicationServices.Application.ShowModalDialog() ?
Could you input alpha characters?
Thanks,
bruce
You may have a textbox control that has been modified so it cancels the keypress of alpha keys in a very sililar way to the code below. (ie trapping the event)
Have a go at replacing the control and using the code below. I looked this up a while back but sadly can't remember the original author....
Private Sub tBx_TextChanged(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) _ Handles tBx.KeyPress Dim isKey As Boolean = Char.IsDigit(e.KeyChar) Dim isDecimal As Boolean = e.KeyChar.ToString = "." Dim isMinus As Boolean = e.KeyChar.ToString = "-" Dim isCtrl As Boolean = Char.IsControl(e.KeyChar) If Not isKey And Not isMinus And Not isDecimal And Not isCtrl Then e.Handled = True End If End Sub
I use the ShowDialog() method of the form which shows the form as modal.....
Dim frm As New MyForm frm.ShowDialog()
Ive just changed my call to;
Imports AcadAppl = Autodesk.AutoCAD.ApplicationServices.Application Dim frm As New MyForm AcadAppl.ShowModalDialog(frm)
And as far as I can tell the code runs in exaclty the same way.
Stormy,
Thanks for the reply.
I did try a plain old vanilla Textbox. I placed a brand new Textboxt on the form and still can not receive alpha characters. It accepts number ( 0 - 9 ) and the decimal char ( '.' ).
Regards,
bruce
How odd...
Can you post the form.designer.vb file - maybe there is somthing amiss there.
Other than the above - all I can think of is a modified textbox control has replaced the original. Im not even sure if thats even possible however if I was in your shoes I would be tempted to reinstall the VB.Studio if you can without causing any other upsets........
If you discover a solution / reason then please post - will be very interested.