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.
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);
}
You could set the value to 5, 10, 15, 20 and the display member as 5%, 10%, 15% etc
You could either create your own combo box based off of the masked text box control, or format them manually. If your user can't type in their own values, just enter them in pre-formatted. If they can, then manually format them when the combo box raises it's changed event.
Hmmm it's very strange problem, I could not solve this in any way. If you want to show normal numbers in your combobox and latter(after selection) change it's text. I did it in this way (looks really funny), and it does solve this problem :)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Thread th = new Thread(() => comboBox1.Invoke((Action)(() => comboBox1.Text += @"%"))){ IsBackground = true };
th.Start();
}
精彩评论