Autore Topic: Usando MaskBox.Mask  (Letto 327 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.723
  • Ne mors quidem nos iunget
    • Mostra profilo
Usando MaskBox.Mask
« il: 27 Aprile 2014, 18:21:59 »
Vi riporto questa discussione apparsa nella M.L. ufficiale:


" Today I am using TextBox_Change event to reject | characters.

Codice: gambas [Seleziona]
 Public Sub txtName_Change()

   If String.InStr(Last.Text, "|") > 0 Then
     Last.Text = Replace(Last.Text, "|", "")
   Endif

 End


I guess MaskBox can do this much simplier, but how, what Mask?

Patrik
"


" No, MaskBox is there to force formatted input. But the Mask property is way
weaker than regular expressions. The main point is that you can only
describe fixed-length strings (plus/minus the finite number of #/9 fields
in your Mask). So wherever you want to forbid "|", you would write "[^|]"
but that also consumes any character but "|". So you cannot accept arbitrary
strings but only strings of a length equal to the times you forbid the "|"
character.

Your initial approach was much better but one thing: if you use InStr() each
time the TextBox changes, you will scan the whole contents each time they
change but you only want to capture inputs, right? If you tell your user not
to use Ctrl-V or stuff, you can do:

Codice: gambas [Seleziona]
Public Sub txtName_KeyPress()
  If Key.Text = "|" Then Stop Event
End

Regards,
Tobi
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »