C# Resize Form by Fixed Amount (listBox1.ItemHeight)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anthonyc1525
    New Member
    • Mar 2010
    • 3

    C# Resize Form by Fixed Amount (listBox1.ItemHeight)

    I have a c# windows form with a listbox in it, and i would like the form to be able to resize such that its height only increases in increments of listBox1.ItemHe ight. I think the way it would work is when you start resizing prevent it from resizing until the mouse has moved listBox1.ItemHe ight in the Y direction, then increase the Height by listBox1.ItemHe ight. At this point i believe the width can be resized freely.

    Normally i would just put a panel of the same color behind it to hide the fact that the listbox doesnt fill the whole area, but in this program that just wont look good. I know ive seen some commercial programs that do this, but i cant remember any to give you any ideas.

    I think i may need to use "WM_SIZING" , according to some posts i found about keeping aspect ratios.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Maybe I can offer another approach...

    If you tie into the ResizeEnd event, you can modify your form's height to always round to some multiple of your listbox height.

    Code:
            private void Form1_ResizeEnd(object sender, EventArgs e)
            {
                int height = this.Height;
                int height2 = (int)Math.Floor((double)height / 8.0 + 0.5) * 8;
    
                this.Height = height2;
            }
    If you don't mind it resizing by pixel and then snapping, it might work for you. It could be a bit easier than having it resize in increments (which I'm not sure how to do off the top of my head).

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Instead of forcing the form to resize in increments of the listbox, what about resizing the listbox to make use of the available space of the form? That is a bit more common. It should gracefully grow and shrink as the form does.

      Code:
              private void Form1_SizeChanged(object sender, EventArgs e)
              {
                  int Whitespace = this.listBox1.Location.X;// So to give equal spacing on 3 sides
                  listBox1.Size = new Size(
                      this.ClientSize.Width - listBox1.Location.X - Whitespace, 
                      this.ClientSize.Height - listBox1.Location.Y - Whitespace);
              }
      Last edited by tlhintoq; Mar 4 '10, 10:58 PM. Reason: Added code

      Comment

      • anthonyc1525
        New Member
        • Mar 2010
        • 3

        #4
        Thanks for the responses.

        tlhintoq, what you said does not work because no matter what size a listbox is set to, its height will still only show up as a multiple of its itemheight. (And yes, I tried your code just to be sure and it didn’t work). This is apparent if you just take a blank form and put a listbox on it and set it to Dock.Fill and you will see what im talking about.

        GaryTexmo, I never thought of doing that, thanks for the advice. I ended up doing something like this but I realized my listbox was very slow at resizing normally (since I override the DrawItem), so I just didn’t size the listbox until ResizeEnd as well, and I put a similar looking blank listbox in front anytime I resize.

        I’m still open to more suggestions if someone has something better in mind.

        The form is basically looks like a large listbox that fills all but a bottom docked textbox. And like I said, the way the form is setup it just doesn’t look good unless the listbox is the correct height, even if I do put something the same color behind it, because then it looks like a large padding on top or bottom of the list box. (Its a hell of a lot more technical than that, but that gives you a good approximation. If you have ever seen a ti-89/92 calculator, the UI looks a lot like the screen on that)

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          tlhintoq, what you said does not work because no matter what size a listbox is set to, its height will still only show up as a multiple of its itemheight.
          My mistake. I thought you were trying to do something like this:

          [IMGNOTHUMB]http://files.me.com/tlhintoq/mqg7qr[/IMGNOTHUMB]
          [IMGNOTHUMB]http://files.me.com/tlhintoq/brnmkb[/IMGNOTHUMB]
          [IMGNOTHUMB]http://files.me.com/tlhintoq/535dmw[/IMGNOTHUMB]

          If you are trying to be absolutely precise about the distance from the bottom of the listbox to the bottom of the form, then you can adjust your form height after the lisbox height is set (since it only sizes in increments of ItemHeight as you said)

          Code:
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Text;
          using System.Windows.Forms;
          
          namespace JustChecking
          {
              public partial class Form1 : Form
              {
                  private int Whitespace = 10;// default value
                  public Form1()
                  {
                      InitializeComponent();
                  }
          
                  private void Form1_SizeChanged(object sender, EventArgs e)
                  {
                      int AbsoluteMargin = 15;
                      listBox1.Size = new Size(
                          this.ClientSize.Width - listBox1.Location.X - Whitespace,
                          this.ClientSize.Height - listBox1.Location.Y - Whitespace);
                  }
          
                  private void Form1_Load(object sender, EventArgs e)
                  {
                      for (int Index = 0; Index < 100; Index++)
                      {
                          listBox1.Items.Add(string.Format("Item {0}", Index.ToString()));
                      }
          
                      Whitespace = this.listBox1.Location.X;// So to give equal spacing on 3 sides
                  }
          
                  private void Form1_ResizeEnd(object sender, EventArgs e)
                  {
                      int BorderCompensation = this.Height - this.ClientSize.Height;
                      this.Height = listBox1.Bounds.Bottom + BorderCompensation + Whitespace;
                  }
              }
          }

          Comment

          • EARNEST
            New Member
            • Feb 2010
            • 128

            #6
            Similar issue, asking here, instead of creating other minor thread.
            I will have a window form with containers (groupbox, panels etc), when the size of the main window form changes, is there a way of changing the size of all the containers there? Or only thru certain formulas and calculations.

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Earnest, have you tried setting the anchors? That works for most cases... if not, you'll have to change the locations/sizes manually when the resize event is triggered. Your issue is slightly easier than the OP's as you don't need to change the form size, just that of the components based on the form's size :)

              Comment

              • EARNEST
                New Member
                • Feb 2010
                • 128

                #8
                http://bytes.com/profile/239261/garytexmo/,
                thanks man. I will check it, so far it works...

                Comment

                Working...