0

How do I make it so that txtTitleofWebpage.Text is in italics when copied to the Clipboard so that the user can copy the references into a Microsoft Word document? I'm trying to make a Windows Form Application using C# to generate Harvard References from information the user inputs into a form.

Here is the code:

Clipboard.SetText(wholeName + ", (" + yOPDate + ") " + txtTitleofWebpage.Text + 
                  " [online]. Available from: " + txtURLWeb.Text + " [Accessed: " + 
                  accessDateWeb.Value.ToShortDateString() + "].", TextDataFormat.Rtf);
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
SomeRandomDev
  • 129
  • 11
  • 2
    Well, not that way. You need to use a format that can specify typographic styles and is compatible with whatever app you want to paste into. HTML and Rich text (RTF) are common choices. Google "c# copy html to clipboard", the top hits are all existing SO questions. – Hans Passant Feb 03 '17 at 00:20
  • @HansPassant - So, I can set TextDataFormat to HTML. Though, I find if I add an italics opening and closing tag around txtTitleofWebpages.Text I get errors. – SomeRandomDev Feb 03 '17 at 00:26
  • 1
    add @ in front of the opening and closing tags, this will ignore escape characters which they might be seen as. Like: `@"This \n will not make a newline"` – EpicKip Feb 03 '17 at 10:03
  • @EpicKip So, like this...? `Clipboard.SetText(@ wholeName + ", (" + yOPDate + ") " + @ txtTitleofWebpage.Text @ + " [online]. Available from: " + txtURLWeb.Text + " [Accessed: " + accessDateWeb.Value.ToShortDateString() + "]." @, TextDataFormat.Html);` – SomeRandomDev Feb 03 '17 at 10:48
  • @HansPassant - The text will be pasted into a Word Document. – SomeRandomDev Feb 03 '17 at 10:50
  • @EpicKip - The text will be pasted into a Word Document. – SomeRandomDev Feb 03 '17 at 10:50
  • @jamesfromit is not in quotes, it should be because its not "text" until you put it in quotes. And @ only works in front of a " to escape special characters from that string. – EpicKip Feb 03 '17 at 10:51
  • @jamesfromit Ok, I can easily help you if you just provide me an example string (which is not working) instead of string names. I can't detect the problem without their value. – EpicKip Feb 03 '17 at 10:55
  • @EpicKip - Nothing seems to be copying to the Clipboard. Here is the code: `Clipboard.SetText(@"" + wholeName + ", (" + yOPDate + ") " + @"" + txtTitleofWebpage.Text + @"" + " [online]. Available from: " + txtURLWeb.Text + " [Accessed: " + accessDateWeb.Value.ToShortDateString() + "]. ", TextDataFormat.Html);` – SomeRandomDev Feb 03 '17 at 10:55
  • Ok this time change the variables (txtURLWeb) to some test data so I can use it. I cant do anything if you don't provide me the values. Its going wrong around there haha. Does it even give an error? – EpicKip Feb 03 '17 at 10:57
  • @EpicKip - This is what it's supposed to be formatted as: Nightingale, J, (13/01/2017) _Title of Webpage in Italics_ [online]. Available from: http://www.google.co.uk/ [Accessed: 03/02/2017]. – SomeRandomDev Feb 03 '17 at 10:59
  • @EpicKip - No, it doesn't give any errors. – SomeRandomDev Feb 03 '17 at 10:59
  • @jamesfromit It doesn't copy because of TextDataFormat.Html, I can tell you that much. I'm looking for a way to copy the formatting along. Hold on a sec :) – EpicKip Feb 03 '17 at 11:01
  • @EpicKip - Okay, thank you. I have plenty of time to work on this. :) – SomeRandomDev Feb 03 '17 at 11:05
  • @jamesfromit My break is starting now, I left 2 links that might fix your problem. I'll try and find the answer later if I have time. – EpicKip Feb 03 '17 at 11:28
  • @EpicKip - Thank you! :) – SomeRandomDev Feb 03 '17 at 11:41
  • @jamesfromit Edited my answer with a code sample and some things to try. If its working please let me know :) – EpicKip Feb 03 '17 at 12:14
  • @jamesfromit Check my last edit for RTF – EpicKip Feb 03 '17 at 13:47
  • @jamesfromit If my answer fixed it/helped you the best could you accept it? – EpicKip Feb 03 '17 at 15:17
  • @EpicKip - Yeah, I will totally accept it. I just haven't had the time to try it yet. College is getting in the way. :) – SomeRandomDev Feb 03 '17 at 16:29
  • @jamesfromit I can test it anymore today as my workday is done and I wont be at a computer tonight so I hope it works – EpicKip Feb 03 '17 at 16:54

1 Answers1

1

It seems you have to insert a 'header' in the html string, I found 2 examples for this:

This works with Word:
Example code:

    Clipboard.SetText(@"Version:1.0
                        StartHTML:000125
                        EndHTML:000260
                        StartFragment:000209
                        EndFragment:000222
                        <HTML>
                        <head>
                        <title>HTML clipboard</title>
                        </head>
                        <body>
                        <!–StartFragment–><b>Hello!</b><!–EndFragment–>
                        </body>
                        </html>", 
                        TextDataFormat.Html);

This copies a Hello! to your clipboard, you have to change the fragments based on the size I think so I don't know exactly how that would work with a dynamic string but I hope this will get you started. 666

If you can also use RTF

Clipboard.SetText(@"{\rtf1\ansi This is in \i\f0\fs17 italic\i0.}",
                    TextDataFormat.Rtf);

Example with string

var q = "test123";
Clipboard.SetText(@"{\rtf1\ansi This is in \i\f0\fs17 " + q + @"\i0.}",
                    TextDataFormat.Rtf);

or

var q = "test123";
Clipboard.SetText( $@"{\rtf1\ansi This is in \i\f0\fs17 {q}\i0.}",
                    TextDataFormat.Rtf);

Note the @ before the 2nd part of the string, if you need to escape certain characters (you will need that with RTF) add @ in front of each opening ".

This seems to be a lot easier because you don't have to insert the header but the formatting itself is abit more complicated imo.

EpicKip
  • 4,015
  • 1
  • 20
  • 37