Click here to Skip to main content
15,069,133 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
}
   
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
Richard MacCutchan 19-Mar-21 13:32pm
   
If you start with all the buttons hidden it should be easy enough.
Kleo Rogers 19-Mar-21 13:36pm
   
it is a same to me to hide it or to show it
Richard MacCutchan 19-Mar-21 13:44pm
   
Sorry, I don't understand what you are saying.
Kleo Rogers 19-Mar-21 15:38pm
   
It is same to me to have a command who will hide it or show the buttons so it is a not a problem to hide them at beginning
Richard MacCutchan 19-Mar-21 13:52pm
   
I have modifies the code in my solution slightly. Having tested that i can confirm that it should do what you need.
Kleo Rogers 19-Mar-21 15:38pm
   
I tested your code it is still creating new buttons it is reading from the text file and creating that many buttons how many i have lines in text file
Maciej Los 21-Mar-21 6:17am
   
5ed!
Richard MacCutchan 21-Mar-21 6:29am
   
Thanks, and for the other votes.
Kleo Rogers 21-Mar-21 8:48am
   
If you spend this much time on me ofc I will vote Thank you for the time
And help
Richard MacCutchan 21-Mar-21 13:36pm
   
That message was meant for Maciej Los.
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];
   
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;
            }
   
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