6
votes

I have a Form that has a combobox on it.

The combobox is set DropDownList. These drop down items are a descriptive form of an object. This means they can get quite long. The position of the combobox on the screen means when the drop down list is displayed it doesn't all fit on the screen. Some of it is chopped off by the right edge of the screen.

I can't move the combobox.

Is their some way I can move the drop down list part of the control. Perhaps centre it under the control ?

Update

I've attached a screenshot. You can see the form here -

enter image description here

When entering transactions the user fills in the form and clicks Save. A number of the transactions that will be entered for any client though will be recurring transactions. These can be saved to the favourites. The drop down box lists the currently saved favourites and when one is selected the program automatically fills in the transaction fields.

Screenshot 2 showing the whole program and the combobox list running out of space.

enter image description here

I realise from the screenshot I could move the form but I like to keep the forms for entering transactions centered on the screen.

I may have to look at other options for the interface.

Thanks,

4
suggestion: why not use a drop-down with simpler names/lines, and a multiline text box which is populated with the full description when user selects one entry in the combobox. This way, the text box can hold all of the text, and user is happy :)scrat.squirrel
Looks like you have a similar problem to this stackoverflow.com/questions/2395747/combo-box-dropdown-positionkavun
How about using a single column ListBox or ListView that provides scroll bars. Achieving this not easy with ComboBox.CharithJ
@Kavun. The code in the link works nicely on .Net 4.0 But I can't get it to compile here on 2.0user476683
@woohoo. I've added an update to the original Q. That's a nice solution but I'm not sure how I could incorporate it into the current interface.user476683

4 Answers

2
votes

maybe you should create your own comboBox, as shown here:

http://msdn.microsoft.com/en-us/library/ms996411

0
votes

Sorry for late posting :-). Yes You can do that. But you need to create a custom ComboBox and override the WndProc method of base ComboBox;

Its like this;

System.Runtime.InteropServices

private const int SWP_NOSIZE = 0x1;
private const int WM_CTLCOLORLISTBOX = 0x0134;

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int
X, int Y, int cx, int cy, uint uFlags);

protected override void WndProc(ref Message m)
{
     if (m.Msg == WM_CTLCOLORLISTBOX)
     {
         // Make sure we are inbounds of the screen
         int left = this.PointToScreen(new Point(0, 0)).X;

         //Only do this if the dropdown is going off right edge of screen
         if (this.DropDownWidth > Screen.PrimaryScreen.WorkingArea.Width - left)
         {
            // Get the current combo position and size
            Rectangle comboRect = this.RectangleToScreen(this.ClientRectangle);

            int dropHeight = 0;
            int topOfDropDown = 0;
            int leftOfDropDown = 0;

            //Calculate dropped list height
            for (int i = 0; (i < this.Items.Count && i < this.MaxDropDownItems); i++)
            {
                dropHeight += this.ItemHeight;
            }

            //Set top position of the dropped list if 
            //it goes off the bottom of the screen
            if (dropHeight > Screen.PrimaryScreen.WorkingArea.Height -
                   this.PointToScreen(new Point(0, 0)).Y)
            {
                topOfDropDown = comboRect.Top - dropHeight - 2;
            }
            else
            {
                topOfDropDown = comboRect.Bottom;
            }

            //Calculate shifted left position
            leftOfDropDown = comboRect.Left - (this.DropDownWidth -
                   (Screen.PrimaryScreen.WorkingArea.Width - left));
            //when using the SWP_NOSIZE flag, cx and cy params are ignored
            SetWindowPos(m.LParam,
                         IntPtr.Zero,
                         leftOfDropDown,
                         topOfDropDown,
                         0,
                         0,
                         SWP_NOSIZE);
          }
      }

      base.WndProc(ref m);
}

The code is obtain from a MSDN article Building a Better ComboBox

0
votes

Did you try setting in the designer

Combobox.Anchor = Left | Right
0
votes

Try setting the DropdownWidth of the combo.