Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SO guys I need your help I have a file named Drinks.txt and an that file I have for example 4 lines (it could be more ) I need to make 4 buttons visible or to create them and to read each line and place it as button text

What I have tried:

for (int i = 0; i < File.ReadLines(@"drinks.txt").Count(); i++)
{
      //Add button right here
}
int i =   File.ReadLines(@Drinks.txt").Count();

           toolStripLabel6.Text=i.ToString();
           toolStripLabel6.Visible = true;

on the second one i was trying to get the lines count to the label and create the buttons from that count
but this is not working what do I do or is it easier to do this with QL table instead of txt file
Posted
Updated 15-Mar-21 22:40pm
v3

This is pretty easy:
C#
string[] lines = File.ReadAllLines(@"drinks.txt");
for (int i = 0; i < lines.Count(); i++)
{
      //create an instance of button
     //C# 6.0 and higher
    //Button btn = new Button(){Name=$"Button{i}", Text = $"{i}", Size = new Size(90, 24), Location = new Point(10, (24*i) + 4)};
    //up to C# 5.0
    Button btn = new Button(){Name=string.Format("Button{0}", i), Text = string.Format("{0}", {lines[i]), Size = new Size(90, 24), Location = new Point(10, (24*i) + 4)};    //put it on the form
    this.Controls.Add(btn);
}
 
Share this answer
 
v3
Comments
Kleo Rogers 16-Mar-21 10:23am    
I have a error on
string[] lines = File.ReadLines(@"drinks.txt");

cannot implicitly convert type 'system.collections.generic.ienumerable to string an explicit conversion e4xist are you missing a cast
and on
$"Button{i} Syntax error ',' expected

i tried to play around with it and replace the string[] lines = File.ReadLines(@"drinks.txt"); with foreach (string line in File.ReadLines("drinks.txt")) remove the $ sign and get the buttons with text button {i} and get 12 buttons and i have 4 lines
Maciej Los 16-Mar-21 10:35am    
Sorry, my wrong. Check out updated answer.
Kleo Rogers 16-Mar-21 11:07am    
First error is ok now but second is still there if I delete $ sign I don't have any error but it is not righting the correct text on button i just have {i} as a text
Maciej Los 16-Mar-21 14:13pm    
What version of visual studio you are using? As far as i remember, using $ character ar the begining of text line is availanle within c# ver. 8. Try to use string.Format instead.
Kleo Rogers 16-Mar-21 14:19pm    
2013
Try like this:
C#
int i = 0;

foreach (string line in File.ReadLines("drinks.txt"))
{
           i++;
           // toolStripLabel6.Text=i.ToString();
           toolStripLabel6.Text = line;
           toolStripLabel6.Visible = true;
}

See example here on how to create an array of buttons: Button Array Using C#[^]
 
Share this answer
 
v2
Comments
Kleo Rogers 16-Mar-21 2:49am    
So this will create count of lines and give me result to the label but that was the wrong way i need every line to rad and create that many buttons and and read the line and place every line as text in button text or i am reading this code 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