RichTextBox Properties

Hi all,
Is there any way to block the user create a new line in RichTextBox?  I want user edit the texts inside but he will not be able to crete a new line
and how I block the user entering any another language characters except english letters in richtextbox?
Thank you
hakanoguAsked:
Who is Participating?
 
Martin LissOlder than dirtCommented:
Here's a zip file with a bunch of things commented out so that at least the RTB can be accessed.
Q-28595021.zip
0
 
Martin LissOlder than dirtCommented:
For the first part of your question, do this In the change event for your richtextbox

Private Sub MyRTB_Change()
Const EM_GETLINECOUNT As Long = &HBA


Dim linecount As Long
linecount = SendMessage(txtEdit.hWnd, _
                  EM_GETLINECOUNT, 0, 0)
If linecount >= 4 Then ' allow 3 lines
    Exit Sub
End If
End Sub

Open in new window

0
 
hakanoguAuthor Commented:
Hello martin,
İt is asking SendMessage function and txtEdit variable for your code.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Martin LissOlder than dirtCommented:
Change txtEdit to the name of your richtextbox.
0
 
hakanoguAuthor Commented:
I am taking warning forSendMessage as "sub or function is not defined"
0
 
Martin LissOlder than dirtCommented:
At the top of your form's code put

Private Declare Function SendMessage Lib "user32" Alias _
 "SendMessageW"(ByVal hwnd As Long, ByVal wMsg As Long, _
 ByVal wParam As Long, lParam As Any) As Long

Open in new window

0
 
hakanoguAuthor Commented:
ın general declerations
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub RichTextBox4_Change()

Const EM_GETLINECOUNT As Long = &HBA


Dim linecount As Long
linecount = SendMessage(RichTextBox4.hwnd, EM_GETLINECOUNT, 0, 0)
If linecount >= 4 Then ' allow 3 lines
    Exit Sub
End If
End Sub

with this codes ı am still be able to add some lines Martin.
0
 
Martin LissOlder than dirtCommented:
Please attach a zip file of your project.
0
 
hakanoguAuthor Commented:
Maybe You will not be able to run my project because of my INFITF application but my richbox inputs always changing according to my tree view.
V7.zip
0
 
aikimarkCommented:
I think you need to add some code to the Richtext KeyDown or KeyPress events.  Look for the enter key, with or without either the shift or Ctrl key being pressed.  Set the value of the parameter =0 to prevent the character from entering the control.

If you need to prevent a copy/paste of a new line/enter key, you will need to add code to the richtext control's change event.
0
 
Martin LissOlder than dirtCommented:
Move the code to the KeyDown event and add line 10 and optionally 11.
Private Sub RichTextBox4_KeyDown(KeyCode As Integer, Shift As Integer)
Const EM_GETLINECOUNT As Long = &HBA


Dim linecount As Long

linecount = SendMessage(RichTextBox4.hwnd, EM_GETLINECOUNT, 0, 0)
'MsgBox linecount
If linecount >= 30 Then
    KeyCode = 0
    Beep
    Exit Sub
End If

End Sub

Open in new window

0
 
Martin LissOlder than dirtCommented:
If you need to prevent a copy/paste of a new line/enter key, you will need to add code to the richtext control's change event.
@aikimark: I tested that and it's not necessary.

@hakanogu: Just a note to let you know that when you do this

Public oi, orow, ocl, i, J, k, l, m, n, p, r, s, t, u, uu, a, B, c, d, e, f As Integer

only f becomes an Integer and all the rest are Variants.
0
 
hakanoguAuthor Commented:
Actually just İ did with this code:

Private Sub RichTextBox4_KeyPress(KeyAscii As Integer)

         If KeyAscii = 13 Then  ' The ENTER key.
               ' Set the focus to the next control.
            KeyAscii = 0        ' Ignore this key.
         End If
End Sub
0
 
Martin LissOlder than dirtCommented:
I was wrong when I said in response to aikimark that code in the Change event wasn't necessary. If there are already the maximum number of lines then you can't paste any more, but if there are less that that you can so keep my change event code along with the other code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.