error on exiting a window - winforms

In the following picture, when i click File->New Game I see this window:
If i continue and press Start Game, everything works great. But if i just click the red X, i get this error:
This is the code for File->New Game:
private: System::Void newGameToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
NG->ShowDialog();
ShowPossible();
update_score();
if(pc_exists()==1)
ComputerPlay();
}
NG->ShowDialog() shows the New Game dialog.
And then ShowPossible() shows something on the board (hints for possible moves).
And that's what's making trouble.
I need some code that quits from newGameToolStripMenuItem_Click() on X click instead of continuing to ShowPossible().
I tried making a global variable called ready in form NewGame, and at form load initialized it with 0, and only when i click Start Game it turns to 1. And finally added this condition in the above function:
...
if(ready)
ShowPossible();
...
So this way if i don't click Start Game, and only click X, ready will be 0 and it won't enter ShowPossible(). But it didn't work. Somehow when the code for the button Start Game finishes, ready is still 0.
Is there any more efficient way to deal with this?
Thank you !

EDITED:
Since you are using System.Windows.Forms, check against the DialogResult enumeration (Thanks chris):
if (NG->ShowDialog() == System::Windows::Forms::DialogResult::Cancel)
return;
From MSDN:
When a form is displayed as a modal dialog box, clicking the Close
button (the button with an X at the upper-right corner of the form)
causes the form to be hidden and the DialogResult property to be set
to DialogResult.Cancel.

Related

when "button-press-event" will be called in gtk+?

I need an event which handles clicks on main window of my program.
I used button-press-event but when i click on my window nothing happens.
I'm designing my GUI using Glade Interface Designer.
please explain for me when button-press-event will be called in gtk+ & is it the event i need or not?
Check you've got the button press event mask set for the window under the
Window Properties - Common tab in glade
'button-press-event' should give you mouse button presses. It may be that you need to use gtk_widget_add_device_events () to add button presses to the list of events that are received by the window but I think it should get them by default, can you post you code please?

updating UI in windows phone 7

In method that is working in the background, i have two important lines :
createPopup();
MessageBox.Show(sth);
more lines
more lines
createPopup() just creates a popup, adds a grid as a child and shows popup.
My question is, why first shows up messageBox, then shows up Popup, which appears after all lines in this method done ? How could I make this popup to show before all lines in this method will be done ?
All the UI changes are normally queued up and will be shown at once on the screen.
And this does not include MessageBox. So it shows up immediately and prevents the execution, until user clicks on Ok. Hence eventhough your popUP is first executed, it will be shown in the UI only after the MessageBox.
For your problem, Try placing your MessageBox.Show(something) in a separate thread.
createPopup();
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("some message");
});
more lines
more lines
Give it a try. I am not sure whether it solves your problem or not as I dnt know the code in createPopUp() method.
Creating the pop-up, does not actually draw it on the screen until the Layout event. If you want to ensure that the pop-up has been drawn before you display the pop-up, attach an event handler to the pop-up's LayoutUpdated event and display the message box from within that event handler. Be sure to detach the event handler as well or you will see multiple message boxes.
public InitPage()
{
Popup popup = new Popup();
popup.LayoutUpdated += popup_LayoutUpdated;
LayoutRoot.Controls.Add(popup);
}
void popup_LayoutUpdated(object sender, object e)
{
popup_LayoutUpdated -= popup_LayoutUpdated;
MessageBox.Show("hello");
}

How can I show a FolderBrowserDialog more than once?

In my Windows Form's Form_Load event, I want to show a FolderBrowserDialog to let the user select a directory, and if the directory they've selected is not valid (meaning it lacks certain files that the application needs), I want to show it again. However, when I create a new FolderBrowserDialog, it does not appear when I call ShowDialog.
while (ValidDirectorySelected() == false && tryAgain == true)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
tryAgain = false;
}
}
}
When I step into it, the dialog.ShowDialog() line is reached on the second time, and then nothing happens. The dialog does not appear, and the debugger doesn't move on. It just stops. It works perfectly the first time, but not the second. I've even tried just copying that entire using block and pasting it right after the first one, and the same thing happens. The dialog shows only once.
What do I need to do to show a FolderBrowserDialog more than once?
Solution:
Passing 'this' to ShowDialog fixed my issue. I also moved the using to outside of the while loop, to avoid needlessly re-creating the dialog.
Minimize Visual Studio, you'll find the dialog back.
This is a focus issue, triggered because you display the dialog in the Load event. When the dialog closes, there is no window left in your app that can receive the focus. Your Load event hasn't finished running so the app's main window isn't yet visible. Windows has to find a window to give the focus to and will select one from another program. Like Visual Studio.
When you display the dialog again, it cannot steal the focus back because Visual Studio has acquired it. So the dialog appears behind Visual Studio's main window, out of view.
You'll have to fix this by allowing your main window to become visible. And call dialog.ShowDialog(this) to be completely sure. You could use the Shown event, for example.
Try this:
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
while (ValidDirectorySelected() == false && tryAgain == true)
{
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
tryAgain = false;
}
}
}
...move your using outside the while loop to keep from destroying the folder browser every time. You don't have to do that. You can reuse FolderBrowserDialog.

Show NotifyIcon Context Menu and Control Its Position?

I'm trying to show a context menu when I left-click a NotifyIcon. Just calling NotifyIcon.ContextMenuStrip.Show() doesn't work very well. A solution has been posted here before that calls a secret method using Reflection:
Dim mi As System.Reflection.MethodInfo = GetType(NotifyIcon).GetMethod("ShowContextMenu", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
mi.Invoke(Icon, Nothing)
This works great, except that I also need to control where the menu is shown. I want to wait for the SystemInformation.DoubleClickTime to elapse between receiving the NotifyIcon.MouseUp event and displaying the menu, so that I can handle single-clicks and double-clicks separately. But invoking the ShowContextMenu method displays the menu at the current mouse position when ShowContextMenu is called, not when the icon was actually clicked. Which means that if the mouse moved during the DoubleClickTime, the menu will be displayed in a different part of the screen. So if I can control where the menu is shown, I can just save the mouse coordinates when I receive the MouseUp event, and then I can ensure that the menu is displayed near the icon. Is there a way to do this?
Thanks in advance.
Well, I just discovered that there are existing programs that exhibit this same behavior. I just went through all the icons in my system tray and about half of them do it. If you left-click the icon and then move the mouse during the delay before the menu appears, the menu will appear at the last mouse location, wherever that is on the screen. Snagit is one application that does this. Outlook is the only program in my tray that always shows the menu where I clicked the icon. But Snagit looks like it's using a .NET ContextMenuStrip, while Outlook is probably using a native menu.
So either this is standard behavior, or it's a problem that no one else has been able to solve either. And as a user, I've never noticed this behavior until yesterday when I was testing my own application. So I guess it's not that big of a deal and I won't worry about it.

How do I disable the exit button on a Silverlight 3 Child Window?

I mean the small exit/cancel button marked with an X in the top right hand corner. I want to implement a Logon dialog box that accepts a username/password so obviously I don't want the user to be able to dismiss the modal pop up. If it is not possible to remove or disable the button then is there some way I can intercept the closing event and stop it closing?
You can use the HasCloseButton property of the ChildWindow to hide the close button.
Please let me know if this helps.
Ezequiel Jadib
The code below prevents a ChildWindow from ever closing, effectively disabling the X button. Modify to suit your business logic.
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
e.Cancel = true;
}
Select child window and Press F4. It will show the property window. Then goto HasCloseButton property and uncheck the checkbox.
Enjoy
HasCloseButton="False" ..
This property used to hide the 'X' button In ChildWindow

Resources