I have a text file with a few lines of data, each line containing 2 pieces of data. I split the data with a # delimiter and assign the first piece of each line to a button name and create a button. The second piece of data will be used later (it’s just an inputted path, you can see that in the code lol) but is not relevant to this question.
As of right now, I have a TableLayoutPanel
with 3 columns and 4 rows. Depending on the data in the file, a button will be placed in a row&column accordingly.
Lets say the file I’m reading from has 4 lines of data, then 4 buttons will be created, 3 on the first row, one on the second row.
Here’s my code:
private void GetAllPaths()
{
int count = 0;
StreamReader inFile = new StreamReader("app-paths.txt");
string line = inFile.ReadLine();
while (line != null)
{
string[] temp = line.Split("#");
CreateButton(temp[0], temp[1]);
count++;
line = inFile.ReadLine();
}
inFile.Close();
if (count > 0)
{
label1.Visible = false;
}
}
private void CreateButton(string appName, string appPath)
{
tableLayoutPanel1.Controls.Add(new Button { Text = Button, Width = 150, Height = 100, BackColor = Color.SlateBlue, Font = new Font("Segoe UI", 8.5f, FontStyle.Bold), Dock = DockStyle.Fill, AutoSize = true }, 0, 0);
}
Because each button is created programmatically, how can I tell which created button is clicked? Am I able to get the name or some type of ID/Key for each button?