|
-
May 22nd, 2009, 07:44 AM
#1
Clipboard and C# How does this work ?
Hi friends !
I have created a code by use of an example which works, ...
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.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 22nd, 2009, 07:54 AM
#2
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?
Last edited by memeloo; May 22nd, 2009 at 08:50 AM.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
May 22nd, 2009, 08:44 AM
#3
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.
-
May 22nd, 2009, 11:21 AM
#4
Re: Clipboard and C# How does this work ?
 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.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 22nd, 2009, 11:35 AM
#5
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 ;]
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
May 22nd, 2009, 12:12 PM
#6
Re: Clipboard and C# How does this work ?
 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
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 22nd, 2009, 12:37 PM
#7
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?
-
May 22nd, 2009, 01:24 PM
#8
Re: Clipboard and C# How does this work ?
 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?
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|