Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

c# - Why does modal-form disappear completely when minimized?

I'm trying to have the owner-form minimize when the modal-form is minimized. But when I minimize the modal-form – it disappears completely. (- I can click on the owner-form.)

How do I solve this?

I have:

public partial class Form1 : Form
{
    Form2 frm2 = new Form2();

    public Form1()
    {
        InitializeComponent();
        frm2.Owner = this;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm2.ShowDialog();
    }
}

And:

class Form2 : Form
{
    Form1 frm1;
    FormWindowState ws = new FormWindowState();

    public Form2()
    {
        SizeChanged += new EventHandler(Form2_SizeChanged);
    }

    void Form2_SizeChanged(object sender, EventArgs e)
    {
        frm1 = (Form1)Owner;
        if (WindowState == FormWindowState.Minimized) 
        { 
            ws = frm1.WindowState; 
            frm1.WindowState = FormWindowState.Minimized; 
        }
        else frm1.WindowState = ws;
    }

}

(While trying this, I also ran into this: Modal form doesn't appear in tray until minimized and owner-form is clicked once. How do I make it appear? )

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is by design. As part of the modality contract, showing a dialog disables all the other windows in the application. When the user minimizes the dialog window, there are no windows left that the user can access. Making the app unusable. Winforms ensures this cannot happen by automatically closing the dialog when it gets minimized.

Clearly you'll want to prevent this from happening at all. Set the MinimizeBox property to false. The MaximizeBox property ought to be set to false as well, making both buttons disappear from the window caption. Leaving room for the HelpButton btw.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...