I'm trying to save images from clipboard. This works well, if I copy something from Paint for example. If I take a screenshot, the Clipboard.GetContent().AvailableFormats shows me 0 items - so no Bitmap Image.
But when I put a timer for 1ms which gets the clipboard data, everything works fine. Since I think depending on random time is dirty, I would like to call something like WaitForClipboard.
Any ideas?
I think you could achieve it by handle clipboard update event. You could reference the below link for how to handle clipboard event.
Clipboard event C#
In the handler of clipboard update event, you could use the Clipboard.GetContent().AvailableFormats to check for available content.
Related
I'm trying to set the cursor in an open document, but it doesn't show. I can see that the line is "marked" and I can "navigate" the lines, but the cursor is not shown, and thus I'm not able to write anything. Also it seems like the document doesn't really fully load, since guidelines and the navigationmap is not shown either.
Which makes me believe that the focus isn't being set completely inside the document.
I have confirmed that the focus indeed is not set in the document in the window, since If I have the output window focused before, it's still focused after the window.Activate() method has been called.
I've used the common way of opening the document through ProjectItem.Open(Constants.vsViewKindCode), activating it, and using the TextSelection.GotoLine(1,false) method.
This correctly shows the document and sets the line correctly, but I have to manually click inside the document for the cursor to appear.
The code I have:
Window window = projItem.Open(Constants.vsViewKindCode);
window.Activate(); <----- this does not focus the window.
TextSelection textSelection = window.Document.Selection as TextSelection;
textSelection.GotoLine(1, false);
I want to not have to manually click inside the document for it to load completely and for me to be able to write in it.
Hope someone can help me.
Oh well, another issue I had to solve by myself... Two for two. I guess people don't know that much in here like I'd hoped. Oh well.
Anywho, I discovered that if you use DTE.ItemOperations.OpenFile(path); and nothing else,
the file will receive focus correctly, just like it should have when you use .Activate().
I have a Form with a TextBox in it. Every time the text changes i use the TextChanged Event to create a PDF-file and load it to an AxAcroPDF-Object in the same Form. This works fine, but then the TextBox loses focus and for some reason the textBox.Focus() after loading the file doesn't work.
Has anyone ideas how I can arrange that you can go on typing while refreshing the PDF?
EDIT:
i had another idea, i made a separate thread where i update the PDF and in the TextChanged-event i only set a flag. But now im getting a strange error
Unable to cast COM object of type 'System.__ComObject' to interface type 'AcroPDFLib.IAcroAXDocShim'.
Try this one:
textBox.Select();
textBox.Focus();
Im so ashamed of myself, i found a really, really dirty hack, but it works...
I did the following:
When i write a text in the MessageBox i rewrite my PDF in the TextChange-Event. In the same method i store the Control that has focus (when calling the LoadFile on the PDF-Object this Control still loses focus). And now the dirty work comes: I implemented a Thread that constantly sets focus to the Control stored in the variable. In the Leave-Event of the TextBox i reset the variable so other controls wont be blocked.
Its a really dirty hack i know, but now i can instantly "edit" a pdf with my own form, which is a nice eyecandy ;)
Thanks for all the help!
I could not get .Focus() and .Select() to work so I used Jquery and it works perfectly.
$(document).ready(function () {
setTimeout(function () {
$(".contentWrapper input")[0].focus();
}, 100);
});
i have a question about the moving file event on filesystemwatcher class , i`d like to stop the moving of file or edit it when the moving file event arises for a certain file , is that possible to handle inside the moving event ?
No, there is no way to stop someone moving or renaming a file using the FileSystemWatcher class.
If you look, none of the event arguments passed by the events on the FileSystemWatcher class have a Cancel property. Also, the fact that the class is simply called a Watcher is a bit of a clue.
You might consider using Access Control Lists to make sure someone cannot delete a file (since a move is really just a copy/delete). Or perhaps you could try opening a FileStream on the file so that you have it locked.
You cannot directly "cancel" the operation by means of the event handler. You would have to provide a compensating operation to programatically "undo" any changes you want undone.
You only receive the events after the fact has happened. It's a mere notification, not an event you'd have to approve. This can also be guessed from the missing Cancel or Handled property in the FileSystemEventArgs (as opposed to, for example, the KeyEventArgs) class.
You can detect a move and try to move the file back, based on the OldFullPath property of the RenamedEventArgs you receive.
This might however be confusing to your users or to other software. And try not to end up in an infinite loop, where you move the file back and forth every time you receive the event.
Suppose i have a textfile which continuous to be updated with text, how can i display the contents in a textbox in windows form (in real-time)?
for example. this is the contents of log.txt:
connected, bla bla bla
disconnected, bla bla bla
PS: i want it to be displayed in textBox of Form1 (real time also) so everytime the text file has new text, the textbox displays it. Any ideas, pls help. thanks. also can u provide a sample working code. thanks
Use a FileSystemWatcher
Use the example on MSDN (see the link above) you can add a event handler to Changed Event
and update your textbox from there.
Just call Application.DoEvents(); once you've updated your text. But note that updating too often might cause flicker as well as slow down your overall processing. Also note that this is error prone if any drawing/update code causes updates/draws again (infinite recursion).
Edit: Read half the question ...
Add a FileSystemWatcher to watch for changes to the log file. However this is a very ineffective approach and depending on the settings used to write the log file you might be missing access rights and/or the file might update only when the application closes. If both processes are your own code and you're able to modify them, you should think about other possibilities (e.g. a simple "server" you can connect to using some telnet (or custom) client).
Insert a RichTextBox and a FilesystemWatcher, and a textbox or something else to store the path and filename of the text file you want to show. Now, go to the FilesystemWatcher_Changed (or whatever it's called) event. There you put an if that checks if the changed file is the one you want to be shown, and then RichTextBox.LoadFile(file).
What this is going to do is monitor the fileystsem (hard disk or any folder you specify), and every time a file has cha nged, it's going to check if it's the file that we're looking at. If it is, it's going to reload the file into the RichTextBox.
Don't forget to set the RaiseEvent and Path properties of the FilesystemWatcher.
Example:
string blaFile = "C:\TextFile.txt"; // The text file that we want to read.
private void FilesystemWatcher_Changed(....).... // The FilesystemWatcher_Changed event notifies us when a file on the monitored path (FilesystemWatcher.Path) has changed. When a file has changed the code within this event gets executed.
{
if (e.File == blaFile) // We check if the file that has chenged, is the one we want.
{
RichTextBox.LoadFile(blaFile); // It is! We load it into the RichtextBox again so that it's "up-to-date"!
}
}
I'm not completely sure about the e.File as I haven't used C# in a while now, but it's something like that.
It is not suggested to use text file, since there are two processes to access the file and you require real-time update.
Try to use NamePipeServerStream and NamePipeClientStream for two processes memory sharing.
Please guide me as how to save/discard values of controls of a form, upon pressing Ok/Cancel button accordingly in Visual Studio C#?
Controls in a form include TablelayoutPanel(TextBoxes), NumericUpDown.
Need your expert guidance
Regards
Asad
With both of your buttons, inside the "onclick" event, call a function that will save the content of the form. You also need this call in the "onclose" event of the form, in case the user presses the top-right X button (or not, if you dont want data to be saved at that moment)
Inside that function, you will need some code that will save data to the registry.
Writing in the registry is easy. This webpage also explain how to get the data back. The values you will write will be the textbox.Value and such
The question isn't clear, but in a WinForm you can call
this.Close()
on the Click event of your Close button.
Every object or variable used by the form will be destroyed. Be careful! running background threads will still be alive until they terminate.
About saving the status of your variables it completely depends on what you need to with them after; you can either keep them in memory and pass them around like parameters or write on a disk (maybe with serialization?).
We need to know more.
edit
You may want to take a look at Application Configuration ( http://msdn.microsoft.com/en-us/library/ms184658(VS.80).aspx ).