Reputation: 156
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. :(
Upvotes: 3
Views: 456
Reputation: 14476
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:
Upvotes: 3
Reputation: 596
String.Format("{0,-20}{1}", element[1], element[2]);
https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx
Upvotes: 2
Reputation: 489
Firs you need find element from index =0 with maximum length. Maximum length is for this MyOtherApp.exe and it has 14 characters. Let say
var lngth = 14;
Then you need to do PadRight (put + 1 to have one space extra) on the first element to all list items:
processes_checkedListBox.Items.Add(element[1].PadRight(lngth+1,' ') + element[2], false);
PadRight = 5 do nothing becouse it is total length of the string which will be more than 5 in this example.
Upvotes: 0
Reputation: 5143
Using Tab \t
:
class Program
{
static void Main(string[] args)
{
StringBuilder builder = new StringBuilder();
List<Entry> entries = new List<Entry>();
entries.Add(new Entry()
{
App = "MyApp.exe",
Value = "0x1234567A"
});
entries.Add(new Entry()
{
App = "MyOtherApp.exe",
Value = "0x1234567B"
});
foreach (Entry entry in entries)
{
//append multiple tabs here - the longer your appname is.
builder.AppendLine(string.Format("{0}\t{1}", entry.App, entry.Value));
}
Console.Write(builder.ToString());
Console.ReadLine();
}
}
class Entry
{
public string App { get; set; }
public string Value { get; set; }
}
Output:
MyApp.exe 0x1234567A
MyOtherApp.exe 0x1234567B
Upvotes: 0
Reputation: 14476
You can do this with string formatting:
var items1 = new []{"MyApp.exe", "0x1234567A" };
var items2 = new []{"MyAwesomeApp.exe", "0x1234567B"};
var items3 = new []{ "MyApp3.exe", "0x1234567C"};
Console.WriteLine(string.format("{0,-20} {1,-20}", items1));
Console.WriteLine("{0,-20} {1,-20}", items2);
Console.WriteLine("{0,-20} {1,-20}", items3);
This will give you
MyApp.exe 0x1234567A
MyAwesomeApp.exe 0x1234567B
MyApp3.exe 0x1234567C
Upvotes: 1
Reputation: 3267
Here's an answer using PadRight
string s1= "MyApp.exe";
string s2= "0x1234567A";
string s3= "MyOtherApp.exe";
string s4= "0x1234567B";
Console.WriteLine(s1.PadRight(20,' ') + s2);
Console.WriteLine(s3.PadRight(20,' ') + s4);
//melya's answer
Console.WriteLine(String.Format("{0,-20}{1}", s1, s2));
Console.WriteLine(String.Format("{0,-20}{1}", s3, s4));
Output: Both will lead to same output:
MyApp.exe 0x1234567A
MyOtherApp.exe 0x1234567B
Check it on Fiddle
@melya's answer is also good.!
Upvotes: 0
Reputation: 77063
I think you want the results to have the same total length with not less than 5 space in between, but the closer to 5 spaces, the better. Due to this fact, you will need to find the maximum total length of app name and hexa. Let's suppose it is n. In this case, the length of all texts will be n + 5. For each pair, calculate the difference between n + 5 and the sum of the length of the file name and hexa. The result will be the amount of space you will need to put between left and right.
Upvotes: 1