Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clipboard.GetDataObject is working - but one step behind! 1

Status
Not open for further replies.

Olavxxx

Programmer
Sep 21, 2004
1,134
NO
Ok, so this is a quite hard thing to google for.
I found only one person who had the same issue, but he had posted it on that lame forum we all know.. the one who wants you to pay and register, to read on the forums..

Anyhow, Here is my issue:
I have made a "copybrowser", eg. it is a webbrowser in C# 2008, which listens for keyboard events.

What it triggers on, is
Code:
  if (Keys.C == (Keys)vkCode && Keys.Control == Control.ModifierKeys)

This works perfectly and it's really nice to watch my application trigger on copy events.

What it does, is quite simply:
Code:
                    if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text, true))
                    {

                        frm2.setSearch(Clipboard.GetDataObject().GetData(DataFormats.Text, true).ToString());
                    }

So, where is my problem?
Well, my problem is that GetDataObject is "one step in the past".

I fill the Clipboard with using CTRL+C and it works, but I have to "doublecopy" if I wish to get my result at once.

Eg. if I have some text:
abc def geh

And I first copy abc, nothing happends.
If I then copy def, I get abc.

I think you get my problem.
I have tried clearing the Clipboard in the Program.cs - static void Main().

I also tried adding "" to the Clipboard, hoping it might "push" the array.

I have searched on google and on msdn, but I cant seem to find this fault, which I guess is just a MINOR fault :p

Hope someone can help me!

ps. it's a non profit thingy for my work, so we can search on a custom webpage we are building atm., since we have no way to integrate two of our major archival systems! (and the archival storage-id's are very easy to mis spell, so it's important to have something slick).

Olav Alexander Mjelde
 
Is it possible that your application is firing before the data is copied to the clipboard via the key combinations, i.e.

Ctrl+C-->Your GetData-->Data Copied to Clipboard

Which would mean you got nothing the first time and then the previous copy as you describe.

Just an 'off the top off my head' idea but it's the only thing I can think of at the moment.

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Hi, I guess I could start a new thread and pause it like 1000ms or so..

Thanks for the tip.. I'll try it and check if it works out :)

Olav Alexander Mjelde
 
Wee, thank you A LOT!

I put in a thread.sleep and now it works like a charm :)

Code:
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                //Console.WriteLine((Keys)vkCode);
                if (Keys.C == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
                {
                    Thread.Sleep(1000);

                    if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text, true))
                    {

                        frm2.setSearch(Clipboard.GetDataObject().GetData(DataFormats.Text, true).ToString());
                    }
                }
            }

So, now I can polish my coding a bit, also I dont really like to "sleep" in the GUI, so I think I will make a bgworker or something, maybe just a threadpool where I have a "sleep", before it triggers the .setSearch();¨

Anyhow, I will give you a star for this tip :)

Olav Alexander Mjelde
 
I agree, I'm not a big fan of using Sleep in the GUI either.

Glad we at least found out why it was behaving the way it was, thanks for the star [smile]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Haha, I have this one application I made, which is multithreaded just to have have a simple function..

It's another specialized webbrowsing application.
It checks regexp while pressing enter in input fields.. eg. if the input is correct, it goes to next input.

When you have succeeded in filling out the inputs, the enter in the last field, submits the "form".

Then it starts a new thread with sleep, so that after 10 seconds, it gives focus back to the first input. (also on focus, it selects all in that input).

It's really slick and I really enjoy developing the application, as I see 5-6 people using it and it REALLY improves the speed and effectivity of theire work.

This is especially the cause, since the webpages it searches on, has very limited faulthandling and no input-checks (easy to crash the scripts).

However, then I can filter out wrong input, I can streamline the input and give a minimized UI.

So, yes.. I must say that I like the WebBrowser component for theese tasks :)

The one I'm making now, will search on an htmldb page, not built yet, so I just test it on google atm.. but it works like a charm now!

Thanks again!

Olav Alexander Mjelde
 
Sounds like a very useful/productive app you've built! [smile]

Again, glad I could help [smile]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Oes Noes..

Today, when I got back to work and testet it on XP SP3, the fix didnt work there (I use Vista Ultimate at home).

I thought I might have done some other changes too, but I tried logging into my personal PC from work (remote login) and emailed the codefiles to my work, but they have the same issue here.

I tried googling some more and I found one guy claiming that the clipboard has a bug, which supposedly should get fixed in version 2.0, with some additional parameters (retrytimes and retrytimeout).

He claimed that the problem might be if two applications use the clipboard at the same time, that it could get write protected (or something in that manner).

However, in the way I use the clipboard, I dont think I can affect this behaviour.

I think I'll try to download some clipboard code projects and see if they work on this PC, which has problems with the code.

Ps. I also ahve problems with compiled code.. I'll try on some more xp pc's..

Stay tuned for more hairpulling :p

Olav Alexander Mjelde
 
A fresh day, requires a fresh code :)

So, now I have removed my logic from the Program.CS and I just keep the keytrigger code there.

Then now instead of triggering on CTRL+C as before, I now use simply:

Code:
                if (Keys.F12 == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
                {
                    if (frm2.GetAutoTrigger() == false)
                    {
                        frm2.SetAutoTrigger(true);
                    }
                    else
                    {
                        frm2.SetAutoTrigger(false);
                    }

                }

eg. Now I have code in my webBrowser form, which gets the clipboard data in another way (used some of this code
So, now it works perfectly.
Instead of triggering on CTRL+C, it now triggers on the Clipboard and I use CTRL+F12 to activate or deactivate the auto-triggering on the clipboard event.

Hope this helps someone else in the same distress as I where in. :)


Olav Alexander Mjelde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top