Solved

RichTextBox Properties

Posted on 2015-01-12
14
81 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 45

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 45

Expert Comment

by:Martin Liss
ID: 40545047
Change txtEdit to the name of your richtextbox.
0
 

Author Comment

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

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 45

Expert Comment

by:Martin Liss
ID: 40545218
Please attach a zip file of your project.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 45

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 45

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 45

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 45

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now