Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Tricky problem. I could not find a good fix for this, merely a workaround. Add a new class and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.</p> <p>The workaround is not a very good one. The problem is that the dropdown window will ignore the attempt to move it until the drop-down animation is complete. The visual effect is not great, you'll see the window drop down, then jump to the left. I don't know an other way to slap it over the head, maybe somebody else does.</p> <p><strong>C# version:</strong></p> <pre><code>using System; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; public class MyComboBox : ComboBox { protected override void OnDropDown(EventArgs e) { // Is dropdown off the right side of the screen? Point pos = this.PointToScreen(this.Location); Screen scr = Screen.FromPoint(pos); if (scr.WorkingArea.Right &lt; pos.X + this.DropDownWidth) { this.BeginInvoke(new Action(() =&gt; { // Retrieve handle to dropdown list COMBOBOXINFO info = new COMBOBOXINFO(); info.cbSize = Marshal.SizeOf(info); SendMessageCb(this.Handle, 0x164, IntPtr.Zero, out info); // Move the dropdown window RECT rc; GetWindowRect(info.hwndList, out rc); int x = scr.WorkingArea.Right - (rc.Right - rc.Left); SetWindowPos(info.hwndList, IntPtr.Zero, x, rc.Top, 0, 0, 5); })); } base.OnDropDown(e); } // P/Invoke declarations private struct COMBOBOXINFO { public Int32 cbSize; public RECT rcItem, rcButton; public int buttonState; public IntPtr hwndCombo, hwndEdit, hwndList; } private struct RECT { public int Left, Top, Right, Bottom; } [DllImport("user32.dll", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode)] private static extern IntPtr SendMessageCb(IntPtr hWnd, int msg, IntPtr wp, out COMBOBOXINFO lp); [DllImport("user32.dll")] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr after, int x, int y, int cx, int cy, int flags); [DllImport("user32.dll")] private static extern bool GetWindowRect(IntPtr hWnd, out RECT rc); } </code></pre> <p><strong>VB.Net version:</strong></p> <pre><code>Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.Runtime.InteropServices Public Class MyComboBox Inherits ComboBox 'P/Invoke declarations Private Structure COMBOBOXINFO Public cbSize As Int32 Public rcItem As RECT Public rcButton As RECT Public buttonState As Integer Public hwndCombo As IntPtr Public hwndEdit As IntPtr Public hwndList As IntPtr End Structure Private Structure RECT Public Left As Integer Public Top As Integer Public Right As Integer Public Bottom As Integer End Structure &lt;DllImport("user32.dll", EntryPoint:="SendMessageW", CharSet:=CharSet.Unicode)&gt; Private Shared Function SendMessageCb(hWnd As IntPtr, msg As Integer, wp As IntPtr, ByRef lp As COMBOBOXINFO) As IntPtr End Function &lt;DllImport("user32.dll")&gt; Private Shared Function SetWindowPos(hWnd As IntPtr, after As IntPtr, x As Integer, y As Integer, cx As Integer, cy As Integer, flags As Integer) As Boolean End Function &lt;DllImport("user32.dll")&gt; Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef rc As RECT) As Boolean End Function Protected Overrides Sub OnDropDown(e As EventArgs) ' Is dropdown off the right side of the screen? Dim pos As Point = Me.PointToScreen(Me.Location) Dim scr As Screen = Screen.FromPoint(pos) If (scr.WorkingArea.Right &lt; pos.X + Me.DropDownWidth) Then Me.BeginInvoke(New Action(Sub() 'Retrieve handle to dropdown list Dim info As COMBOBOXINFO = New COMBOBOXINFO() info.cbSize = Marshal.SizeOf(info) SendMessageCb(Me.Handle, &amp;H164, IntPtr.Zero, info) 'Move the dropdown window Dim rc As RECT GetWindowRect(info.hwndList, rc) Dim x As Integer = scr.WorkingArea.Right - (rc.Right - rc.Left) SetWindowPos(info.hwndList, IntPtr.Zero, x, rc.Top, 0, 0, 5) End Sub)) End If MyBase.OnDropDown(e) End Sub End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload