Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Combobox (winforms) how can I make the values appear as percentage

Tags:

c#

winforms

I've got a combo box with a set of values( 5, 10,15, 20).

When the user goes to select a value I want them to appear as percentages (5%, 10%, 15%, 20%).

I was playing with format string using a value of ##% but that did not work.

like image 252
codingguy3000 Avatar asked Feb 27 '23 23:02

codingguy3000


1 Answers

FormatString should work, but it will multiply the number by 100. Probably want to add a 0 in front of your string to handle 0%.

This code worked for me.

    private void Form1_Load(object sender, EventArgs e)
    {
        this.comboBox1.FormatString = "##0%";
        comboBox1.Items.Add(0);
        comboBox1.Items.Add(0.33);
        comboBox1.Items.Add(0.50);
        comboBox1.Items.Add(0.67);
        comboBox1.Items.Add(1);
    }
like image 159
doobop Avatar answered May 10 '23 17:05

doobop