Clipboard and C# How does this work ?
Hi friends !
I have created a code by use of an example which works, ...:D
But I dont know WHY
the code is
Code:
void wordLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
IDataObject data = Clipboard.GetDataObject();
_rtbDoc.Paste(); // RTB is my Richtextbox
....
In the worker thead I'm opening a word document selecting it by use of 'WholeStory' and copying the selection. So This I understand loads it into the clipboard. But in the above code the red italic line what does it do ? Because IDataobject is not used anywhere else in all of my code. But if I delete this line the paste method of my richtextbox stays empty.
Also I tried to use it just after reading the data in the 'do delegate' of the backgroundworker and having the paste() method in the Completed method. This also results in an empty RTB. So what does the method Clipboard.GetDataobject do with the data so Paste() can read from the clipboard.
I'm asking because I hate things which I cannot understand, they seem to tend to be risky , because maybe then sometimes it will work, sometimes fail, I dont know. Who can explain me what goes on behind the scenes.
Re: Clipboard and C# How does this work ?
hmm, this question looks like you were to lazy too take a look at msdn or press F1 in visual studio, or maybe you've never heard about it?
Re: Clipboard and C# How does this work ?
Hi,
Clipboard.GetDataObject Method retrieves the data that is currently on the system clipboard. You can use IDataObject methods to get the data and format if needed.
Re: Clipboard and C# How does this work ?
Quote:
Originally Posted by
memeloo
hmm, this question looks like you were to lazy too take a look at msdn or press F1 in visual studio, or maybe you've never heard about it?
Thx for invalidation instead better reading what I'm asking for.
My request was why the code I have shown works. I'm using the RTB's Paste() methode as you see to catch the data, which have been sent to the clipboard using a backgroundworker-thread which opens word, reads the document and copys it to the clipboard.
And the interesting thing is, if i ONLY USE the Paste method I get no data from clipboard, but if at first I'm reading them into an IDataObject object then I can past the data from the clipboard.
And my question was WHY ? Sorry I cannot find an answer in the MSDN documentation. Or I'm simple to stupid to understand what they are talking about.
Lodemar Thx a lot for the reference. I had already read it, before I posted. But my application is an STAThreadApplication and also is marked as such an application.
There are simple two questions not answered in all the MSDN I have already read and googled about whats the best way to read data from a word document into a Richtextbox using C#
The best exampple I found was using a clipboard, but the example was not using a backgroundworker and it also did not explain why it used to read the clipboard data into the IDataObject field, but never using it, instead using the RTB's Paste method to get the data into the RTB.
It was no problem to get it read into the clipbord by use of the backgroundworker, but Why it works when I insert that red line of code and why stays the RTB empty when I dont use this codeline ?
Thats what I want to understand. :D
Re: Clipboard and C# How does this work ?
I'm sorry for not having read and understood your post well enough. Although I don't know either why this is so you made me curious and I have to check it on my own ;]
Re: Clipboard and C# How does this work ?
Quote:
Originally Posted by
memeloo
I'm sorry for not having read and understood your post well enough. Although I don't know either why this is so you made me curious and I have to check it on my own ;]
I dont know if the phenomen only comes when you read the data using a backgroundworker, but the line was already added in the example I had found in the web
Re: Clipboard and C# How does this work ?
It is wierd. But why does it bother you so much, anyway?
Did you try to run the code above, without the line in red, not inside a BackgroundWorker?
Re: Clipboard and C# How does this work ?
Quote:
Originally Posted by
Talikag
It is wierd. But why does it bother you so much, anyway?
Did you try to run the code above, without the line in red, not inside a BackgroundWorker?
No this I havn't tried. I tried to set this line in the dowork delegate just after the data was copied from the word document. In that casse you will always and 100 % get an empty RTB. If you paste the RTB:Past code into the the dowork fdelebate you need to test for invokeRequired and using the Invoke stuff.
Best it works filling RTB by Paste() in the RunWorker_Completed delegate as this already is back in the GUI thread, so we have no troubles
I think that this is necessary to 'redirect' the Clipboard to the mainthread as it was filled by the workerthread. But its assumption only and I hate assumptions.
Additonal does anyone know how we can retrieve the data from IDataObject and loading the RTB without use of trb's Paste() method. ( How to cast IDataObject ) All I tried ended up in a) showing the full rtf code as a text where I could read all the tags ... or
b) a black and white text copy of the original text without any textformats in it. Bold, italic, sizechanges or colors, all lost
Only using Paste method I got WYSIWYG of the real word file.
The point is.. it works now. But this IMHO is not a professional way to do something you do not really know, why it works. Maybe it fails in some cases. How to get sure about, you see?