Sign in to follow this  
Followers 0
qwert

How to limit Rich Text without scroll bar?

3 posts in this topic

#1 ·  Posted (edited)

I need to limit the number of characters that can be typed or pasted into a basic richedit control.  The particular fields I'm working with are simple boxes that can accept a couple of lines of rich text.

I've found very few examples of limits, but there seems to be an impression that the limit statement requires the control to have scroll bars defined.  The help file does not say that.  But my fields cannot have them, so I'll need some other method if the limitation is real.

Here's a script that demonstrates the situation:

;
;       Rich Text Character Limit Test
;
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiRichEdit.au3>

$hGUI = GUICreate("Rich Window", 400, 200, 600, 300)
$hRichEdit = _GUICtrlRichEdit_Create($hGUI, "", 10, 10, 380, 120, $ES_MULTILINE)
_GUICtrlRichEdit_SetSel($hRichEdit, 0, -1)                          ; select all
_GUICtrlRichEdit_SetFont($hRichEdit, 14, "Arial")                   ; set font
_GUICtrlRichEdit_SetSel($hRichEdit, 0, 0)                           ; select none
$err = _GUICtrlRichEdit_SetLimitOnText($hRichEdit, 100)         ; set a practical limit ... DOES NOT WORK
If $err = False Then MsgBox(0, "error", @error, 0, $hGUI)
$list = GUICtrlCreateButton("Check Character Count", 100, 150, 200, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    Case $list
        MsgBox(0, "Current Count", _GUICtrlRichEdit_GetTextLength($hRichEdit))
    EndSwitch
WEnd

Thanks in advance for any help.

57134299965e0_RichLimit.PNG.acfeafd7467c

 

 

 

Edited by qwert

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The limit you set is ignored/corrected to 64k:

; #FUNCTION# ====================================================================================================================
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; ===============================================================================================================================
Func _GUICtrlRichEdit_SetLimitOnText($hWnd, $iNewLimit)
    If Not _WinAPI_IsClassName($hWnd, $__g_sRTFClassName) Then Return SetError(101, 0, False)
    If Not __GCR_IsNumeric($iNewLimit, ">=0") Then Return SetError(102, 0, False)

    If $iNewLimit < 65535 Then $iNewLimit = 0 ; default max is 64K
    _SendMessage($hWnd, $EM_EXLIMITTEXT, 0, $iNewLimit)
    Return True
EndFunc   ;==>_GUICtrlRichEdit_SetLimitOnText

so test this script:

;
;       Rich Text Character Limit Test
;
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiRichEdit.au3>

$hGUI = GUICreate("Rich Window", 400, 200, 600, 300)
$hRichEdit = _GUICtrlRichEdit_Create($hGUI, "", 10, 10, 380, 120, $ES_MULTILINE)
_GUICtrlRichEdit_SetSel($hRichEdit, 0, -1)                          ; select all
_GUICtrlRichEdit_SetFont($hRichEdit, 14, "Arial")                   ; set font
_GUICtrlRichEdit_SetSel($hRichEdit, 0, 0)                           ; select none
$err = _GUICtrlRichEdit_SetRealLimitOnText($hRichEdit, 100)         ; set a practical limit ... DOES NOT WORK
If $err = False Then MsgBox(0, "error", @error, 0, $hGUI)
$list = GUICtrlCreateButton("Check Character Count", 100, 150, 200, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    Case $list
        MsgBox(0, "Current Count", _GUICtrlRichEdit_GetTextLength($hRichEdit,True,True))
    EndSwitch
WEnd

; #FUNCTION# ====================================================================================================================
; Authors........: Chris Haslam (c.haslam)
; Modified ......autoBert
; ===============================================================================================================================
Func _GUICtrlRichEdit_SetRealLimitOnText($hWnd, $iNewLimit)
    If Not _WinAPI_IsClassName($hWnd, $__g_sRTFClassName) Then Return SetError(101, 0, False)
    If Not __GCR_IsNumeric($iNewLimit, ">=0") Then Return SetError(102, 0, False)

    ;If $iNewLimit < 65535 Then $iNewLimit = 0 ; default max is 64K
    _SendMessage($hWnd, $EM_EXLIMITTEXT, 0, $iNewLimit)
    Return True
EndFunc   ;==>_GUICtrlRichEdit_SetLimitOnText

as you can see: when also using the second parameter of _GUICtrlRichEdit_GetTextLength the correct text length is returned.

Edited by AutoBert

Share this post


Link to post
Share on other sites

Your RealLimit function works.  I now have my fields working properly.

But I'm perplexed by that one statement in the standard RichEdit function:

If $iNewLimit < 65535 Then $iNewLimit = 0

What could possibly be the purpose when MSFT clearly supports limits?:

Quote

EM_EXLIMITTEXT

Specifies the maximum amount of text that can be entered. If this parameter is zero, the default maximum is used, which is 64K characters.

And why isn't this limitation mentioned in help?

Regardless, I do appreciate your help with this and think others will find it valuable.

Thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0