-1

My code is C# windows form
I have a file with data:

David One And Two/Three
Alex One Two Four And Five/Six
Amanda Two Seven/Ten
Micheal Seven/Nine

and trying to have them in array like

string[] Names = File.ReadAllLines("C:\Students\Name.txt", Encoding.Default);


and have them in Group of Radio Buttons

RadioButton[] n = new RadioButton[Names.Length];

for (int i = 0; i < Names.Length; i++)
 {
   n[i] = new RadioButton();
   n[i].Text = Names[i];
   n[i].Location = new Point(10, 10 + i * 20);
   groupBox1.Controls.Add(n[i]);
 }

But it shows as my attached image
I tried without Encoding.Default and Encoding.UTF8 but the same problem.
What is I'am doing wrong? Please see my image and help me. Thank you in advance!

enter image description here

Jonas Willander
  • 432
  • 3
  • 9
  • 29
  • It sounds like you're asking two questions here. Didn't you already ask the one about reading the file just yesterday? Is that one gone? I never saw an answer about the format of the file's line endings: `lf` or `crlf`? – clarkitect Nov 09 '16 at 19:02
  • @clarkitect thank you for quick respons. I'am sorry , yes it's allmost the same question actually I want my array strings in a radiobutton. By the way What it the meaning of lf or crlf? What do you mean by that? Sorry I'am new for thos short words.. ;) – Jonas Willander Nov 09 '16 at 19:09
  • `lf` is short for "line feed" and `cr` is short for "carriage return" which is a really old-fashioned name for "hard return." The characters within the file that terminate each "line" of the file. We haven't really seen the actual file in your example because it's pasted as plain text rather than code (or a link to pastebin or something). The result you're seeing puts the contents of the file into question. The other possibility is that the radio button's label is interpreting that `/` character somehow. – clarkitect Nov 09 '16 at 19:17

1 Answers1

0

I'm guessing your file contains empty lines at the end. You can try to remove them before creating a radio button:

for (int i = 0; i < Names.Length; i++)
 {
   var name = Names[i];
   if(name.Trim() == string.Empty) continue;

   n[i] = new RadioButton();
   n[i].Text = Names[i];
   n[i].Location = new Point(10, 10 + i * 20);
   groupBox1.Controls.Add(n[i]);
 }

If your text is trimmed you can try to increase the width of the control:

n[i].Width = 300; // Put a value which will show the entire text
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162