开发者

Modal common dialog not showing until pressing the alt key

开发者 https://www.devze.com 2023-01-26 20:22 出处:网络
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 F

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 sho开发者_运维百科ws 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.


I am not used to code in .NET, but I faced similar problem with my win32 (pure c++) code and MessageBox() function. What i mistaken was that I did not done my drawing whithin BeginPaint() and EndPaint() function pair in WM_PAINT handler. As soon as I added those two functions and done my drawing code in between them the problem disappeared. Although I don't know how that can be done in .NET environment:(


Try using your modal form as the owner of your FontDialog:

yourFontDialog.ShowDialog(yourModalForm);


I had the same problem with the MessageBox in VBNet.

It seems like windows though that the top most window is not the actual one. By calling BringToFront() right before the MessageBox.show it resolves the problem:

Me.BringToFront()
MessageBox.Show("blablabla", Me.Text, 
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question)
0

精彩评论

暂无评论...
验证码 换一张
取 消