-
Sep 10th, 2015, 07:00 AM
#1
Thread Starter
Lively Member
Set a limit of lines in a richtextbox
Good Day,
I am setting my richtextbox lines to a maximum number of 2 by determining the location of my cursor.
Code:
Private line As Integer
Code:
Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
Dim index As Integer = RichTextBox1.SelectionStart
line = RichTextBox1.GetLineFromCharIndex(index)
End Sub
And trapping my user not to enter anymore text on keypress
Code:
Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
If Char.IsLetterOrDigit(e.KeyChar) = True Then
If line = 2 Then
e.Handled = True
End If
End If
If e.KeyChar = Convert.ToChar(13) Then
If line = 2 Then
e.Handled = True
End If
End If
End Sub
My problem is when the user type a word or a text that is on the edge , that basically does not fit on the line, it will be moved on the nextline.
My question is how can i prevent the user to enter a text that will cause the word to moved on the nextline, that is when the cursor is on the last line of the richtextbox.
And the wordwrap must be valued as true.
Thanks.
-
Sep 10th, 2015, 09:29 AM
#2
Re: Set a limit of lines in a richtextbox
You should set the MaxLength property equal to the amount of characters allowed per line times the desired amount of maximum lines(in this case 2).
In order to do this, I would suggest using a monospaced font such as Consolas or Courier. Then once you've determined the font, you can get the width of a single character by using Graphics.MeasureString. Once you've the width of a single character, divide the width of the RichTextBox by the width of the single character to get the amount of characters allowed per line. Finally you multiply that amount by your desired maximum lines(2).
Here is an example, I would suggest using the SizeChanged event of the RichTextBox so that you get an accurate amount each time the size of the RichTextBox changes:
Code:
Private Sub MyRichTextBox_SizeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles MyRichTextBox.SizeChanged
'Get the width of a single character
Dim charWidth As Integer
Using g As Graphics = MyRichTextBox.CreateGraphics()
charWidth = g.MeasureString("A", MyRichTextBox.Font).Width
End Using
'Get the maximum amount of characters per line
Dim maxLineChar As Integer = MyRichTextBox.Width \ charWidth
'Set the MaxLength equal to the maxLineChar * maximum amount of lines
MyRichTextBox.MaxLength = maxLineChar * 2
End Sub
Last edited by dday9; Sep 10th, 2015 at 09:32 AM.
Reason: Changed division from double to long
-
Sep 10th, 2015, 09:40 AM
#3
Re: Set a limit of lines in a richtextbox
If you do not want to use a monospace font, which I agree, is a great solution. Then keep track of the keystrokes and when the line jumps to 3, do a backtrace and delete the last key pressed.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Sep 11th, 2015, 11:42 PM
#4
Re: Set a limit of lines in a richtextbox
Try this:
Code:
Public Class Form1
Private maxLines As Integer = 5
Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
Dim lines As New List(Of String)(RichTextBox1.Lines)
Dim caretPosition As Integer = RichTextBox1.SelectionStart
RichTextBox1.Lines = lines.Take(maxLines).ToArray
RichTextBox1.SelectionStart = If(caretPosition > RichTextBox1.Text.Length, RichTextBox1.Text.Length, caretPosition)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 12th, 2015, 01:30 AM
#5
Thread Starter
Lively Member
Re: Set a limit of lines in a richtextbox
Hi,
Thank you for all of your reply..
All the code are working fine, but if the user does not press the enter key and just continue to input a text and the user just put an space on the edge of the RTB the cursor must be on the next line.
Thanks.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|