dynamic menu help please - select a value 0 to 369

Hello,

I would like to create a menu dynamically that allows me to select a value 0 to 369…
I did it, but it seems quite long, can this be done more concisely? The code is too long to post, I got this message

“Your post is really, really long! Too long! The maximum number of characters allowed is 10000. That’s really long!”

The project can be downloaded from
https://www.mediafire.com/?3dt30dmdfze7pvs

Thanks,

Lennox

370 items in a menu is a lot
Even breaking it into groups where you have a top level item with a submenu for the specific items
0 - 100
101 - 200
201 - 300
301 - 369
still leaves 100+ items in each sub menu
a slider with min = 0 and max = 369 might work more simply but I have no idea if that fits your UI

would it not be easier to simply have the user type in a 3 digiit number rather that have 36 menus with 10 submenus each?

remove all the code you have…
put this in the KEYDOWN event of the textfield

  dim x as integer
  if key=chrb(9) or key=chrb(10) or key=chrb(3) or key=chrb(8) or key=chrb(13) then return false
  if key<"0" or key>"9" then return true
  x= val(me.text+key)
  if x<0 or x>369 then return true
  return false

this will ONLY accept a number between 0 and 369 to be typed in

Love that error message :slight_smile:

Thanks,
Dave suggestion seems OK.
Lennox

Hi Dave,

I was trying to get one decimal place by modifying your code but couldn’t, can you advise how to do it?

Thanks.

Lennox

 dim x as double

Thanks Michel,
I tried that before and again but it is not accepting the point when typed.
Lennox

[quote=146997:@Lennox Jacob] dim x as integer
if key=chrb(9) or key=chrb(10) or key=chrb(3) or key=chrb(8) or key=chrb(13) then return false
if key<“0” or key>“9” then return true
x= val(me.text+key)
if x<0 or x>369 then return true
return false[/quote]

  • Copy the event
  • Delete the current TextField
  • Drag another one from the library
  • Paste back the Keydown event.

For whatever reason the textfield is corrupted in your project.

Thanks Michel,
I did that…

  1. dragged a new textField on to the window
  2. Added a keydown event
  3. added this …
    dim x as double
    if key=chrb(9) or key=chrb(10) or key=chrb(3) or key=chrb(8) or key=chrb(13) then return false
    if key<“0” or key>“9” then return true
    x= val(me.text+key)
    if x<0 or x>369 then return true
    return false
  4. Ran it in the IDE but could not input a point.
    1. Compiled it, but could not input a point.

I tried this too
dim x as double
if key=chrb(9) or key=chrb(10) or key=chrb(3) or key=chrb(8) or key=chrb(13) then return false
if key<“0” or key>“9” then return true
x= Cdbl(me.text+key)
if x<0 or x>369 then return true
return false

Could you send the project that you have for me?

Thanks.

Lennox

Don’t you just want to use a text box with a mask of ##.# ?

I have simply modified the project you posted. See the blue textfield, which has Dave code in the KeyDown event.

But I can input the dot in the pink one as well. The bug you report does not exist for me in 2014R2.1 under Yosemite.

Zero_to_369.xojo_binary_project

OK Michel,
Still cannot input the point in the blue.
Thanks, maybe it is because I am not using Yosemite, I am using 10.9.4
Lennox

In the blue tab, there is this code:

if key<"0" or key>"9" then return true

Which excludes the point.

You can try

if  (key<"0" or key>"9" ) and key <> "." then return true

Thanks Jeff,

That works great.
Thanks again.

I could have used “Don’t you just want to use a text box with a mask of ##.# ?” too, but I need some coding practice/exercise.

Lennox

If you wish to allow the use of normal editing keys such as the left and right arrow, delete, forward delete you may wish to go with something like this in the key down event.

[code] Dim n As Integer

n = asc(Key)
if n > 47 and n < 58 then // Check if a digit
return false
// check for left arrow, right arrow, delete, forward delete, tab key, minus sign and decimal point
elseif n = 28 or n = 29 or n = 8 or n = 127 or n = 9 or n = 45 or n = 46 then
return false
end if
beep // not a valid key so beep and return true so key is ignored
return true
[/code]
If you don’t want to allow minus signs then remove the “or n = 45” portion in the elseif above. Now the user can use the normal editing keys in the event they mistype a character.