开发者

How do I prevent a user from resizing a window in my C# application?

How do I prevent resizing my applica开发者_如何学编程tion window?

How can I do that?


On the main Form (or any Form that you don't want to be resizable), change the FormBorderStyle property to Fixed______ (FixedSingle, Fixed3D, FixedDialog, FixedToolWindow).


Another way is to define your form size and set the actual size as MinimumSize and MaximumSize via the properties.

This doesn't change your BorderStyle and the end user can't modify the size of the form.


There are a few of workarounds for this:

  1. Set maximum size property to a value you prefer. If you do not want the application window to be shrunk as well, then set a minimum size property. If you prefer the application to have the exact same size as that of design time, then set both maximum size and minimum size as size of your window. (Once you set maximum size or minimum size from the designer, you can't resize your window programmatically, unless you re-set maximum size and minimum size programmatically again)

  2. Set FormBorderStyle to FixedSingle or FixedDialog. The difference in looks wont be noticeable for untrained eyes, but one considerable difference I'd found from my experience is that, when you make it FixedSingle, you can still change the size programmatically. With FixedDialog its not possible. That's a huge advantage for FixedSingle property. (If you want to change size of your window programmatically here after going for FixedDialog, then you got to programmatically change FormBorderStyle first, which would create a slight blink effect when running the application).

So simply go for FixedSingle. And to make sense, do the following:

a. Set maximize box property to false.

b. Set SizeGripStyle to Hide. (@colithium points this)


Try this:

Change FormBorderStyle to Fixed(Single, 3D, Dialog).


You can change the ResizeMode property of the Window object to CanMinimize or NoResize in the xaml or via the designer.


There is a problem to find these properties in older versions of Visual Studio, like Visual Studio 2008 and ResizeMode does not appear here.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  if (e.CloseReason == CloseReason.UserClosing)
  {
    e.Cancel = true;
    this.WindowState = FormWindowState.Minimized;
  }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜