Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there any way to achieve minimum size of control with scroll bar when window is to small for that size AND at the same time have autosize of that control if window is greater than minimum size?
Anchors works great for resizing to greater size but not smaller (auto scroll don't appear), docking disables auto scroll bars.

Only way I have found it's to dock controls in parent, and add non visible panel in background with fixed size, and set auto scroll on parent to true. Than when user resize window to size smaller than fixed size of hidden panel scroll bars appears. Whe size of window is greater, docked controls resize to parent size.

Is there better 'non-hacking' solution?
Posted

1 solution

Your solution is really a "hack" which is absolutely not needed.

The real solution is a combination of properties of the child object and a parent panel, which should be of the the panel type derived from System.Windows.Forms.ScrollableControl, such as "regular" System.Windows.Forms.Panel.

First of all, this is not true that you cannot use Dock with chilren. You can use either Dock or Anchor, and I would advise to prefer docking, as anchoring is more manual in development and support and more likely causes flicker. So, I will show the code sample with docking. I will take care only about horizontal dimension:

C#
namespace AutoScrollApplication {
    using System.Windows.Forms;

    public class AutoScrollForm : Form {
        
        internal AutoScrollForm() {
            Panel pad = new Panel();
            pad.Dock = DockStyle.Fill;
            this.Controls.Add(pad);
            TextBox textBox = new TextBox();
            textBox.Dock = DockStyle.Top;
            pad.Controls.Add(textBox);
            pad.AutoScroll = true;
            // now, select 300 for limiting factor;
            // I hope in real code in won't be an immediate constant I put just for illustration:
            pad.AutoScrollMinSize = new System.Drawing.Size(300, 0);
        } //AutoScrollForm
    
        [System.STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new AutoScrollForm());
        } //Main

    } //class AutoScrollForm

} //namespace AutoScrollApplication


For the purpose of the demo, I avoided using the designer and put all the application in just one file. If you build it, it will show the exact effect you wanted.

That's it.

[EDIT]

Please see my warning about the inappropriate immediate constant 300 I put just for illustration of the technique. You could do better by using the current size of the window. For this purpose, use, for example:

C#
pad.AutoScrollMinSize =
                new System.Drawing.Size(this.ClientRectangle.Width, 0);
In this case, the scrolling will be set exactly on the boundary between two modes: when a window is first shown, the scroll bar is not visible. From this point, if you make your form wider, the child TextBox will grow in width, if you make if narrower, the scroll bar appears.

Isn't that nice?


Good luck,
—SA
 
Share this answer
 
v6
Comments
Espen Harlinn 7-May-12 17:12pm    
Good reply :-D
Sergey Alexandrovich Kryukov 7-May-12 17:38pm    
Thank you, Espen.
--SA
n.podbielski 12-Oct-12 8:04am    
I read your solution first time now. I will try it and let you now if this is working?
Sergey Alexandrovich Kryukov 12-Oct-12 11:53am    
Good; please do.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900