Control anchor property not working when form starts maximized - c#

Here's my problem: I have a winform with controls on it. Many of these controls have their Anchor property set to Top|Right. The size of the form in the designer is set to 1680x1050. If my resolution is set to 1680x1050 then it always comes up correctly.
If I change to a smaller resolution(say 1600x900) and the form is set to open as Maximized, then none of my controls move themselves to maintain their distance from the right edge. The controls on the right edge are all sticking out off the form a little. But, if I then unmaximize the window, and I can resize the window and all the controls will maintain their current, incorrect distance from the right edge.
If I set the form to start as normal(not maximized) then it opens up with all the controls in the right place, and everything stays in the right place if I resize the form.
This has been a very frustrating problem. Do any of you kind souls have advice for me?

Id recommend just using my proposed solution of:
theForm.WindowState = FormWindowState.Maximized;
My guess is that the property "Maximized" that is set during the initialization call of the form and may be causing the problems. (It's hard to say without seeing the project code). The Load even gets called after some of the more important events, so if there is some sort of existing problem with that property, it is avoided using the FormWindowState.Maximized code.
If you wish to post the actual code of what you think may be causing the problems, I'll edit this answer to help.
-J

I Solve my problem by:
Set the Control AutoSize property to false.
private void Form_Load(object sender, EventArgs e)
{
dataGridView1.AutoSize = false; //true;
}

Put all controls in SplitContainer and set Dock property of splitcontainer and control such as datagridview = Fill

Related

Scrollbars if form larger than screen

I'm needing some assistance with what I can tell a vendor developing a form in an application I don't have the source to. We've found a bug with one of their large Winforms where if it's opened in an RDP session at 1024x768, the bottom 250px or so of the form which is ~1000px high is unreachable.
They're being difficult about saying there's no solution which I know to be nonsense, but I don't have a Visual Studio environment presently set up to build a proof of concept to test for myself and then show them otherwise.
From what I can tell from other StackOverflow questions and MSDN documentation pages, something like the following should resolve the problem so the form will auto-size to the maximum displayable height then put a vertical scrollbar in to allow viewing the bottom part of the form. Will the following achieve my goal?
public MyTallForm()
{
InitializeComponent();
this.AutoScroll = true
this.AutoSize = true
}
AutoSize responds to change in controls inside the form by growing & shrinking the form as needed.
AutoScroll responds to change in controls inside the form by displaying/hiding the scrollbars.
Thus, AutoScroll won't be activated if AutoSize is active since the form is always large enough. If the problem is from too small display resolution, you'll want AutoScroll.
If the form kept open between RDP sessions, you might need to subscribe to DisplaySettingsChanged to be aware of resolution changes, and either simply Maximize (not sure if it's already maximized, toggling to Minimized and back to Maximized perhaps?) or use GetWorkingArea if you need detailed size.
Set AutoScroll = True and AutoScaleMode to Dpi In Form Properties
Hope it helps.

How to autosize the form window based on screen resolution during run time (Where the size of the window created during design is too large)

I have created a windows application where the form size is large enough to view.
I want the form to shrink or maximize based on the screen resolution of the display such that the user can view all the controls in a compact way.
I tried the auto size property in the form but that doesn't work. Is there any property in the form which can resolve this issue?
Or do I need to code something else to resolve this issue ?
Thanks
Setting the WindowState = Maximized will cause the window to open to the full extents of the screen resolution. It doesn't matter what that value is, it will match it.
Edit:
From your comments, it sounds like you want what the AutoSize property will accomplish. I updated the form to add some controls and set the AutoSize = True and the AutoSizeMode = GrowAndShrink. Between these three properties, you should be able to get the form to do exactly what you wish. The one thing to pay attention to is the full extents of your controls within the form. From this picture you can see the form during runtime will resize to fit both text boxes while in the editor, I shrunk the form to hide almost everything. Please also note that in the example below, I set the WindowState = Normal.
Inside the form load event, do this:
private void Form_Load(object sender, EventArgs e) {
Rectangle resolutionRect = System.Windows.Forms.Screen.FromControl(this).Bounds;
if (this.Width >= resolutionRect.Width || this.Height >= resolutionRect.Height) {
this.WindowState = FormWindowState.Maximized;
}
}

Can I make a window not resize itself until the user lets go of the mouse button?

I have some screens with a lot of stuff on them, and the redrawing performance is pretty poor. It is possible to set a form into a resizing mode where a rectangle is shown on the screen that denotes the new window dimensions as the user resizes it, but the actual form doesn't resize until they let go of the mouse button?
Thanks!
Yes; this behavior is defined by one of the window styles, which you can turn on/off using the Control.SetStyles method. In particular, I think you want this:
myForm.SetStyle(ControlStyles.ResizeRedraw, false);
You could then hook the mousedown/resize/mouseup events and force the redraw to happen when you wanted.
You can also try turning on the double buffering style:
myForm.SetStyle(ControlStyles.DoubleBuffer, true);
See this article for details: http://msdn.microsoft.com/en-us/library/fkf25009(v=vs.100).aspx
Completely suppressing all drawing is not practical, the window frame painting is out of your direct control. Nor is it necessary, all you have to do is make your drawing fast when the form is being resized. Like this:
private bool fastRender;
protected override void OnResizeBegin(EventArgs e) {
fastRender = true;
base.OnResizeBegin(e);
}
protected override void OnResizeEnd(EventArgs e) {
base.OnResizeEnd(e);
fastRender = false;
this.Invalidate();
}
And check the fastRender variable in your Paint event handler, drawing only the minimum. Or nothing at all. If the actual delay is caused by a large number of controls then tackle that by making them invisible in ResizeBegin and visible again in ResizeEnd. Easy to do with a Panel. If it is caused by a controls that are docked or have the Anchor set so that they'll resize or move whenever the user resizes the window then you'll find Suspend/ResumeLayout useful.
Have you tried using the DoubleBuffer feature of a winform/control?
How about using ResizeEnd instead of Resize.
Open the window that is abnormally sized; then hold down the control key, hit the + key until you return to your required size. Good luck.

scroll bar TableLayoutPanel c#

I have a TableLayoutPanel that has several TableLayoutPanels inside it. The amount changes dynamically and will almost always be too many to be able to fit inside the form.I need it to have a scroll bar so I can view the entire component.
I have tried setting the autoscroll property on the main panel to true and docking it and/or setting a maximum size. What the controler does instead, is to try and fit ALL of the Panel inside the form therefore changing the size of its inside components and squeezing them all together instead of creating a scroll bar to scroll through my Panel.
Do you guys know what I might be doing wrong?
Thanks.
Jose
PS: I am using VS 2010
I had the same issue one day and found out that the problem was that I had a "MinimumSize" set to the TableLayoutPanel. That caused the control to keep a minimum height no matter what the Dock and/or Anchor constraints, preventing the AutoScroll feature to work properly. Since that feature is based on a simple check on the Y coordinates of all children controls of a control against that control's height, the scrollbar was not appearing despite the fact the the TableLayoutPanel's child controls were "disapearing" out of sight due to its parent control clip area.
So in other words, check the MinimumSize property of TableLayoutPanel and make sure it's empty.
Maybe this will help.
if it still doesn't work, try put a panel and then put the tableLayoutPanel into that panel. Set to true the autoScroll property of the panel.
I had the same thing happen on my project. All my controls were squished inside the TableLayoutPanel. In my case, I was rendering many of the controls as ColumnStyle.Percent. Switching this to ColumnStyle.Autosize was the fix I needed.
I assume you've set these properties as well, but just in case my TableLayoutPanels also use the following settings:
AutoSize = true;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
AutoScroll = true;
Late answer, but I've been fighting with a complex layout recently and was looking for a solution to get my scrolling working on a screen with far too many fields. Honestly, better design should avoid this problem 99% of the time but sometimes your hands are just tied.
The Problem
The problem seems to be that if you're nested too deeply with multiple grids, groups, and panels they stop reporting properly to parent controls that their contents have overflown the size of the current screen. Actually, the problem is probably that the controls are all set to dock fill and do fit within their parent control, so the top level control doesn't know that the contents of its great-great-great-great-grandchild controls don't fit.
Solution
The trick to fix this seems to be manually forcing the top most panel to scroll at a certain minimum size:
MainPanel.AutoSize = true;
MainPanel.AutoScrollMinSize = new Size(400, 500);
TableLayoutPanel scrolling is full of bugs.
The solution to make it work correctly is here.

Why does clicking in a text box cause an AutoScroll panel to scroll back to the top?

Finishing up a register form in a C# application and I noticed with the panel if I enable AutoScroll and then have a textbox that is below the scroll and click on it it jumps all the way back up to the top. Is there a way to fix this with some code or is it a propriety?
It's a little difficult for me to explain it in words, so here's a short video that shows the behavior.
I have had the same problem. I fixed it with this code in my panel:
protected override Point ScrollToControl(Control activeControl)
{
return this.AutoScrollPosition;
}
I had this exact problem. I had to remove the docking from my panels on the form and this fixed the problem.
Since apparently no one has seen this behavior before and could provide a quick answer, I opened up Visual Studio to try and reproduce what you describe.
I created a new WinForms project with a GroupBox containing a Panel whose AutoScroll property is set to "True". Then, I added two new GroupBox controls inside of the Panel, each containing two TextBox controls. The first embedded GroupBox is at the top of the form, entirely visible at startup; the second embedded GroupBox is at the bottom where it must be scrolled into view. This is equivalent to the design/layout that you have as best I can tell from your description and video.
However, when I run the project, scroll down to the second embedded GroupBox and select one of the TextBox controls that it contains, it performs exactly as expected. The TextBox control that I clicked on gets the focus, without scrolling the entire panel back up to the top. I can't seem to reproduce what you're seeing. If you could either tell me what I've done wrong in designing my test sample or post the smallest sample project needed to recreate the behavior you're experiencing, I might be able to help.
Otherwise, here are a few suggestions of things to investigate:
The tab order of the objects on your form. This really shouldn't be causing the behavior described because clicking on a control should set the focus to that control, regardless of its position in the tab order, and jumbling up the tab order multiple times in my sample project still doesn't appear to have the same effect. But I suppose it's worth a try anyway. In Design Mode, go to your "View" menu, and click "Tab Order". All of the controls that you can set the tab order for will have a little colored box at their top-left corner, indicating their tab order in each container. To set the tab order, click once on each of the controls in the natural order you want them to be focused.
Scour your code for any <Control>.Focus or <Control.Select> statements. Make sure that you don't have any validation code that's altering the tab order in any way during run-time. This could be causing focus to jump back to a control located near the top of your Panel, forcing it to auto-scroll to the new location.
Try to reproduce the behavior in a brand new, clean project. Ideally, create a new project in Visual Studio and lay out the controls the exact same way you have them in the project with which you're experiencing difficulties. This is the same thing I did, partly because I don't have your particular project to work with, and also because this is the best way to troubleshoot particularly tricky behavior. It's more likely there is some quirk to your design or source code that's causing this behavior, rather than some kind of bug in the controls themselves. But either way, this will let you know exactly where the problem is occurring, which will get you that much closer to a solution.
You can use TableLayoutPanel" instead of "Panel" to avoid scrollbar change its position.

Categories