You are not logged in.

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

1

Thursday, October 20th 2011, 3:34pm

KeyPress Events - Filter

Hey everybody,

any chance getting this to work without setting up a keylistener the java way?

Jabaco Source

1
2
3
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub



Dani

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

2

Friday, October 21st 2011, 1:26am

Don't know whats your problem.
On my computer it works fine.

Greatings
theuserbl
theuserbl has attached the following image:
  • KeyPress.PNG

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

3

Friday, October 21st 2011, 10:07am

Thanks for your reply,

the problem is that the values are still passed through to the UI. In VB that is not the case!

[img]http://www.jabaco.org/board/index.php?page=Attachment&attachmentID=290&h=0ffc74b1a1606d76930a18e797443141779b6af3[/img]

Using a keylistener in Jabaco it would probably look something like this:

Jabaco Source

1
2
3
4
5
6
7
Public Sub keyTyped(arg2 As KeyEvent)
   ' Eingabebegrenzung auf 0-9
   Dim KeyCode As Integer
   KeyCode = arg2.getKeyChar
   If (KeyCode < arg2.VK_0) Or (KeyCode > arg2.VK_9) Then
      arg2.setKeyChar(0)
   End If


But: I was hoping that I dont have to set up a keylistener for every single textbox that I need to limit inputs.
I am aware that there is a sample app about input validation in the forum! Just looking for something slim!!

Any ideas?


Dani
Dani has attached the following image:
  • keypress.jpg

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

4

Friday, October 21st 2011, 11:57am

Ok, then it seems, that you have to write your own Usercontrol.

Here I tried out something. Its quick and dirty code:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Dim Buffer As String = "0"

Public Sub Usercontrol_Initialize()
   Text1.Left = 0
   Text1.Top = 0
   Text1.Width = Me.Width
   Text1.Height = Me.Height
   Text1.Text="0"
End Sub

Public Sub Text1_KeyPress(KeyAscii As Integer)
   If ((KeyAscii < 47 Or KeyAscii > 57) And KeyAscii <> 8) Then
     Text1.Text = Buffer
   Else
     Buffer = Text1.Text
   End If
End Sub

Public Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
   If ((KeyCode < 96 Or KeyCode > 105) And KeyCode <> 8 And KeyCode <> 37 And KeyCode <> 38 And KeyCode <> 39 And KeyCode <> 40  ) Then
     Text1.Text = Buffer
   Else
     Buffer = Text1.Text
   End If
End Sub

Public Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
System.out.println(KeyCode)
   If ((KeyCode < 96 Or KeyCode > 105) And KeyCode <> 8 And KeyCode <> 37 And KeyCode <> 38 And KeyCode <> 39 And KeyCode <> 40 ) Then
     Text1.Text = Buffer
   Else
     Buffer = Text1.Text
   End If   
End Sub


It seems, that it would be better, to use the Java way. I have at the moment no the time to search for it. In my little implementation it writes the letter and removes it. Backspace don't work, etc.

Greatings
theuserbl

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

5

Friday, October 21st 2011, 1:42pm

Thanks theuserbl,

that is quick ... And dirty ;) ...
I am aware of a solution like that. As you pointed out it might be better to use the java referenz.
Thats my thing with Jabaco I guess. I am never quite sure whether to use Java "shortcuts"/"detours" or not!?

Staying with our little example here if I would set up a keylistener

Jabaco Source

1
Text1.Parent.addKeyListener(Me)


I would have to do that for every TextBox I need one for. Now in the key event I need to sort out wich TextBox fired the event

Jabaco Source

1
If Text1.Parent.hasFocus = True Then


Is it possible to add different keylisteners to different controls?
Does all this have any impact performancewise compared to using the VB syntax of jabaco (Private Sub Text1_KeyPress)?
The compiled .jar is pure java anyways so my guess is it does not make any difference!


Dani

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

6

Saturday, October 22nd 2011, 3:09am

Ok, I have now uploaded a solution which goes the Java way.

I would have to do that for every TextBox I need one for.
You only need to do it one time in a Usercontrol. And then use the Usercontrol again and again.

For the solution I have looked at http://edenti.deis.unibo.it/utils/Java-t…0JTextField.txt

Greatings
theuserbl
theuserbl has attached the following file:

Rate this thread
WoltLab Burning Board