Solved

RichTextBox Properties

Posted on 2015-01-12
14
85 Views
Last Modified: 2015-01-12
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
0
Comment
Question by:hakanogu
  • 8
  • 5
14 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40544943
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
 

Author Comment

by:hakanogu
ID: 40545025
Hello martin,
İt is asking SendMessage function and txtEdit variable for your code.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40545047
Change txtEdit to the name of your richtextbox.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:hakanogu
ID: 40545061
I am taking warning forSendMessage as "sub or function is not defined"
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40545067
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
 

Author Comment

by:hakanogu
ID: 40545085
ı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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40545218
Please attach a zip file of your project.
0
 

Author Comment

by:hakanogu
ID: 40545248
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
 
LVL 45

Expert Comment

by:aikimark
ID: 40545292
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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40545293
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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40545303
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
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40545313
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
 

Author Comment

by:hakanogu
ID: 40545316
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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40545332
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Determine Range to Select 5 53
Excel - Save a copy of work book 13 89
Add a task in Outlook from access 11 42
VBA: loop recent folder and copy txt file. 8 36
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question