I know that I can set it up in the property window but I need to do it in code. How?

Dani AI

Generated

was correct that the designer "Locked" flag only affects the Visual Studio designer. To stop users resizing at runtime you must set the form up for a fixed size or otherwise restrict its window styles (the approach hinted at—set properties before showing the form—is the right place to do it).

A few practical options that weren't shown in the thread:

  • Fix the size by constraining MinimumSize and MaximumSize to the same value (set these in the constructor or Load so the user never sees a resizable window):

    this.MinimumSize = this.MaximumSize = new Size(800, 600);

    See the Form.FormBorderStyle docs for built-in fixed-border choices and visual differences: Form.FormBorderStyle property.

  • If you still want a fixed-looking border but also want to remove maximize/minimize buttons, explicitly set the box properties before showing the form (do this in the constructor or just after creating the form instance).

For advanced control (remove the sizing frame at the Win32 level), clear the WS_SIZEBOX style with SetWindowLongPtr after the form handle exists. That prevents resizing even if something else tries to re-enable sizing, but requires P/Invoke and 32/64-bit care — see the Win32 docs: SetWindowLongPtr function.

Troubleshooting tips: set these properties early (constructor or Load), avoid fighting the OS in Resize events (causes flicker), and remember modal dialogs (ShowDialog) behave differently than non-modal windows.

Recommended Answers

All 7 Replies

this.whateverAttributeYouWantToSet = whateverValueMakesSense

Why do you want to use Lock?
To prevent user can resize form?
If so, you use form`s properties like: MaximizeBox, MinimizeBox, like:

//on form1:
Form2 form2 =new Forms();
form2.MaximizeBox = false;
form2.MinimizeBox = false;

Or is there anything else you want to use Lock?

commented: thanks! got it +1

there's no this.Locked.

I need to prevent the user for resizing the form

Try this:

private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.FormBorderStyle = FormBorderStyle.Fixed3D;
            form2.MaximizeBox = false;
            form2.MinimizeBox = false;
            form2.Show(this);
        }

The Locked property is a design type only property that only affects if you can move/resize the form inside the designer. To prevent resizing set FormBorderStyle to one of FormBorderStyle.Fixed3D, FormBorderStyle.FixedDialog, FormBorderStyle.Single, FormBorderStyle.ToolWindow. If you want to prevent min/max, then set MaximizeBox to false and MinimizeBox to false.

commented: thanks! got it +1

Ok. I got it. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.