简体   繁体   中英

C# Combo_box changes formatted string

Given:

String course = String.Format
(
    "{0}-{2} {1}  {3} {4} {5}", 
    c.course_ID.ToString().Trim(), 
    c.course_Name.Trim().PadRight(20), 
    c.Section_Num.ToString().Trim(),
    c.Start_Time.ToString().Trim(), 
    c.Quarter.Trim(), 
    c.Year.ToString().Trim()) 
);

I have this output:

"150-2 FF Test 11:59:00 Winter 2016"

"314-1 Test Course 11:59:00 Winter 2016"

However, when these are then added to a combobox, the second line is always two spaces longer than the previous line.

combo_box.Items.add(course);

"150-2 FF Test 11:59:00 Winter 2016"

"314-1 Test Course 11:59:00 Winter 2016"

Any suggestions on how to fix this?

The problem is one of font widths. The default font for a Combo box is MS Sans Serif, which is a variable-width (or proportional) font. Each character takes up a different amount of space.
固定宽度与可变宽度字体

Switching to a monospaced (or fixed-width) font such as Courier New should solve your problem:
更改组合框上的字体属性

Like so:
具有固定宽度字体的组合框

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM