C # Combobox (winforms), how can I make percent values

I have a combo box with a set of values ​​(5, 10, 15, 20).

When the user goes to the selection of the value, I want them to be displayed as a percentage (5%, 10%, 15%, 20%).

I played with the format string using the value ##%, but that didn't work.

+4
source share
4 answers

You can set the value to 5, 10, 15, 20, and the displayed item is 5%, 10%, 15%, etc.

+1
source

FormatString should work, but it will multiply the number by 100. You may need to add 0 in front of your line 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); } 
+5
source

You can create your own combo box based on a hidden text box, or format them manually. If your user cannot enter their own values, simply enter them into a pre-formatted one. If they can, then manually format them when the combo box fires this event.

+1
source

Hmmm, this is a very strange problem, I just could not solve it. If you want to show normal numbers in your combo box, and the last (after selection), change the text. I did it this way (looks really funny) and it solves this problem :)

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { Thread th = new Thread(() => comboBox1.Invoke((Action)(() => comboBox1.Text += @"%"))){ IsBackground = true }; th.Start(); } 
+1
source

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


All Articles