I've encountered an annoying problem:
When I set my form's ClientSize property to match 300*300, the size indeed does change, but it's totally not 300*300. Actually, it's not even x*x but rather x*y, since its width is greater than its height.
This is a picture of my program after setting my form's ClientSize = new Size(300, 300); and creating a black Panel inside it with size 250*250.
I measured them with photoshop and the black Panel was indeed 250*250, whereas the form's client size was more like 280*260 or so.
Why is this happening, have I understood the ClientSize property's meaning wrong?
If so, how can I get the actual "drawing area" of my form to accurately match the size I want?
Check have you set min or max size of your form.
Related
I have a form which contains a Menu Strip docked to the top, a Status Strip docked to the bottom, and a Panel docked to fill the entire space between the aforementioned controls. I have set the attributes to the following values for the form:
During the design phase:
AutoScaleMode: Dpi
AutoSize: false
AutoSizeMode: GrowOnly
DoubleBuffered: true
SizeGripStyle: Show
During runtime (in the form's constructor):
// Calculate the default size of the window on the basis of the ratio of the dimensions of the window to the dimension of the screen resolution of the machine used in development as the default dimensions of the window is aligned to that of the machine used to design it
this.Size = new Size(Screen.GetWorkingArea(this.Location).Size.Width * (widthOfWindowInDesignPhase /horizontalResolutionOfTheDisplayInDesignPhase), Screen.GetWorkingArea(this.Location).Size.Height * (heightOfWindowInDesignPhase / verticalResolutionOfTheDisplayInDesignPhase));
this.MinimumSize = new Size(this.Size.Width, this.Size.Height);
My first attempt to resolve this issue was to tinker around with the AutoSize and AutoSizeMode properties, but I require this to be set to the aforestated values as changing them would not allow the user to resize the form. The other approach I tried, which also failed, was by setting the AutoSize properties of the aforementioned controls to false as to force the child containers of the form to not resize.
Thanks in advance.
PS Screenshot of the concerned form:
A (blunder) numeric error occurs when calculating the size of the form as the statements (widthOfWindowInDesignPhase /horizontalResolutionOfTheDisplayInDesignPhase) and (heightOfWindowInDesignPhase / verticalResolutionOfTheDisplayInDesignPhase) return a floating point value that cannot be stored as an integer.
The appropriate statement is as follows:
this.Size = new Size(Convert.ToInt32(Screen.GetWorkingArea(this.Location).Size.Width * (Convert.ToDouble(widthOfWindowInDesignPhase) / Convert.ToDouble(horizontalResolutionOfTheDisplayInDesignPhase))), Convert.ToInt32(Screen.GetWorkingArea(this.Location).Size.Height * (Convert.ToDouble(heightOfWindowInDesignPhase) / Convert.ToDouble(verticalResolutionOfTheDisplayInDesignPhase))));
I am trying to get an accurate Form.Width on FormClosing().
I have a zoom() method I use to scale a picture in the Form. The form's width is then manually re-sized by me to fit. I then, set the MaximumSize's Width limit to the new width of the form. For our purpose, let's say the width and maxwidth are now 1386.
If I re-size the form in the Windows interface by dragging its edge to the left, the width is being reduced just fine. FormClosing reports the re-sized width accurately. Let's say 1107.
However, if go through the zoom() method I wrote, even though I drag to the left to the same position, FormClosing is reporting the ORIGINAL 1386 width. The only things I do to form size in the zoom() method are set width and maxwidth.
I am at a loss to handle this behavior. I need FormClosing to report the correct size. I think FormClosing is setting the Width to MaximumWidth when it fires. I think I read something about how the Form is set to Hidden by the time the FormClosing event is fired. Maybe being Hidden reverts its size to MaxWidth.
Any help would be much appreciated. I have been struggling with this for a few hours and can't find anything.
Thanks.
Here is the relevant code:
private void zoom()
{
// Re-size form width to slightly more than page width.
// This is percentage-based, meaning 100 = default size.
Size picSize = new Size
{
Width = (int)(originalRenderingSize.Width * integerUpDownZoom.Value / 100),
Height = (int)(originalRenderingSize.Height * integerUpDownZoom.Value / 100),
};
// some code here which I commented-out, and still, the problem exists.
// Set maximum to prevent ugly sizing.
this.MaximumSize = new Size(picSize.Width + 40, Screen.FromControl(this).WorkingArea.Height);
// The problem is this: When this method processes, as shown, a page is rendered and sized according to
// a user-provided zoom level represented by an integerUpDown control. Once rendered, the form should
// re-size its width to match the newly-scaled page's width. This works, too. So far, so good.
//
// But!!! If the user grabs the right side of the form and drags it to the left to reduce the width
// of the form (mind you, this is Windows OS doing this), upon Form.Form_Closing(), you can see a
// quick flash of the form fully-open and unscrolled. Then, in the FIRST line of Form_Closing(),
// the debugger reports the form's width as the fully-open and UNSCROLLED width.
//
// The goal is to set the width upon the conclusion of this zoom, but then, on closing
// get the width of the Form in order to persist it for use the next time the app is run.
//
// 2 options have been tried. Both cause Form_Closing to erroneously report the fully-open width.
// But if BOTH are commented-out, the re-sizing occurs and Form_Closing() reports properly the
// form's scrolled width.
// Bear in mind that Form_Closing is one of those things that can happen anytime or never. This means
// the bug is triggered by EITHER and ONLY the two options below.
// Option 1: Tried first. It sets the width fine.
//
// this.Width = picSize.Width + 40;
// Option 2: It also sets the width fine.
// I think this option causes width to change (it invokes width change, and somewhere in the
// OS's width change, the error occurs.
//this.MinimumSize = new Size(MaximumSize.Width - 1, this.Height);
//this.MinimumSize = new Size(0, 0);
}
Edit: I have new information which should help. The culprit is FormClosing(). Here's what I did: I re-sized the form in Windows until it occupied maybe 500 pixels in width. The horizontal scrollbar on my panel was showing. Then, in the FormClosing event, I added the line: Form.Show(). After that, I put a MessageBox.Show("Hi."). Sure enough, the Show() caused the form to repaint in its full, unscrolled width. It did not maintain its scroll state.
I think I have the solution and will post back after trying. Namely, I need to check Scroll values and operate on those values as well.
Edit 2: FormClosing() also re-sets HorizontalScroll.Value and VerticalScroll.Value to 0. This is a bummer! I wonder how to work around this. I also looked at e.Cancel, but there seems to be no way to take advantage of it, either. It seems e.Cancel just re-shows() the form in its unhidden state.
Sorry I cannot post the code. It would be pointless anyway. Think about it. The Windows OS handles the resizing. Not me. When the user re-sizes the form, it's all on Windows. If user re-sizes the form to 500 pixels wide, but FormClosing() reports it is 1386 pixels wide, no amount of my coding will show us anything new. This is a behavior issue. There is no code to show you how the Windows OS handles form re-sizing.
On FormClosing Event Use:
MessageBox.Show(this.Width.ToString());
You're using another Instance of your form's class, Form.Show() should not work in this event cause Dispose function is called and if you have referenced the same instance it will be disposed as well.
So I'm making a C# Windows Forms application in which I have a label. This label's size is 100x100 by default*, but I want to automatically increase the label's height so that any string fits in it regardless of its "height". How would I do this? I haven't tried anything myself yet because I don't really have an idea of what to do. I'm just a beginner, after all!
Thanks in advance.
*100x100 is just an example, the real size is different (I'm not sure yet what it'll be)
Thanks everyone, I managed to get what I want by setting the label's MaximumSize property. (Answered by #LarsTech in comments)
AutoSize defaults to true - so the default behaviour of the textbox should resize to any font size/content that is in use.
The label control in Windows Forms is a container which accepts your input strings. Therefore, if you do change its initial values it will get to its content size anyways. So, just change its Text Property.
You could try it first.
here's an example code you can use. It uses the AutoSize property.
If you label is called Label1 you can change it like this:
Label1.AutoSize = true;
Label1.Text = "The text in this label is longer than the set size.";
And it will automatically change it.
How can I make two controls share the same height. I have set the Size to be the same for both controls but when I run it, I am seeing a difference.
this.txtUserName.Size = new System.Drawing.Size(382, 45);
this.btnLogin.Size = new System.Drawing.Size(75, 45);
Actually both have the same Height as you set them. However in order to accomodate various BorderStyles (or FlatStyles as it is called in the case of Buttons) the full size is not always visible.
So, the Button will look to be one pixel smaller on each side than the Height is has with these (current) appearances:
button1.FlatStyle = FlatStyle.Standard
button1.FlatStyle = FlatStyle.System
The full Size will be visible with
button1.FlatStyle = FlatStyle.Popup
button1.FlatStyle = FlatStyle.Flat
If you are sure about your choice of FlatStyle you may want to adapt their Height (and Location!) accordingly..
To further complicate thing the TextBox's visible Height will change if you set its BorderStyle from FixedSingle or Fixed3D to None: It will shrink by 7 (!) pixels..
Note that as far as I remember, all those details of the borderstyles depend on the Windows version & Visual Styles on the target machine.. I am running W8.1 here
Open the Form.Designer.cs and go to InitializeComponent() method for the form, you'll see initialization code for all components on the form.
If you are setting these values prior to InitializeComponent() call from the constructor, it'll be lost. Otherwise you may need to check Margin and Padding of the components.
http://msdn.microsoft.com/en-us/library/vstudio/ms229627(v=vs.100).aspx
I have a couple of pictureboxes that need to be resized by aspect ratio when the window size changes. I assumed I can anchor the width, but set the height manually (i.e. anchor the left, right, and top edges; but not the bottom.) However, my control won't resize if I try changing the Size property. Why wouldn't that work? How can I resize my control?
private void Form1_Resize(object sender, System.EventArgs e)
{
int oldWidth = 1280;
int oldHeight = 1024;
int newWidth = webcamPictureBox.Size.Width; // this is auto-resized w/ window; becomes 591
int newHeight = (oldHeight * newWidth) / oldWidth; // this is calculated manually; becomes 472
// Control won't resize if I change the Size property
// Size property stays the same
this.webcamPictureBox.Size = new Size(newWidth, newHeight);
this.thumbnailPictureBox.Size = new Size(newWidth, newHeight);
}
You could be running into one of a few problems, I suppose:
What's the SizeMode of the PictureBox? Is it AutoSize, StretchImage, or something like that? It should probably be Normal for what you want to do.
Are you sure you have your anchoring set up correctly?
Did you debug and check the final size of the picture boxes you're trying to resize? You should verify that it's what you expect and that the form has been repainted.
These are just some ideas; your code along is not enough to provide a great response.
It's difficult to answer this question definitively with the information you've posted.
Windows forms controls problems are difficult to debug by nature because of all the controls that might be affecting what you're doing. First off, you'll want to try debugging this on your own on a form with as few controls as possible. Are there any circumstances under which the resizing behaves properly?
That said, is the Dock property set on these controls? They definitely won't resize if they're set to DockStyle.Fill.
Thanks for the ideas -- they pointed me on the right track! Everything was set up as they should be, except the TableLayoutPanel they were in was constraining their sizes. I determined this by noticing the pictureboxes' size values were simply uneditable, both in runtime and in the designer.
So I set TableLayoutPanel's AutoSize to true, and it works great!