I have a very weird problem.
I tried to run this code and whenever I pressed the space key it print multiples 'A' as I want.
comments: the code is inside main, Key.IsPressed is using WinAPI GetKeyState method and the space constant is the virtual key of the space key.
while(true) if(Key.IsPressed(Key.Space)) Console.Write('A');
but when I replace Console.Write('A'); with System.Windows.Forms.MessageBox.Show("A"); it show the message box only on the first time I press the space key (After I close the message box and then press space again).
Even weirder, if I change the key from space to enter it works fine.
The purpose is to create a hidden shortcut manager program so I can't use any key reader that needs focus (like Console.ReadKey).
Any time you use a framework method and it doesn't do what you expect, you should first read the documentation. There you will find information on how the Show method works, for example:
A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.
Related
I haven't been able to find an example containing this functionality, and either i missed it in the documentation or it's not there.
I have a fullscreen GUI program, and when a user is required to type in a number, a calculator window popup has to appear in the center of the screen. The user types a number and clicks on enter, or hits cancel to continue past the window.
The problem is clicking on the fullscreen window behind the calculator brings that window to the front and hides the calculator without the intended entry being completed, which could get annoying for the user.
I guess the functionality I'm trying to create is what happens in most text editors/IDE's when you press the Open File button. Let me know if you want to see code, it's just two separate Window classes at the moment.
The Present() function of the Window object brings that particular window to the front. So adding a FocusOutEvent listener on the window you wish to keep in front like:
windowObj.FocusOutEvent += (obj, args) => windowObj.Present();
will work.
The alternative to this (and the better way) is to set the Modal property of the window to true.
If you've stumbled across this and you were looking for a popup window that suspends whatever called it to wait for input, see this question:
gtk# thread for window
You basically use the Dialog class instead of Window and add your elements to the ActionArea of the Dialog.
Hope this helps.
I have a string value in a variable. I want it to be thrown in keypress event.
When user clicks on "Start Writing Button". The text contained in variable gets written to the area whereever cursor has focus.
eg.
string str = "Example"
I have a web page with a textbox and a button. When user clicks on Start Reading button Example gets written on to textbox.
Basically the characters being written should be trapped on
javascript-onKeyPress event
Winform- KeyPress event
etc.
EDIT
I want to use some devices that will be throwing data constantly to my variable using window service. I need to write this data to the active window whereever the cursor has focus currently irrespective of window or web.
I copied data to clipboard and pasted on active window but this is problematic since different tabs are considered to same active window and doesn't writes.
Looking for a proper way rather then workaround I have taken.
Now that you've clarified the question, I suspect you want SendKeys.SendWait.
You'll need to be somewhat careful with it, but it may do what you want.
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 ).
I have a created a custom keyboard shortcut for my application,
when the user press combination keys of CTRL + ALT + Q, i show
a messagebox "Are you sure you want to log out ?" Then if clicked
YES, i log out of the application.
Problem :
I want to make sure that, only once instance of message box shows.
No matter how many times the user presses the shortcut.
currently it shows multiple message box, on pressing multiple
shortcuts.
How to overcome this ?
From MSDN
A message box is a modal dialog box,
which means no input (keyboard or
mouse click) can occur except to
objects on the modal form. The program
must hide or close a modal form
(typically in response to some user
action) before input to another form
can occur.
File a bug on connect.microsoft.com !
Taking ck's comment into consideration...If you are showing a custom dialog (form) then you need to invoke the form using Form.ShowDialog() and not Show().
A quick and dirty way would be to have a class level boolean variable that tracks when the user is trying to exit. If they are, it's set to true, and your routine to display the dialog box can check this flag, then return without doing anything.
Seems like Singleton Pattern is your option.
I think you can create your own form and use the mymessageboxform.show() method, and check its dialogue result.
You'll want to make your application single instance so it can only be started once.
Single Instance App
I want the 'Alt' to be pressed by code. It is like firing key-press event with key 'Alt' by the code, not hitting it manually. The need is, I have set 'ShortCut keys for menu, but it (the single underline on key letter) is not visible to the user unless he presses 'alt'. So i need to make the Alt be pressed by default.
Is there a way to 'press' or 'fireup' the keys in key board using c# code?
Check out the System.Windows.Forms.SendKeys class.
You can use the static Send method to send keystrokes to the active window. If you're trying to send keystrokes to another window, you'll need to use the Windows API to activate the other window first.
If you have any control over the operating system on which the program is being deployed, apparently you can force the underlined shortcut letter to always be displayed by going to Control Panel -> Display -> Appearance -> Effects -> Hide underlined letters for keyboard navigation.
(http://www.chinhdo.com/20080902/underlined-letters-windows/)
here is a complete article on use of SendKeys on codeproject
Here is how you simulate input (both mouse and keyboard).
http://msdn.microsoft.com/en-us/library/ms171548.aspx
If you look at the System.Windows.Forms.SendKeys class you will see that it provides you with what you want.
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx