Align text in Combobox

I want to align my text in the combo box so that it shows in the center of the combo box how to do this, and you can see that the combo box is used by default when it is in focus, how can I remove this border as well Please solve my two problems. Thanks.

+9
source share
7 answers

This is not supported for ComboBox. The exact reasons are lost in the fog of time, ComboBox has existed since the beginning of the nineties, but, of course, has something to do with the awkwardness of receiving text in a text field in order to line up the text in a drop-down list. A custom drawing with DrawItem also cannot solve it, which affects only the appearance of the drop-down elements.

As a possible workaround, perhaps you can do something outlandish, for example, fill the lines of elements with spaces so that they look in the center. You will need TextRenderer.MeasureText () to find out how many spaces you need to add for each element.

The β€œborder” you are talking about is not a border, it is a focus rectangle. You also cannot get rid of this: Windows refuses to create a user interface that will not display the control with focus. Users who prefer a keyboard over a mouse take care of this. There is no workaround for this.

+19
source

This article will help you: http://blog.michaelgillson.org/2010/05/18/left-right-center-where-do-you-align/

The trick is to set DrawMode -Property from ComboBox to OwnerDrawFixed , as well as subscribe to its DrawItem event.

Your event should contain the following code:

 // Allow Combo Box to center aligned private void cbxDesign_DrawItem(object sender, DrawItemEventArgs e) { // By using Sender, one method could handle multiple ComboBoxes ComboBox cbx = sender as ComboBox; if (cbx != null) { // Always draw the background e.DrawBackground(); // Drawing one of the items? if (e.Index >= 0) { // Set the string alignment. Choices are Center, Near and Far StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; // Set the Brush to ComboBox ForeColor to maintain any ComboBox color settings // Assumes Brush is solid Brush brush = new SolidBrush(cbx.ForeColor); // If drawing highlighted selection, change brush if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) brush = SystemBrushes.HighlightText; // Draw the string e.Graphics.DrawString(cbx.Items[e.Index].ToString(), cbx.Font, brush, e.Bounds, sf); } } } 

ComboBox-Preview

To align the elements correctly, you can simply replace StringAlignment.Center with StringAlignment.Far .

+19
source

set the RightToLeft property to true. It does NOT cancel the character sequence. This is true only for justification.

+6
source

You cannot do this in a special window list box

+4
source

Winforms is pretty tough when it comes to customizing controls. If you are looking for a more personalized user interface, I recommend that you consider creating a WPF application that allows you to define user controls. This will require some work, so this is just what you want to do if you really find it necessary. Here is a decent site to get you started http://www.wpftutorial.net/

+2
source

you can do something like this by adding space in front of the Display element in your request

eg:

 combobox1.DataSource = Query(Select col1 , (' '+col2) as Col2 from tableName) combobox1.DisplayMember = "Col2"; combobox1.ValueMember = "col1"; 
0
source

Just wanted to add to what @OhBeWise said, for Telerik winrik forms it should look like this: this.comboBox.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;

0
source

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


All Articles