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.

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
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
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
Code: Select all
SetWindowLong_(hStringGadget, #GWL_STYLE, GetWindowLong_(hStringGadget, #GWL_STYLE)|#ES_NUMBER)
Code: Select all
SetWindowLong_(hStringGadget, #GWL_STYLE, GetWindowLong_(hStringGadget, #GWL_STYLE)|#ES_NUMBER|#ES_RIGHT)
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