Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have multiline textbox now i copy the value from excel cell and paste to multi line textbox.Content is pasted but also paste the empty spaces also.so the content go to upside.so textbox is view like no contents are not available in textbox.

Double click the cell and copy the content and paste it paste correctly.but the whole cell is copied the spaces not trimmed(this is for user friendly purpose).

What I have tried:

I have used textbox1.text.trim() method but this is not working.
Posted
Updated 27-Oct-16 22:34pm
v2

You don't\can't trim the sapces when the user copy and pastes, what you do instead is trim the spaces when the form is submitted and you are reading the data.

C#
string data = TextboxMyData.Text.Trim();
 
Share this answer
 
(not a solution, but a comment - comments do not seem to work atm)

Trim() method will only trim regular spaces; if there is anything else at the beginning of the pasted string, it will not be trimmed. So, you will have to launch your program in debug mode, and find out of what exactly is composed pasted string. Then act on this string accordingly, or let us know if you still are stuck. Kindly :)
 
Share this answer
 
If you mean you want trim to remove blank lines, then no, Trim will not do that - it only removes whitespace at the ends of strings, not from the middle, and the Text property of a multiline TextBox returns a single string, which each line delimited by "\r\n" - so blank lines aren't seen by Trim.
Instead, try this:
C#
string text = Clipboard.GetText();
string[] parts = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
tbOutData.Text = string.Join("\r\n", parts.Select(p => p.Trim()).Where(w => !string.IsNullOrWhiteSpace(w)));
 
Share this answer
 

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