Show only one instance of messagebox.show in c# - c#

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

Related

MessageBox acting weird

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.

limiting tabbing to a custom in-window message box

I have made a custom MessageBox for my application and it launches as a UserControl. I have two buttons inside it and I would like to allow users to press Tab to switch between Buttons. However, since it's a UserControl overlaying the content, Pressing tab more than twice makes the focus go in the background on elements that aren't supposed to be tabbed at.
I can't figure out a good idea how to prevent this, I've thought of making a method that will select all elements and make their IsTabStop values to false and then restore them later, but I think that would be more of a problem then a solution.
Is there a way around this to limit tabbing only to the UserControl?
I would also appreciate advice on working with the message box.. the whole messagebox is an async function that has an infinitive loop until the answer is given. Is there another way to stop the application until one of the message box options was selected?
Crowcoder's reference has lead to correct MSDN page where I found my solution:
dialog = new UCMessageBox("Are you sure you want to exit the application?", MBType.YesNo);
AppMessageBox.Children.Add(dialog);
KeyboardNavigation.SetTabNavigation(dialog, KeyboardNavigationMode.Cycle);
The key was to call .SetTabNavigation function and direct it to my dialog (custom UserControl for the message box) and setting the KeyboardNavigationMode to Cycle.
After closing the UC rest of the application continued normally regarding navigation.

Mono Gtk Window require focus for input

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.

Add button to copy text in MessageBox

I have a program that contains a list of names, and a button that displays a random name in a MessageBox. Is there any way I can add a button "Copy" next to the OK to the MessageBox, that when clicked copies the name and then closes?
If the above isn't possible, is there a way to enable copying the text in a MessageBox?
Thank you.
edit: My users won't understand Ctrl+C, Highlight and Right Click > Copy is what I'm looking for (if a Copy button isn't possible)
If a user presses Ctrl-C while the MessageBox has focus, the message, the MessageBox caption and the MessageBoxButtons labels are copied to the clipboard.
I googled your title and found this..
Or if you really need a button that says copy you can create your own MessageBox with a new windows form and then do what you want with the buttons. Open it like this to keep the MessageBox feel :
var myMessageBox = new CustomMessageBox();
myMessageBox.ShowDialog();
It sounds like maybe you are looking for the Clipboard class.
Clipboard.SetText(variableWithValue);
There is also another answer here about manipulating the contents of a Message Box.
It also might be easier to simply make a modal dialog that emulates a MessageBox without actually using the MessageBox class.

How to save/discard values of controls of a form, upon pressing Ok/Cancel button?

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 ).

Categories

Resources