Setting ComboBox DropDown Width in C #

I have this code that adjusts the width of a comboBox dropdown:

  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 that it extends the width of the drop-down list to the right, while I prefer it to expand to the left because the comboBox is on the right side of my form. Any thoughts or suggestions you might have. Thank.

+3
source share
4 answers

, , Microsoft ( ). , - , , . , , .

0

, . , , , , , (, , ):

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

10 , ComboBox1 10 .

. ?

+3

CodeProject , , . . .

+2

You might want to take a look at the placement of the control in the container. For example, create a FlowLayoutPanel with the FlowDirection property for RightToLeft. Place the ComboBox in a new panel. One of the advantages of this method is that you can resize in any way and the control / container will behave as expected.

0
source

Source: https://habr.com/ru/post/1746473/


All Articles