Hi :( Please, help me, I can't find any information even in MSDN(
I'm creating MS Word file, by saving text from richTextBox.
I have some questions:
- How can I Add new Line to it? Now I'm creating new string array, and then - string[] a; RTB1.Lines = a;
- How can I add something from Clipboard (picture or text) from new string? Now When I'm add one string (string A for exmpl.), and then - another string (string B for exmpl.), than I get A+B. And I need : one Line - A, Next line - B.
Please, Help me... Thank you.
ArtemPosted Sep 3, 2007, 6:07 PM
At first, I have found that image deleting aftep pasting a new string (not after pasting next image), and I thought that it happens because image need more than one Line, and string pasting from new Line and delete the image..
And the next problem is, in my real application I need a little biger construction of file... Something like this:
- Some interesting text
- picture
- Some lines of interesting text
- And some interesting text again and again and again....
You see? the third string is like an array of lines (and I don't know the length of this array). Of course, I'll try to Paste this array not from the Clipboard, and I hope, it'll work, but not now, because need to go. However, thank you very much again!
AlanPosted Sep 3, 2007, 5:24 PM
OK, I've downloaded the .rar file, decompressed it and loaded it into VS and I now understand what the problem is. Even though you're pasting 10 images into the RTB from the clipboard only one is showing up!
However, I haven't been able to come up with a satisfactory solution to this :(
It seems that whenever you try to paste an image on a new line all the previous ones are deleted, which is bizarre to say the least! As you've probably discovered yourself, it does work if you just paste the image immediately after the text, including wrapping to the next line if there's insufficient space on the current line.
In fact by prefixing the string with "\r\n" and padding it out on the right with sufficient spaces, you can make use of the wrapping behaviour to get it to work in a fashion but it's not very satisfactory:
private
void button1_Click(object sender, EventArgs e){
DataObject data = new DataObject();
data.SetData("\r\n\r\nSome interesting Text" + new string(' ', 69));
data.SetData(DataFormats.Bitmap, pictureBox1.Image);
Clipboard.SetDataObject(data);
for (int i = 0; i < 10; i++)
{
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.SelectionLength = 0;
richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Text));
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.SelectionLength = 0;
richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
}
}
You'll notice that to make life easier I've placed both the text and the image on the clipboard using a DataObject so you can paste either one of them to the RTB.
ArtemPosted Sep 2, 2007, 1:50 AM
ArtemPosted Sep 1, 2007, 1:52 AM
No... You don't understand me... Of course, I use SetDataObject... I create a simple application, which shows my problem. I upload it on the Rapidshare.Com : http://rapidshare.com/files/52600831/WFA.rar.html ... Try it..
Thank you
AlanPosted Aug 31, 2007, 6:36 PM
When I tried this myself by calling the SaveFile() method on an RTB containing text and an image, it worked fine and both the text and image were visible when I reopened the RTF file in WordPad.
To place image data on the Clipboard you will, of course, need to call the SetDataObject() method rather than the SetText() method.
ArtemPosted Aug 31, 2007, 3:08 AM
Here is my steps:
- past from Clipboard Text1;
- past from Clipboard Picture1;
- past from Clipboard Text2;
- past from Clipboard Picture2
...
- past from Clipboard TextN;
- past from Clipboard PictureN.
Everything past from new Line... But after Saving, there is no Picture1, Picture2 ... Picture(N-1) .. Understand? Where is my fault?
for Pasting I use:
[code]
Clipboard.SetText(str);
richTextBox1.Text += "\r\n";
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.SelectionLength = 0;
richTextBox1.Paste();
Clipboard.Clear();
[/code]
ArtemPosted Aug 30, 2007, 7:41 PM
AlanPosted Aug 30, 2007, 7:01 PM
I was just looking at this again in the hope that something might occur to me and I noticed that you've got the code in the Paint eventhandler for pictureBox2, so every time it's repainted (which can be very often) the code will execute again.
I don't know whether this could be causing the problem, but I'd have thought you only wanted to create the bitamp once and assign it to the pictureBox2.Image property. So, I'd see if it makes any difference if you place the code somewhere else where it will only execute once.
ArtemPosted Aug 28, 2007, 7:51 PM
Mabye there is another way to draw Image (mabye without Bitmap)?
AlanPosted Aug 28, 2007, 11:15 AM
Yes, I've noticed that legibility can suffer when you draw text on a bitmap rather than directly on the form.
I think the only thing you can do is to experiment with the font. Opinions differ, of course, but I find that Arial and Courier are the easiest to read and Georgia also looks good on bitmaps.
ArtemPosted Aug 27, 2007, 10:14 PM
And one more question : When I was making Image in pictureBox (by Paint event), the picture was vey nice, but pictureBox1.Image was null. And I couldn't get Image from there.
Then I solve this problem by next way: at first, I was creatin bitmap, and draw on it, and then make pictureBox1.Image = my Bitmap and everything allright, BUT Text now looking not so good and hard to read.
This is my code:
[code]
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
Bitmap flag = new Bitmap(500, 260);
Graphics g = Graphics.FromImage(flag);
Painter pn = new Painter();
g = pn.DrawStudent(g, ..., ..., ..., tabControl1.Font);
pictureBox2.Image = (Image)flag;
draw = false;
}
[/code]
and "Painter.cs" use g.Drawstring ... Font I take from tabControl.... Without drawing on Bitmap everyting was OK, but now... It's not so good..
Can I solve this little problem?
AlanPosted Aug 27, 2007, 3:50 PM
To paste an image from the clipboard to a new line in the RTB, you could use this code:
richTextBox1.Text += "\r\n";
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.SelectionLength = 0;
richTextBox1.Paste();
This assumes that the image is in a pastable format.
To get an image onto the clipboard in the first place:
string
fileName = @"C:\imagefolder\someimage.bmp";Bitmap bmp = new Bitmap(fileName);
Clipboard.SetDataObject(bmp);
ArtemPosted Aug 26, 2007, 3:26 AM
Ok! Thank you! with text everything allright now. And now how to post Image from Clipboard (or not, it doesn't matter) to richTextBox from new line..???
AlanPosted Aug 25, 2007, 8:42 AM
To add a new line to a RichTextBox, you can do this:
string
nextLine1 = "Some more text";richTextBox1.Text += Environment.NewLine + nextLine1;
To add a further line equal to whatever text is currently in the Clipboard:
string
nextLine2 = Clipboard.GetText();richTextBox1.Text += Environment.NewLine + nextLine2;
So, you'll gather from this that to get something on a new line you precede it by Environment.NewLine which is the same as "\r\n" (a carriage return, line feed pair) on Windows operating systems and append it to the existing Text property.
To move the caret, say to the end of the existing text, you can do this:
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.SelectionLength = 0;
ArtemPosted Aug 24, 2007, 7:18 PM