1

I have a single line textbox, when I copy some text from lets say notepad that is on multiple lines and paste them in to my text box, only the first line of text appears (thats obvious) but how can I change this so that the lines are joined automatically upon pasting them and separated by a space. I see that I would need to modify the textbox_changed event but this would affect everything that goes on in that textbox not only the paste event. Could you provide me with some code to handle a paste event and ignore all other events.. thanks :)

winforms
mouse paste event
paparazzo
  • 44,497
  • 23
  • 105
  • 176
ace007
  • 577
  • 1
  • 10
  • 20

2 Answers2

3

is this what you're looking for?

Clipboard Events A Textbox in C# has a number of useful events to indicate when certain actions have been taken. For example, .NET textboxes have an event to indicate when the text has changed or when the user has pressed a key. These events allow C# developers to write clean code that interacts with textboxes. Following the same principles, we can manually implement events that are triggered by clipboard actions, i.e. text is cut, copied, or pasted in the textbox. The .NET Framework does not come with these events, but they are not difficult to implement. 7/5/11 Update: Added support to suppress copy, cut, and paste events. Custom Textbox To implement custom events, we are going to have to create our own textbox user control. The user control will inherit the Textbox class since we want all the default behaviors of a .NET textbox. Creating a custom user control will also let us override the WndProc function, which processes messages passed to the control. By overriding the function, we can detect messages such as when text is cut, copied, or pasted, before allowing the control to process them.

Beth
  • 9,531
  • 1
  • 24
  • 43
  • would be nice if they gave a real example, seems like a guessing game where i'm supposed to put this code – ace007 Nov 15 '12 at 22:11
  • you're making a custom user control instead of using the Windows textbox control. You put the code in the new user control. – Beth Nov 15 '12 at 22:21
0

if you are using .asp webforms you need to change the text mode in your textbox

to SOMETHING LIKE THIS TextMode="MultiLine" Columns="50" Rows="5"

in Winforms

 textBox1.Multiline = true;
BenMorel
  • 34,448
  • 50
  • 182
  • 322
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • you answered a question that the OP wasn't asking – Sam I am says Reinstate Monica Nov 15 '12 at 21:15
  • 2
    "how can I change this so that the lines are joined automatically upon pasting them" – Sam I am says Reinstate Monica Nov 15 '12 at 21:16
  • and why do you thik if he is using a textbox which changes to textarea is wrong – COLD TOLD Nov 15 '12 at 21:17
  • 1
    Because it doesn't `automatically upon pasting them and separated by a space`. at least not without extra work – Sam I am says Reinstate Monica Nov 15 '12 at 21:18
  • 1
    He was clearly asking how to paste multiline text into a single line textbox... I don't think an option is for him to change his single line textbox. – Dave Zych Nov 15 '12 at 21:19
  • He is asking how can he copy some stuff from the let say notepad to the textbox so he is not loosing lines typically in a single line textbox you lose one line – COLD TOLD Nov 15 '12 at 21:21
  • Sorry for late reply, it's a winforms application and yes I will be using the string.join method once i detect the mouse paste event – ace007 Nov 15 '12 at 21:22
  • @ace007 if all you need is to wrap line just like in notepad why not use muliline – COLD TOLD Nov 15 '12 at 21:25
  • @COLDTOLD i dont know how to explain this but that's not an option it simply wont work in my application. I have changed my textbox to a multiline but still I need to detect the mouse paste event so that I can join the lines in to a single line separated by a space. onjly this way I can process the text to my needs. – ace007 Nov 15 '12 at 21:32
  • @ace007 take a look at this http://stackoverflow.com/questions/3446233/hook-on-default-paste-event-of-winforms-textbox-control – COLD TOLD Nov 15 '12 at 21:39
  • @COLDTOLD his code makes no sense to me, he uses private void Main() yet that's not a winforms load event and its not invoked when the form is loaded looks almost like a console app load event. Also when I change it to a form_load event I have an error saying the "sender" variable cant be used because it will give a different meaning to another sender object thats in the forms load event.. so where am I supposed to paste the code he has given :/ – ace007 Nov 15 '12 at 22:05
  • @COLDTOLD Ok i figured out how to use his code, apart from it destroys my form by overwriting it and creating its own new form: "var form = new Form();" – ace007 Nov 15 '12 at 22:27