Click here to Skip to main content
15,888,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 set of variables. Which is reading some string data from a file. I want to display it to checkedlistbox with some proper alignment. first string should be left aligned and other one should be right aligned.

For example:
following is list of items to be displayed where [] indicates checkbox:
[] ABCDEFGHI             1233434
[] asfsad               2342dvdg
[] dfg2542323         dfhgdsfhdf
I want to have the strings aligned as above. Could you please help achieving this result?

What I have tried:

I tried with string.format. Also used Padleft and Padright methods.
Posted
Updated 31-Aug-20 3:11am
v2
Comments
[no name] 31-Aug-20 8:37am    
In case you like to solve that with padding you need to choose a 'fixed-width font'/'monospace font'. Monospaced font - Wikipedia[^]
Maciej Los 31-Aug-20 16:00pm    
The simplest way is to create custom control with 2 embeded controls.

1 solution

One of the solutions could be:
Given the text of checkboxlist item are from two different sources, havea predefined length of text (say 40) and then put strings from left to right (index 0) and put the another data string from end (index length). Fill in the middle left space with space.

This way, you would have a view as above.

PS: if you have defined lenghts of the two strings, it would be easier to optimize. Base here is to play with the length.

BTW, for above, you can make use of string concatenation and PadRight[^]/Padleft[^] methods with defined lengths.

UPDATE:
A quick attempt on .NET Fiddle: Fixed length of text can have padleft/padright setup[^]
It shows:
C#
String first ="firstone";
String second ="secondone";
String needed = first.PadRight(25,' ') + second.PadLeft(25, ' ');
Console.WriteLine(needed);
Console.WriteLine("lenght: "+ needed.Length);

//Output as:
firstone                                 secondone
lenght: 50
 
Share this answer
 
v5
Comments
[no name] 31-Aug-20 9:14am    
Works only for monospace fonts ;)
Sandeep Mewara 31-Aug-20 9:16am    
Hold - Googling whats monospace fonts :D
Sandeep Mewara 31-Aug-20 9:17am    
Ah right. Fixed real state. True.

Without starting with any lenght, how we can keep real state too part of it?
Sandeep Mewara 31-Aug-20 9:26am    
Have a look at this .NET fiddle: https://dotnetfiddle.net/7gpT9R

With fixed length, looks good - should be okay in any font.
[no name] 31-Aug-20 9:29am    
Try that e.g. with Arial and yyou will see it does not works. Your link shows a console app which what I know use by default a fix width font. But I can be wrong...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900