Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using a multiline textbox to store input.

Its size varies according to different systems.

So size in not fixed.

Since size is not fixed if a person inputs a text and the lines exceeds 3 lines I need to
display a messagebox showing 3 lines exceeded.

How to do this.

Thanks in advance
Posted
Comments
ArunRajendra 14-Jul-14 5:03am    
The best way restrict is using character count and line count. Unfortunately you cant get the line count.
KUMAR619 14-Jul-14 5:05am    
How can I count the maximum characters available to be entered in a single line based on the text box size
ArunRajendra 14-Jul-14 5:11am    
You cant count the no of character in line. You need to count no of characters in the textbox. You can do this by using javaScript / jquery.
KUMAR619 14-Jul-14 5:13am    
can you send me the sample code sir
Thanks7872 14-Jul-14 5:20am    
Why to restrict user based on line numbers? This makes no sense. You should look for the no. of characters. Forget about the lines.

The problem is that "lines" is a nebulous concept when you use a multiline textbox: because what the text box thinks of as a "line" is delimited by a '\n' character entered by the user using the ENTER key - but as far as the display goes it includes word wrap, so a single line of text may display over a dozen "lines" in the running application.

If you want to restrict the user input to a number of "lines" regardless, then you will need to use the Graphics.MeasureString[^] method in conjunction with the text entered and the TextBox.Font property to determine the display size of the actual text.

This will not be as simple as it sounds: you will need to work with the line characters the user has entered, as well as the size of the textbox, and remember to Dispose the graphics context you create each time or you will run out of memory extremely quickly!
 
Share this answer
 
Comments
KUMAR619 14-Jul-14 5:44am    
Is there any way to count the max characters a line in multiline textbox.
If I count these Lines I can automatically break lines and applend \n\r to differentiate them as line and make my required manipulation
KUMAR619 14-Jul-14 5:45am    
Sir, Is there any other way to display message box when word wrap happens. So that I can break lines pragmatically.
KUMAR619 14-Jul-14 7:17am    
Sir please share me some idea
With a quick bit off googling, I have come accross this question which seems to be similar to what you are asking:

http://stackoverflow.com/questions/1048425/limit-number-of-lines-in-net-textbox[^]

For your reference:

This is what I googled "windows forms multiline textbox limit number of lines" as you have not stated what type of application this is I have assumed it is a Windows Form application.
 
Share this answer
 
Comments
KUMAR619 14-Jul-14 7:49am    
You are right sir. But if we didn't pressed enter key all the text in the texbox will be considered as one string. Then how can we restrict lines. Thats why that link is not usable sir. Please help me sir
Pheonyx 14-Jul-14 9:12am    
Clearly you didn't read the whole page but just looked at the first answer, the second answer covers that point.
KUMAR619 14-Jul-14 9:39am    
Thanks but I couldn't convert the following code to c#.

Private Declare Function SendMessageINT Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900