Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I need your help


it is showing just last line from text file on all button text

What I have tried:

string[] lines = File.ReadAllLines(@ ".txt");
for (int i = 0; i < lines.Count(); i++) {
  button10.Text = string.Format("{0}", lines[i]);
  button11.Text = string.Format("{0}", lines[i]);
  button12.Text = string.Format("{0}", lines[i]);
  button13.Text = string.Format("{0}", lines[i]);
  button14.Text = string.Format("{0}", lines[i]);
  button15.Text = string.Format("{0}", lines[i]);
  button16.Text = string.Format("{0}", lines[i]);
  button17.Text = string.Format("{0}", lines[i]);
}

I was trying to play with array and list but have a new problem now

List<Button> button = new List<Button> { button10, button11, button12, button13, button14, button15, button16, button17 };
        string[] lines = File.ReadAllLines(@".txt");
        for (int i = 0; i < button.Count(); i++)
            button[i].Text = string.Format("{0}", lines[i]);

"Index was outside the bonds of the array" i am getting this error
Posted
Updated 23-Mar-21 9:09am
Comments
Richard MacCutchan 19-Mar-21 12:05pm    
You are not reading any text, as you have forgotten to provide a valid filename.
Kleo Rogers 19-Mar-21 12:09pm    
string[] lines = File.ReadAllLines(@".txt");
i just didn't putt any name of the text file

string[] lines = File.ReadAllLines(@"mytextfile.txt");
Richard MacCutchan 19-Mar-21 12:10pm    
So how many lines do you think you will receive?
Kleo Rogers 19-Mar-21 12:12pm    
I don't Know sometimes will be 4 maybe more maybe less i need to leave that open
Richard MacCutchan 19-Mar-21 12:13pm    
No, the answer is none, because you have not provided a filename.

This is what you should be doing:
C#
List<Button> buttonList = new List<Button>(); // only if you need to keep a list
string[] lines = File.ReadAllLines(@"FILENAME.txt");
int Yposition = 10; // starting location of the first button
foreach (string line in lines)
{
    Button button = new Button();
    button.Text = line;
    button.Location = new Point(10, Yposition);
    Yposition += button.Height;
    //
    // add other properties as required
    //
    buttonList.Add(button); // as above
    Controls.Add(button); // add to the Form's Control collection
}
 
Share this answer
 
v2
Comments
Kleo Rogers 19-Mar-21 12:45pm    
@Richard MacCutchan this .txt is just for this question and in the real code i have full path and file name and this solution is for creating new buttons but i need it for exiting ones
Richard MacCutchan 19-Mar-21 12:49pm    
Well the issue is the same, you can only add as much text as you read from the file. So if you only get 4 lines of text you can only set 4 buttons. Unless you add some extra code to restart from the first line if there are more buttons than lines of text.
Kleo Rogers 19-Mar-21 12:52pm    
I was thinking just to add text to the buttons from the line for example I have 4 line it will add text to 4 buttons and to hide rest
Richard MacCutchan 19-Mar-21 13:01pm    
Well it largely depends on what your code is supposed to be doing.
Kleo Rogers 19-Mar-21 13:26pm    
it supposed to read lines fill the button text and hide the rest of the buttons
Quote:
"Index was outside the bonds of the array" i am getting this error

the problem is that you have more buttons than lines in file.

C#
button[i].Text = string.Format("{0}", lines[i]);

I wonder why you use format to make the line the same ?
Why not use this ?
C#
button[i].Text = lines[i];
 
Share this answer
 
Ok I figure it out
I will need to wright more code but it is resolving my problem it is reading line by line
string line1 = File.ReadLines(@".txt").First();            
            {
                button10.Text = line1;
            }
            string line2 = File.ReadLines(@".txt").ElementAtOrDefault(1); 
            {
                button11.Text =  line2;
             }
if (string.IsNullOrEmpty(button15.Text))
            {
                button15.Visible = false;
            }
 
Share this answer
 
Comments
Richard MacCutchan 20-Mar-21 4:12am    
And what happens if there is only one line in the file?
Kleo Rogers 20-Mar-21 4:39am    
it will send a text to the button and rest of the buttons will be hidden and i will have just one button
Maciej Los 21-Mar-21 6:17am    
No, you don't!
Richard MacCutchan showed you a proper way to achieve that!

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