I´m building an application which shows a modal dialog for some operation. That modal dialog is build using a Form, and has as Owner the main application window, which is passed as parameter in the Form.ShowDialog method.
That modal dialog needs to spawn a common dialog when a button is clicked, (FontDialog) A common dialog is showed as a modal window too.
So the "Owner hierarchy" is (A --> B means A "owns" B) MainApp --> ModalDialog --> FontDialog
Ok, the problem is the FontDialog does not show in anyway UNLESS I press the ALT key. The application behaves almost as expected i.e. when I call the FontDialog.ShowDialog() I can't set the focus neither to the MainApp window nor the ModalDialog, but the FontDialog is just "invisible" until I press the ALT key (just that single key) and then shows up.
Anyone has a clue why this is happening? I tried setting the Owner of the FormDialog to null (thus using the Desktop as Owner window) but it behaves in the same wrong way.
Thanks in advance.
Some code
public class SnapshotDialogView : Form
{
/// bla bla bla
///
/// Button click handler
private void btnChangeFont_Click(object sender, EventArgs e)
{
// this.Owner == MainAppWindow
DialogResult result = fontDialog.ShowDialog(this);
if (DialogResult.Cancel == result)
return;
Presenter.ChangeLabelsFont(fontDialog.Font);
}
}
Ah, yes, one more thing. If I hide the ModalDialogForm (SnapshotDialogView in the code) before calling the FontDialog.ShowDialog(), the font dialog shows ok.
I had a similar issue with using MessageBox.Show()
.
After some reading/testing I discovered it was a side effect of overriding onPaint()
with one of my components. I'm guessing it misses a refresh or something when the main frame loses focus since anything that would cause it to repaint makes the MessageBox show up.
my quick solution is set the component Visible = false
before the dialog is shown and set it to true after:
private void btn_Click(object sender, EventArgs e)
{
Grid.Visible = false;
MessageBox.Show("asdf");
Grid.Visible = true;
}
I'm not sure if you found a different solution already since this is a few months old. If you did I am curious about what you found.
Edit: I just read your comments and it looks like we found similar solutions.
Here is a bare-bones example of the scenario you described.
using System;
using System.Drawing;
using System.Windows.Forms;
public class MainAppForm : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainAppForm());
}
public MainAppForm()
{
Text = "MainAppForm";
Controls.Add(new Button { Text = "Show ModalDialog", AutoSize = true, Location = new Point(10, 10) });
Controls[0].Click += (s, e) =>
{
using (ModalDialog dlg = new ModalDialog())
dlg.ShowDialog(this);
};
}
}
public class ModalDialog : Form
{
public ModalDialog()
{
Text = "ModalDialog";
Controls.Add(new Button { Text = "Show FontDialog", AutoSize = true, Location = new Point(10, 10) });
Controls[0].Click += (s, e) =>
{
using (FontDialog dlg = new FontDialog())
dlg.ShowDialog(this);
};
}
}
It does not exhibit the behavior you describe. That means that there is something in your code that is at fault.
What I suggest is to start removing all of the code you can until the problem goes away. That should clue you in as to what the problem is.
Alternately you can start with the above program and start adding your code until the problem appears.
I experienced the same behavior as described here - Modal Dialog Box not displaying until ALT key is selected. Ultimately, I tracked down the issue to be within the DataGridViewCellPaintingEventHandler. I have logic within cell painting event handler to customize painting of some cells. I was not setting e.Handled=true in some scenarios. When e.Handled was not set to true, cells still painted correctly, but resulted in Model Dialog Box not displaying until I selected ALT key. Correctly setting e.Handled=true in all scenarios resolved issue.
Also, previously, I was only customizing painting for cells I needed to customize. Now, I am painting and handling all cells. For cells, I do not need to customize, I perform the following:
e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
e.Handled = true;
Handling painting for ALL cells seems to have resolved the Modal Dialog Box not displaying until ALT is pressed issue.
Hope this helps someone since it is a little different than other answers provided.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With