c# - Sizing code in a form resize event does not work -
i have problem sizing form in form size event. trying avoid situation form goes off screen when user un-maximises form , form cannot resized (as corner off screen). although can't seem reproduce situation. anyway came code out of situation should ever occur again. problem form height not set when form un-maximized, although the code in if statement reached. 1 time when running application top , left properties got corrupted , both became -32000. again came code prevent causing problem. here code, note width fixed:
public partial class mainform : form { rectangle sr; formwindowstate wp; public mainform() { sr = system.windows.forms.screen.primaryscreen.bounds; maximumsize = new size(width, sr.height); wp = windowstate; } private void mainform_activated(object sender, eventargs e) // positions form { top = properties.settings.default.top; if ((top > sr.height - 80) || (top < 0)) top = 80; left = properties.settings.default.left; if ((left > sr.width - 80) || (left < 0)) left = 80; height = properties.settings.default.height; } private void mainform_deactivate(object sender, eventargs e) // remembers forms position { properties.settings.default.top = top; properties.settings.default.left = left; properties.settings.default.height = height; properties.settings.default.save(); } private void mainform_resize(object sender, eventargs e) { control control = (control)sender; if ((windowstate == formwindowstate.normal) && (wp == formwindowstate.maximized) && (control.size.height > sr.height - 80)) // following line has no effect: control.size = new size(control.size.width, 400); wp = windowstate; }
thanks.
make sure form not in maximised state when trying apply height/width changes.
-- athar
Comments
Post a Comment