MaskedEdit in StringGadgets

Windows specific forum
xstreur
New User
New User
Posts: 7
Joined: Wed May 05, 2004 6:16 pm
Location: The Netherlands

MaskedEdit in StringGadgets

Post by xstreur »

I just started programming PureBasic last month. I love it for it's compactness and strength. Untill now I programmed in VB and PowerBasic.
I just miss something I often used in VB:

Is there a way to protect a StringGadget in terms of max.length and rules and formats? Like the Masked Edit field in VB?

Thanx in advance. :?:
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

xstreur,

You have some constants to manage the string gadget from Purebasic, as indicated in the documentation :

#PB_String_Numeric : Only numbers are accepted.
#PB_String_Multiline : Several lines of text are accepted.
#PB_String_Password : Password mode, displaying only '*' instead of normal characters.
#PB_String_ReadOnly : Read only mode. No text can be entered.
#PB_String_LowerCase : All characters are converted to lower case automatically.
#PB_String_UpperCase : All characters are converted to upper case automatically.
#PB_String_BorderLess : No borders are drawn around the gadget.

These constants are last optional parameter of StringGadget() command.

But also you can use all Edit Control Styles from API (ie #ES_something constants) that you can throw using SendMessage_() API call. The handle you will have to refer to is usually coded using :

- GadgetID(YourStringGadgetNumber)

Hope this helps.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
xstreur
New User
New User
Posts: 7
Joined: Wed May 05, 2004 6:16 pm
Location: The Netherlands

Post by xstreur »

FWeil,


Thanx! :D

I already use the standard constatnts to manage the Gadgets but I need more control on the input becaus eof fieldlength of the underlaing database.
Can you make the comments on the Edit Control Styles more specific and refere to a (online)manual where this topic is mentioned.

Thanx in advance.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Maybe you have Win32.hlp file on your drives, maybe not.

I suggest you to download this anyway.

The full version is a 11MB file, a bit huge for help but so useful.

Maybe the best is to ask Google for Win32.hlp as I don't know the quickest link to get it from your place.

So, the CreateWindow_() API command is explained with specific Edit Control management args.

Available specific styles for Edit Controls are :

Style Meaning
ES_AUTOHSCROLL Automatically scrolls text to the right by 10 characters when the user types a character at the end of the line. When the user presses the ENTER key, the control scrolls all text back to position zero.
ES_AUTOVSCROLL Automatically scrolls text up one page when the user presses the ENTER key on the last line.
ES_CENTER Centers text in a multiline edit control.
ES_LEFT Left-aligns text.
ES_LOWERCASE Converts all characters to lowercase as they are typed into the edit control.
ES_MULTILINE Designates a multiline edit control. The default is single-line edit control.
When the multiline edit control is in a dialog box, the default response to pressing the ENTER key is to activate the default button. To use the ENTER key as a carriage return, use the ES_WANTRETURN style.
When the multiline edit control is not in a dialog box and the ES_AUTOVSCROLL style is specified, the edit control shows as many lines as possible and scrolls vertically when the user presses the ENTER key. If you do not specify ES_AUTOVSCROLL, the edit control shows as many lines as possible and beeps if the user presses the ENTER key when no more lines can be displayed.
If you specify the ES_AUTOHSCROLL style, the multiline edit control automatically scrolls horizontally when the caret goes past the right edge of the control. To start a new line, the user must press the ENTER key. If you do not specify ES_AUTOHSCROLL, the control automatically wraps words to the beginning of the next line when necessary. A new line is also started if the user presses the ENTER key. The window size determines the position of the word wrap. If the window size changes, the word wrapping position changes and the text is redisplayed.
Multiline edit controls can have scroll bars. An edit control with scroll bars processes its own scroll bar messages. Note that edit controls without scroll bars scroll as described in the previous paragraphs and process any scroll messages sent by the parent window.
ES_NOHIDESEL Negates the default behavior for an edit control. The default behavior hides the selection when the control loses the input focus and inverts the selection when the control receives the input focus. If you specify ES_NOHIDESEL, the selected text is inverted, even if the control does not have the focus.
ES_NUMBER Windows 95 only: Allows only digits to be entered into the edit control.
ES_OEMCONVERT Converts text entered in the edit control. The text is converted from the Windows character set to the OEM character set and then back to the Windows set. This ensures proper character conversion when the application calls the CharToOem function to convert a Windows string in the edit control to OEM characters. This style is most useful for edit controls that contain filenames.
ES_PASSWORD Displays an asterisk (*) for each character typed into the edit control. You can use the EM_SETPASSWORDCHAR message to change the character that is displayed.
ES_READONLY Prevents the user from typing or editing text in the edit control.
ES_RIGHT Right-aligns text in a multiline edit control.
ES_UPPERCASE Converts all characters to uppercase as they are typed into the edit control.
ES_WANTRETURN Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiline edit control in a dialog box. If you do not specify this style, pressing the ENTER key has the same effect as pressing the dialog box's default push button. This style has no effect on a single-line edit control.

When creating a window, a gadget, on Windows platform, you generate behing a CreateWindow.

You don't need to replace OpenWindow() or StringGadget() by a CreateWindow command, but can change styles using SetWindowLong to change its style after creation.

I guess that from this you will probably find your way.

You will probably take large benefits in getting CodeArchiv (http://www.purearea.net/pb/CodeArchiv/CodeArchiv.html) files because much examples are listed using such disposals to change styles.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
spongehammer
User
User
Posts: 84
Joined: Sat Jul 19, 2003 6:45 pm
Location: UK

Post by spongehammer »

Hi xstreur,

this is a simple example to keep the length to an assigned limit

Code: Select all

Enumeration
  #Window_0
EndEnumeration


Enumeration
  #String_0
  #Text_0
EndEnumeration


  If OpenWindow(#Window_0, 434, 225, 193, 174,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "New window ( 0 )")
    If CreateGadgetList(WindowID())
      StringGadget(#String_0, 43, 28, 112, 21, "")
      TextGadget(#Text_0, 41, 63, 118, 20, "Enter your Text above", #PB_Text_Center)
      
    EndIf
  EndIf


Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_EventGadget
    
    
    GadgetID = EventGadgetID()
    
    If GadgetID = #String_0
      x+1
      If x=22;or whatever
        MessageRequester("Max Reached", "Sorry 22 Chars Only", #MB_OK|#MB_ICONERROR)
        End
      EndIf
       
    EndIf
    
  EndIf
  
Until Event = #PB_EventCloseWindow

End
Chris
I looked up 'paranoid' in the dictionary this morning.
It said, 'what do you want to know that for?'
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

...,

Using API would be this :

Code: Select all

Enumeration 
  #Window_0 
EndEnumeration 

Enumeration 
  #String_0 
  #Text_0 
EndEnumeration 

  If OpenWindow(#Window_0, 434, 225, 193, 174,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "New window ( 0 )") 
    If CreateGadgetList(WindowID()) 
      hStringGadget = StringGadget(#String_0, 43, 28, 112, 21, "") 
      TextGadget(#Text_0, 41, 63, 118, 20, "Enter your Text above", #PB_Text_Center)
      SendMessage_(hStringGadget, #EM_SETLIMITTEXT, 22, 0) ; adjust to the limit you want to have
      Debug SendMessage_(hStringGadget, #EM_GETLIMITTEXT, 0, 0) ; Check if the control has got this change, yuo can remove this line
    EndIf 
  EndIf 
  Quit = #FALSE
  Repeat 
    Select WaitWindowEvent()
      Case #PB_EventCloseWindow
        Quit = #TRUE
    EndSelect
  Until Quit
End 
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
xstreur
New User
New User
Posts: 7
Joined: Wed May 05, 2004 6:16 pm
Location: The Netherlands

Thanx

Post by xstreur »

Thanx FWeil,

This works fine for me. The best thing is that it can simply be integratedin the coding I already had.

Spasiba bolchoi!!!! :D :!:
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

вы радушны xstreur
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

@fweil

Thanks for this.

However, I can't seem to get the styles to be accepted. Could you expand your example to show how to assign one of the styles mentioned in your post.

TIA
Terry
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Well, here is a more complete code with comments from documentation inside.

Hope this answers well to all ...

Code: Select all

Enumeration 
  #Window_0 
EndEnumeration 

Enumeration 
  #String_0 
  #Text_0 
EndEnumeration 

  If OpenWindow(#Window_0, 434, 225, 193, 174,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "New window ( 0 )") 
    If CreateGadgetList(WindowID()) 

      ;
      ; Possible attributes for the string gadget are (from PureBasic's documentation) :
      ;
      ; #PB_String_Numeric : Only numbers are accepted. 
      ; #PB_String_Multiline : Several lines of text are accepted. 
      ; #PB_String_Password : Password mode, displaying only '*' instead of normal characters. 
      ; #PB_String_ReadOnly : Read only mode. No text can be entered. 
      ; #PB_String_LowerCase : All characters are converted To lower Case automatically. 
      ; #PB_String_UpperCase : All characters are converted To upper Case automatically. 
      ; #PB_String_BorderLess : No borders are drawn around the gadget. 
      ;
      ; But also you have the possibility to change styles using Windows API. After gadget's creation, you would
      ; have to use SetWindowLong_() to accumulate gadget's styles with optional styles you would get.
      ;
      ; It's a chance that StringGadget() PureBasic's command can accept API attributes in the attributes argument.
      ;
      ; The attributes list in the API for Edit Controls is the following :
      ;
      ;
      ; #ES_AUTOHSCROLL Automatically scrolls text To the right by 10 characters when the user types a character at the End of the line. When the user presses the ENTER key, the control scrolls all text back To position zero.
      ; #ES_AUTOVSCROLL Automatically scrolls text up one page when the user presses the ENTER key on the last line.
      ; #ES_CENTER Centers text in a multiline edit control.
      ; #ES_LEFT Left-aligns text.
      ; #ES_LOWERCASE Converts all characters To lowercase as they are typed into the edit control.
      ; #ES_MULTILINE Designates a multiline edit control. The Default is single-line edit control.
      ; When the multiline edit control is in a dialog box, the Default response To pressing the ENTER key is To activate the Default button. To use the ENTER key as a carriage return, use the ES_WANTRETURN style.
      ; When the multiline edit control is not in a dialog box And the ES_AUTOVSCROLL style is specified, the edit control shows as many lines as possible And scrolls vertically when the user presses the ENTER key. If you do not specify ES_AUTOVSCROLL, the edit control shows as many lines as possible And beeps If the user presses the ENTER key when no more lines can be displayed.
      ; If you specify the ES_AUTOHSCROLL style, the multiline edit control automatically scrolls horizontally when the caret goes past the right edge of the control. To start a new line, the user must press the ENTER key. If you do not specify ES_AUTOHSCROLL, the control automatically wraps words To the beginning of the Next line when necessary. A new line is also started If the user presses the ENTER key. The window size determines the position of the word wrap. If the window size changes, the word wrapping position changes And the text is redisplayed.
      ; Multiline edit controls can have scroll bars. An edit control with scroll bars processes its own scroll bar messages. Note that edit controls without scroll bars scroll as described in the previous paragraphs And process any scroll messages sent by the parent window.
      ; #ES_NOHIDESEL Negates the Default behavior For an edit control. The Default behavior hides the selection when the control loses the input focus And inverts the selection when the control receives the input focus. If you specify ES_NOHIDESEL, the selected text is inverted, even If the control does not have the focus.
      ; #ES_NUMBER Windows 95 only: Allows only digits To be entered into the edit control.
      ; #ES_OEMCONVERT Converts text entered in the edit control. The text is converted from the Windows character set To the OEM character set And then back To the Windows set. This ensures proper character conversion when the application calls the CharToOem function To convert a Windows string in the edit control To OEM characters. This style is most useful For edit controls that contain filenames.
      ; #ES_PASSWORD Displays an asterisk (*) For each character typed into the edit control. You can use the EM_SETPASSWORDCHAR message To change the character that is displayed.
      ; #ES_READONLY Prevents the user from typing Or editing text in the edit control.
      ; #ES_RIGHT Right-aligns text in a multiline edit control.
      ; #ES_UPPERCASE Converts all characters To uppercase as they are typed into the edit control.
      ; #ES_WANTRETURN Specifies that a carriage Return be inserted when the user presses the ENTER key While entering text into a multiline edit control in a dialog box. If you do not specify this style, pressing the ENTER key has the same effect as pressing the dialog box's default push button. This style has no effect on a single-line edit control.
      ;
      ; So you can use combined attributes like this :
      ;
      Attributes.l = #PB_String_UpperCase | #ES_RIGHT
      hStringGadget = StringGadget(#String_0, 43, 28, 112, 21, "", Attributes)
      TextGadget(#Text_0, 41, 63, 118, 20, "Enter your Text above", #PB_Text_Center) 
      SendMessage_(hStringGadget, #EM_SETLIMITTEXT, 22, 0) ; adjust to the limit you want to have 
      Debug SendMessage_(hStringGadget, #EM_GETLIMITTEXT, 0, 0) ; Check if the control has got this change, yuo can remove this line 
    EndIf 
  EndIf 
  Quit = #FALSE 
  Repeat 
    Select WaitWindowEvent() 
      Case #PB_EventCloseWindow 
        Quit = #TRUE 
    EndSelect 
  Until Quit 
End 
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

@fweil

Thanks... I had not tried using the #ES_??? in combination with the PB flags.

I was trying to use an API like this that worked:

Code: Select all

SetWindowLong_(hStringGadget, #GWL_STYLE, GetWindowLong_(hStringGadget, #GWL_STYLE)|#ES_NUMBER)
But I can't add another style like that and get it to work.

Eg. this failed.

Code: Select all

SetWindowLong_(hStringGadget, #GWL_STYLE, GetWindowLong_(hStringGadget, #GWL_STYLE)|#ES_NUMBER|#ES_RIGHT)
What I am trying to get to is a right justified entry box that allows only numbers plus the period, +, -, *, and / keys.

The #ES_NUMBER and #PB_String_Numeric styles only allow 0123456789, not exactly what I need.

Thanks for your code tips and references.
Terry
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Well,

The usual way to change style I know and use is :

NewStyle.l = GetWindowLong_(GadgetID(YourGadget), #GWL_STYLE)
NewStyle = NewStyle | MoreAttributes
NewStyle = NewStyle | MoreAttributes
SetWindowLong_(GadgetID(YourGadget), #GWL_STYLE, NewStyle)

Your code is different but looks like it should work ...
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

El_Choni
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

El_Choni you have better memory than I have...

:D
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Maybe you can play with this to suit your needs...

Code: Select all

Enumeration 
  #Window_0 
EndEnumeration 

Enumeration 
  #String_0 
  #Text_0 
EndEnumeration 

If OpenWindow(#Window_0, 434, 225, 193, 174,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "New window ( 0 )") 
  If CreateGadgetList(WindowID())
    Attributes.l = #ES_RIGHT 
    hStringGadget = StringGadget(#String_0, 43, 28, 112, 21, "", Attributes) 
    TextGadget(#Text_0, 41, 63, 118, 20, "Enter your Text above", #PB_Text_Center) 
    SendMessage_(hStringGadget, #EM_SETLIMITTEXT, 22, 0) ; adjust to the limit you want to have 
  EndIf 
EndIf 

Quit = #False
valid_chars$ = "0123456789.+-/" 
Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_EventGadget
      Select EventGadgetID()
        Case #String_0
          validate_it$ = GetGadgetText(#String_0)
          For i = 1 To Len(valid_chars$)
            ; see if all characters in StringGadget are valid
            keepit = FindString(valid_chars$, Mid(validate_it$, i, 1), 1)
            If keepit = 0
              ; get current caret position
              SendMessage_(hStringGadget, #EM_GETSEL, @cPos, 0)
              ; remove invalid character
              newstring$ = RemoveString(validate_it$, Mid(validate_it$, i, 1),1)
              SetGadgetText(#String_0, newstring$)
              ;set caret position to last position after setting newstring$
              SendMessage_(hStringGadget, #EM_SETSEL, cPos-1, cPos-1)
            EndIf
            
          Next
        EndSelect
    Case #PB_EventCloseWindow 
      Quit = #True 
  EndSelect 
Until Quit 
End 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Post Reply