how to create a multiline combobox

Posted by: changing on 8 September 2017, 3:25 pm EST

  • Posted 8 September 2017, 3:25 pm EST

    I have a long string to be displayed in a combobox. But Multiline and WordWrap are not avilibale by ComboBoxCellType.
    I tried to create a new class using ComboBoxCellType, IWordWrapSupport, IMultilineSupport. But when I set combo.Multiline = true. It caused a System.StackOverflowException.

    How can I create a multiline combobox?

    BasicComboBoxCellType combo = new BasicComboBoxCellType();
    combo.Multiline = true;

    class BasicComboBoxCellType : ComboBoxCellType, IWordWrapSupport, IMultilineSupport
    {

    public bool Multiline
    {
    get
    {
    return Multiline;
    }

    set
    {
    Multiline = value;
    }
    }

    public bool WordWrap
    {
    get
    {
    return WordWrap;
    }

    set
    {

    WordWrap = value;
    }
    }
    }
  • Marked as Answer

    Replied 8 September 2017, 3:25 pm EST

    Hi,

    You can create a custom ComboBoxCellType and override the PaintCell() function to wrap the text in Combo cell. Here is the code to do that:
    public class myComboCellType : ComboBoxCellType
    {
    public override void PaintCell(Graphics g, Rectangle r, FarPoint.Win.Spread.Appearance appearance, object value, bool isSelected, bool isLocked, float zoomFactor)
    {
    SolidBrush brushBack = new SolidBrush(appearance.BackColor);
    SolidBrush brushFore = new SolidBrush(appearance.ForeColor);
    StringFormat objStringFormat = new StringFormat();
    RectangleF rectfText = default(RectangleF);

    Rectangle rectButton = default(Rectangle);
    g.FillRectangle(brushBack, r);
    rectButton = new Rectangle(r.Right - 17, r.Y, 17, r.Height);
    ControlPaint.DrawComboButton(g, rectButton, ButtonState.Normal);
    if (value!=null)
    {
    objStringFormat.Alignment = StringAlignment.Near;
    objStringFormat.LineAlignment = StringAlignment.Near;

    rectfText = new RectangleF(r.X, r.Y, r.Width - 18, r.Height);
    g.DrawString(value.ToString(), appearance.Font, brushFore, rectfText, objStringFormat);
    }

    //base.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor);
    }

    }


    Thanks,
    Deepak Sharma
  • Replied 8 September 2017, 3:25 pm EST

    Thank you very much DeepakSharma14. Your class works very well.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels