views:

252

answers:

4

Hello. I have this code that adjusts the width of a comboBox drop-down:

  private void comboBox_DropDown(object sender, EventArgs e)
  {
     ComboBox senderComboBox = (ComboBox)sender;
     int width = senderComboBox.DropDownWidth;
     Graphics g = senderComboBox.CreateGraphics();
     Font font = senderComboBox.Font;
     int vertScrollBarWidth =
         (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
         ? SystemInformation.VerticalScrollBarWidth : 0;
     int newWidth;

     foreach (string s in ((ComboBox)sender).Items)
     {
        newWidth = (int)g.MeasureString(s, font).Width
            + vertScrollBarWidth;

        if (width < newWidth)
        {
           width = newWidth;
        }
     }

     senderComboBox.DropDownWidth = width;
  }

It works great, except it expands the width of the drop-down to the right, whereas I would prefer it to expand to the left because the comboBox is located on the right side of my form. Any thoughts or suggestions you may have would be appreciated. Thanks.

+2  A: 

Ok, so .Anchor didn't work like I expected it to, so here's a completely new answer which does work, but I feel is kind of a hack, (but maybe it's a perfectly reasonable way to manage it):

int x = 10;           
comboBox1.Location = new Point(comboBox1.Location.X - x, comboBox1.Location.Y);
comboBox1.Width += x; 

This code pulls it back along the x-axis by 10 pixels, and then expands ComboBox1 by 10 pixels.

This works very smoothly for me. Does this work for you?

Pretzel
+1 haha this is clever.
Mike
I'm not sure I would use the word "clever", but thanks... :-) Somehow I still feel like there should be a more elegant way to do this. *shrugs* I guess that's WinForms for ya, right?
Pretzel
+1  A: 

I wrote up an article on CodeProject on how to hack the combo-box to give it a scroll bar to scroll horizontally. See here for the article.

tommieb75
+1 -- Whoa. That's pretty sweet, man. I'm not sure that's what the OP wants, but you'll get an upvote from me as it may be a better solution to his problem.
Pretzel
@Pretzel: Thanks! :)
tommieb75
@tommieb75: In fact, I'm favoriting this thread so I can come back and look at your code more closely later. I can see using it in another project... :)
Pretzel
A: 

You might want to look at placing the control within a container. For instance, create a FlowLayoutPanel with its FlowDirection property to RightToLeft. Place the ComboBox within the new panel. One benefit of this method is you may change the dimensions by any means and the control/container will behave as expected.

bleh
Thanks, bleh, but I tried setting the RightToLeft property, but it didn't give me the results I wanted.
Jim Fell
A: 

After much searching, it appears that this is actually along-standing problem that Microsoft has yet to address (big surprise). I've decided to re-arrange my layout to better-accomodate this feature-lack when I get the time, but for now, I'm just going to live with it. Thanks, everyone, for your input.

Jim Fell
@Jim: Did my solution not work for you? It's sort of "hack-ish" I'll admit, but it's entirely viable with no weird side-effects, in my experience.
Pretzel
Hi Pretzel. Unfortunately, no. Moving the control around while it's in use is not really a viable option for this application. I've rearranged the layout to work with Microsoft's limitted feature set on their combo-box. Thanks for the suggestion, though.
Jim Fell