.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

TextBox with ShowModalDialog - No Alpha input

6 REPLIES 6
Reply
Message 1 of 7
bruceradtke
515 Views, 6 Replies

TextBox with ShowModalDialog - No Alpha input

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

 

6 REPLIES 6
Message 2 of 7
arcticad
in reply to: bruceradtke

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

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 3 of 7
bruceradtke
in reply to: arcticad

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

 

 

Message 4 of 7
StormyC
in reply to: bruceradtke

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()
Message 5 of 7
StormyC
in reply to: StormyC

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.

Message 6 of 7
bruceradtke
in reply to: StormyC

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

 

Message 7 of 7
StormyC
in reply to: bruceradtke

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. Smiley Happy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost