bvg bvg - 3 years ago 171
C# Question

How to pad two strings on the same line in a checkedListBox C#?

I have multiple strings that look like this:

"MyApp.exe"
and others like this:
"0x1234567A"
.

The dimensions vary, of course.

I want to arrange them on a single line in a
checkedListBox
.

I use the following code:

processes_checkedListBox.Items.Add(element[1] + element[2], false);


element[1]
having
"MyApp.exe"
and
element[2]
"0x1234567A"
.

So if I apply that code the result is something like this, obviously:

MyApp.exe0x1234567A
MyOtherApp.exe0x1234567B


I tried with this:

processes_checkedListBox.Items.Add(element[1] + element[2].PadLeft(5,' '), false);


The result on this case looks like this:

MyApp.exe 0x1234567A
MyOtherApp.exe 0x1234567B


If I apply the padding to the left, the result is identical with the first one.
Likewise if I apply padding to the right on the first element.
The font is the default checkedListBox one, nothing changed,
The padding is 40 spaces, I've inserted above 5 just as an example.

The desired result should be of course:

MyApp.exe 0x1234567A
MyOtherApp.exe 0x1234567B


I don't know how to do it, any suggestions, please?
(.NET 4.5 Framework and MVS 2015 - Windows Form Application)

LATER EDIT

private void Display_Processes_Button_Click(object sender, EventArgs e)
{
processes_checkedListBox.Items.Clear();

string test = File.ReadAllText("test.txt");
string[] testArray = test.Split('\n');

for (int i = 3; i < testArray.Length-1; ++i)
{
string[] element = testArray[i].Split('|');
element[1] = element[1].Trim();
element[2] = element[2].Trim();

processes_checkedListBox.Items.Add(string.Format("{0,-20} {1,-20}",element[1] , element[2]), false);
}
}


This is my actual code, I am reading the info from a file that contains:

+----------------------------------------+-----------+
| Process Name | PID |
+----------------------------------------+-----------+
| securityAccessModule.exe | 0x127F003A|
| CMD.EXE | 0x77430012|
| ps.exe | 0x77010062|
+----------------------------------------+-----------+


I still can't make it work, although I've tried your solutions. I see that in console it works fine. :(

Answer Source

Looks like you need to have a monospaced font.

Try the following code:

var txt = @"+----------------------------------------+-----------+
            |             Process Name               | PID       |
            +----------------------------------------+-----------+
            | securityAccessModule.exe               | 0x127F003A|
            | CMD.EXE                                | 0x77430012|
            | ps.exe                                 | 0x77010062|
            +----------------------------------------+-----------+";

processes_checkedListBox.Font = new Font(new FontFamily("Consolas"), processes_checkedListBox.Font.Size);
processes_checkedListBox.Items.Clear();

string test = txt;
var testArray = test.Split('\n');

for (int i = 3; i < testArray.Length - 1; ++i)
{
    string[] element = testArray[i].Split('|');
    element[1] = element[1].Trim();
    element[2] = element[2].Trim();

    processes_checkedListBox.Items.Add(string.Format("{0,-25} {1,-25}", element[1], element[2]), false);
}

This shows as follows:

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download