Right justified combobox in C#

后端 未结 3 1604
独厮守ぢ
独厮守ぢ 2021-01-12 12:07

By default the items in the C# Combobox are left aligned. Are there any options available to change this justification apart from overriding DrawItem method and setting the

3条回答
  •  隐瞒了意图╮
    2021-01-12 12:38

    You could just set the control style to RightToLeft = RightToLeft.Yes if you don't mind the drop widget on the other side as well.

    or

    set DrawMode = OwnerDrawFixed; and hook the DrawItem event, then something like

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == -1)
                return;
            ComboBox combo = ((ComboBox) sender);
            using (SolidBrush brush = new SolidBrush(e.ForeColor))
            {
                e.DrawBackground();
                e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, brush, e.Bounds, new StringFormat(StringFormatFlags.DirectionRightToLeft));
                e.DrawFocusRectangle();
            }
        }
    

提交回复
热议问题