Weston Odom
Weston Odom

Reputation: 311

Centering text on a button in a WinForms application

I have a simple Windows Forms application with a tabControl. I have 3 panels on the tabControl, each having 5 buttons. The text on first set of buttons is hard-coded, but the next set populates when you click one from the first group, and then the same thing happens again for the last group when you click one of the buttons from the second group. In the [Design] view I manually set the TextAlign property of each button to MiddleCenter. However, when I run the application the text on the middle set of buttons is never centered. It is always TopLeft aligned. I've tried changing the font size and even explicitly setting the TextAlign property every time I set button text programmatically, as follows:

private void setButtons(List<string> labels, Button[] buttons)
    {
        for (int i = 0; i < buttons.Count(); i++)
        {
            if (i < labels.Count)
            {
                buttons[i].Text = labels.ElementAt(i);
                buttons[i].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                buttons[i].Enabled = true;
            }
            else
            {
                buttons[i].Text = "";
                buttons[i].Enabled = false;
            }
        }
    }

This image shows the result: alignment issue

Does anyone have any ideas for what I'm missing?

Upvotes: 6

Views: 7314

Answers (4)

user7657800
user7657800

Reputation:

You can set the UseCompatibleTextRendering property to true, then use the TextAlign property.

Upvotes: 2

iato
iato

Reputation: 424

You can use the TextAlign from the Properties Menu and set it to MiddleCenter ...

If this does not work then the text you have for your button is larger than the actual button itself... to which you should either rescale your Font Size to a lower base size or a percent size of the actual button by using

btnFunction.Font = new Font(btnFunction.Font.Name, Convert.ToInt32(btnFunction.Height * 0.3333333333333333));

This would cause the button's font to be one third of the height of the button....

Upvotes: 0

Weston Odom
Weston Odom

Reputation: 311

The strings in the SQL table that were assigned to the middle column were actually nchar(50), not nvarchar(50), which explains the problem. I added .Trim() to the Text assignment and it looks great now.

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

Trim text which you are assign to button. Also you can refer label by index, without calling ElementAt

private void setButtons(List<string> labels, Button[] buttons)
{
    for (int i = 0; i < buttons.Count(); i++)
    {
        Button button = buttons[i];

        if (i < labels.Count)
        {
            button.Text = labels[i].Trim(); // trim text here
            // button.TextAlign = ContentAlignment.MiddleCenter;
            button.Enabled = true;
        }
        else
        {
            button.Text = "";
            button.Enabled = false;
        }
    }
}

Upvotes: 4

Related Questions