473,322 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.ItemHeight. I think the way it would work is when you start resizing prevent it from resizing until the mouse has moved listBox1.ItemHeight in the Y direction, then increase the Height by listBox1.ItemHeight. 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.
Mar 4 '10 #1
7 5070
GaryTexmo
1,501 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1.         private void Form1_ResizeEnd(object sender, EventArgs e)
  2.         {
  3.             int height = this.Height;
  4.             int height2 = (int)Math.Floor((double)height / 8.0 + 0.5) * 8;
  5.  
  6.             this.Height = height2;
  7.         }
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).
Mar 4 '10 #2
tlhintoq
3,525 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1.         private void Form1_SizeChanged(object sender, EventArgs e)
  2.         {
  3.             int Whitespace = this.listBox1.Location.X;// So to give equal spacing on 3 sides
  4.             listBox1.Size = new Size(
  5.                 this.ClientSize.Width - listBox1.Location.X - Whitespace, 
  6.                 this.ClientSize.Height - listBox1.Location.Y - Whitespace);
  7.         }
  8.  
Mar 4 '10 #3
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)
Mar 5 '10 #4
tlhintoq
3,525 Expert 2GB
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:





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)

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace JustChecking
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         private int Whitespace = 10;// default value
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void Form1_SizeChanged(object sender, EventArgs e)
  20.         {
  21.             int AbsoluteMargin = 15;
  22.             listBox1.Size = new Size(
  23.                 this.ClientSize.Width - listBox1.Location.X - Whitespace,
  24.                 this.ClientSize.Height - listBox1.Location.Y - Whitespace);
  25.         }
  26.  
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.             for (int Index = 0; Index < 100; Index++)
  30.             {
  31.                 listBox1.Items.Add(string.Format("Item {0}", Index.ToString()));
  32.             }
  33.  
  34.             Whitespace = this.listBox1.Location.X;// So to give equal spacing on 3 sides
  35.         }
  36.  
  37.         private void Form1_ResizeEnd(object sender, EventArgs e)
  38.         {
  39.             int BorderCompensation = this.Height - this.ClientSize.Height;
  40.             this.Height = listBox1.Bounds.Bottom + BorderCompensation + Whitespace;
  41.         }
  42.     }
  43. }
Mar 5 '10 #5
EARNEST
128 100+
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.
Mar 5 '10 #6
GaryTexmo
1,501 Expert 1GB
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 :)
Mar 5 '10 #7
EARNEST
128 100+
http://bytes.com/profile/239261/garytexmo/,
thanks man. I will check it, so far it works...
Mar 5 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: David Hayes | last post by:
juglesh <juglesh@nospamRadioKDUG.com> wrote in "Re: how to maximize the browser window that fits the monitor size?" (Saturday, January 01, 2005 3:12 AM): > > >I want to maximize the browser...
2
by: baret bonden | last post by:
Trying to return a selected listbox item to another form .tried lots of ways; defining public variables and passing those as well as textboxes ..I' m able to display the chosen item on it's form...
1
by: James Arnold | last post by:
How is it possible to add 'fixed' jumps when resizing a control? For example, the listbox will only allow you to resize it so a full row will be displayed. I have a segmented progress bar that...
8
by: nirdeshonline | last post by:
Hi, I have added a simple listbox in windows form under c# 2.0. It contains a collection of approx 10 strings as list items. Now when i resize the form whole listbox flickers. Please tell me...
11
by: Ajith Menon | last post by:
I have created a windows application in which the form needs to be resized on the MouseMove event. The windows resize function takes a lot of CPU cycles. And as the resize function is called on the...
0
by: James Arnold | last post by:
Can someone please explain how I can implement the IntegralHeight and ItemHeight properties (found in listbox) into a usercontrol? For example, the listbox will only allow you to resize it so a...
3
by: Danny Ni | last post by:
Hi, I am looking for a way to display images with different aspect ratio into frames with fixed width and height, the problem is some images will look distorted if they are forced into fixed...
1
by: Bob Alston | last post by:
I have a system where many subforms are used. Often the size of the subform had to be larger than could be displayed without scrolling. I set the height of the subform to the typical height...
4
by: John | last post by:
Hi I have a form with a couple of text boxes and a list box. The user is able to type text in text boxes and add elements to the list box. My question is; how can I persist the values in these...
2
by: null | last post by:
Hello I have a listbox in my form, which I want to clear if the form's hieght changes. I am having difficulty with that, as it seems when the form is minimised the height changes, this is not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.