Module to test character

I have this code in the KeyDown area of a textbox:

Select Case ASCB( key )
Case 8 // backspace key
  Return False
Case 27 // Escape key
Return False
Case 127 // delete key
  Return False
Case 9 // TAB key
Case 13 // RETURN KEY
Return False
Case 3 // ENTER KEY
 Return False  
Case 28 // Arrow Key
  Return True
Case 29// Arrow Key
  Return True
Case 30// Arrow Key
  Return True
Case 31// Arrow Key
  Return True
Case 32 // Space
  Return False
Case 33 // Exclamation mark
  Return True
Case 34 // Double quote
  Return True
Case 35 // Number
  Return True
Case 36 // Dollar sign
  Return True
Case 37 // Percent
  Return True
Case 38 // Ampersand
  Return True
Case 39 // Single quote
  Return True
Case 40 // Left parenthesis
  Return True
Case 41 // Right parenthesis
  Return True
Case 42 // Asterisk
  Return True
Case 43 // Plus
  Return True
Case 44 // Comma
  Return True
Case 45 // Minus
  Return True
Case 46 // Period
  Return True
Case 47 // Slash
  Return True
Case 58 // Colon
  Return True
Case 59 // Semicolon
  Return True
Case 60 // Less than
  Return True
Case 61 // Equality sign
  Return True
Case 62 // Greater than
  Return True
Case 63 // Question mark
  Return True
Case 64 // At sign
  Return True
Case 91 // Left square bracket
  Return True
Case 92 // Backslash
  Return True
Case 93 // Right square bracket
  Return True
Case 94 // Caret / circumflex
  Return True
Case 95 // Underscore
  Return True
Case 96 // Grave / accent
  Return True
Case 123 // Left curly bracket
  Return True
Case 124 // Vertical bar
  Return True
Case 125 // Right curly bracket
  Return True
Case 126 // Tilde
  Return True
Case Else
  Return False
End Select

And I am wondering if I can put this in a module then call it from the various textboxes that use it instead of having it replicated multiple times across multiple textboxes.

Why not put it in a textbox subclass, so all your textboxes use it. (Set the Super of your existing textboxes to your subclass.) Then when you get your next big idea, you change the subclass and all your textboxes get it automatically; you don’t have to touch them at all.

Also, your Case 9 isn’t doing anything, it will just drop through to any code after the select case.

Also, since False is the default return value, you only have to test the cases where you want to return True.

2 Likes

Well… because I don’t know how to set that up… I don’t see an option for SubClass. I see one for Class. I understand the changing of the Super, but I have not a clue how to set up a subclass… so I will start researching that now…

Add a Class, set its Super to TextBox and put your code in its KeyDown event. Set your current Textboxes’ Super to your new class.

some readings to consider :
https://documentation.xojo.com/getting_started/object-oriented_programming/oop_classes.html
https://documentation.xojo.com/getting_started/object-oriented_programming/subclassing_examples.html

Or just drag a TextField or a TextArea to the navigator directly, give it an appropriate name (or leave it as CustomTextfield1). Then the KeyDown event can be added there and your code copied in.

FWIW, if you still need the fields to have a KeyDown event, you can right-click on this new KeyDown event and select Add Event Definition. It’ll copy the signature of the selected event and make a new one for the subclasses. Then in your select-case statement, under Case Else, put:

Return KeyDown(key)

2 Likes

Okay, I got that to work, but I have unique code for each textbox that I want to execute when either enter key is pressed. Not sure how to do that, I have tried a couple things but nothing yet seems to work the way I expect it too.

I got the character limitation working, thank you, but originally I had code in each TextBox.KeyDown event that would execute when either Enter key was pressed. Now, I don’t know how to do this, or where to put the code then execute it.

Did you read Jean-Yves (above) links ?

Follow my instructions about creating the new event definition to do it in KeyDown.

If you want a specially named event, right-click on the custom control, Add an Event Definition called EnterPressed with a return type of Boolean and then in your select case, instead of just returning True, do:

Return EnterPressed()

Greg, I’ve always wondered why and how you could call the same keydown event and this (after following your example) shows how to do it.

1 Like

I have been attempting to figure out what you mean but I am not getting anywhere.


Class

Here is another attempt at understanding and doing… it didn’t go so well.

I can’t see from your screenshots what the signature of the KeyDown event definition is.

I am pretty sure you want to use
RaiseEvent EnterPressed
instead of
Return EnterPressed

:wink:

Eureka! The cranial capacitor has gained new knowledge!!! Outstanding! Woo hoo! I be doing the happy dance! Learned something new that will further trim my code base!

Return RaiseEvent EnterPressed( "13" ) = True
Return RaiseEvent EnterPressed( "3" ) = True
1 Like

For what it’s worth, this works exactly the same:

return EnterPressed("3")

I suspect your event definition is wrong if you’re accepting a string when you want the integer returned from AscB.

1 Like

It works both ways. I was reading the text at the bottom of the Window and the format was like I posted. Thanks though, I am once again learning some quite useful stuff… respect and appreciation for the time y’all are taking to help.