8

I need to limit the number of characters to be pasted in a multiline textbox.

Let's say this is my string to be pasted in the textbox:

Good day Ladies and Gents!
I just want to know

If this is possible, please help.

The rule is maximum characters PER LINE is 10, Maximum ROWS is 2. Applying the rule, pasted text should only be like this:

Good day L
I just wan

2
  • do you want to allow user to use short keys Ctrl + V? Commented Jul 19, 2013 at 3:33
  • yes, any form of paste (CTRL + V, right-click+paste, SHIFT + INSERT) is allowed. Commented Jul 19, 2013 at 3:58

5 Answers 5

6

There is not automatic what to do this. You'll need to handle the TextChanged event on the text box and manually parse the changed text to limit it to the format required.

private const int MaxCharsPerRow = 10; private const int MaxLines = 2; private void textBox1_TextChanged(object sender, EventArgs e) { string[] lines = textBox1.Lines; var newLines = new List<string>(); for (int i = 0; i < lines.Length && i < MaxLines; i++) { newLines.Add(lines[i].Substring(0, Math.Min(lines[i].Length, MaxCharsPerRow))); } textBox1.Lines = newLines.ToArray(); } 
Sign up to request clarification or add additional context in comments.

Comments

3

You can catch the message WM_PASTE (sent to your TextBox) to process yourself:

public class MyTextBox : TextBox { int maxLine = 2; int maxChars = 10; protected override void WndProc(ref Message m) { if (m.Msg == 0x302)//WM_PASTE { string s = Clipboard.GetText(); string[] lines = s.Split('\n'); s = ""; int i = 0; foreach (string line in lines) { s += (line.Length > maxChars ? line.Substring(0, maxChars) : line) + "\r\n"; if (++i == maxLine) break; } if(i > 0) SelectedText = s.Substring(0,s.Length - 2);//strip off the last \r\n return; } base.WndProc(ref m); } } 

2 Comments

Thank you for this but changing control(or textbox) will be much of a pain to me...
@JackFrost not really. Just add a separate class in your project. You can reuse this class. This is the cleanest solution.
1

You can achieve this by the following way. Set maximum length of the text box as 22

textBox1.MaxLength = 22; 

In the text change event do the following

private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox1.Text.Length == 10) { textBox1.AppendText("\r\n"); } } 

This will automatically enters to next line after 10 characters

2 Comments

Thank you for your feedback but you see, I already have a restriction regarding that matter (when user is trying to input normally like NOT pasting). My problem now is how to capture the paste...
@Nitesh - you solution doesn't work in the case of pasting because when pasting the text's length goes from 0 immediately to the size of the of the pasted it text. It is never exactly 10 so the adding of the new line never occurs. Nor does it work if the user types in 2 character, hits enter, then types 2 characters and hits enter.
0

Why dont you work on the clipboard data to achieve this. Below is a small example .

String clipboardText = Clipbard.GetText( ); // MAXPASTELENGTH - max length allowed by your program if(clipboardText.Length > MAXPASTELENGTH) { Clipboard.Clear(); String newClipboardText = clipboardText.Substring(0, MAXPASTELENGTH); // set the new clipboard data to the max length SetData(DataFormats.Text, (Object)newClipboardText ); } 

Now paste the data whereever you like , the data shall be trimmed to the maximum length allowed by your program .

Comments

-2
 private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox1.Text.Length == 10) { textBox1.MaxLength = 10; //MessageBox.Show("maksimal 10 karakter"); } } 

1 Comment

How does this help better than the other solutions already given?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.