Click here to Skip to main content
15,912,292 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all, I'm still fairly new to C# and I'm working on an issue in one of my assignments. I've successfully been able to read a text file into my code and transfer the list into a listbox. that was just for testing purposes. Then I was able to generate the same amount of buttons for each item from the text file into a panel. However I cannot seem to get the names of the items to become the text in the buttons and then to assign each button/name to the correct value to then do calculations. I'm not sure if I'm trying to combine two completely different ways of approaching this outcome but any help would be appreciated.

What I have tried:

this is the form class with the form load method

public Form1()
{
InitializeComponent();
}

List<appliances> allEntry = new List<appliances>();

public void Form1_Load(object sender, EventArgs e)
{

allEntry = ApplianceDA.GetAppliances();

DisplayButtons();
}

public void DisplayButtons()
{
int product = 0;
int startTop = 10;
int startLeft = 10;

foreach (Appliances ap in allEntry)
{

Button appButton = new Button();
this.Controls.Add(appButton);
appButton.Text = ("");
appButton.Location = new System.Drawing.Point(startTop + (125 * product), startLeft);
appButton.Height = 58;
appButton.Width = 120;
//appButton.Click += AButton_Click();
pnlBox.Controls.Add(appButton);

product++;
}
}

and here is the ApplianceDA class that reads the text file

static class ApplianceDA
{
private const string PATH = "Appliances.txt";


public static List<appliances> GetAppliances()
{
List<appliances> appliance = new List<appliances>();
StreamReader textIn = new StreamReader(new FileStream(PATH, FileMode.OpenOrCreate, FileAccess.Read));
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split(',');

Appliances product = new Appliances(columns[0], Convert.ToInt64(columns[1]));

appliance.Add(product);
}

textIn.Close();

return appliance;
}
Posted
Updated 10-Aug-16 15:44pm
Comments
BillWoodruff 11-Aug-16 3:37am    
In the loop where you create the Buttons you are not using the 'ap object in the loop: why is that ? Does each Applicance instance contain something you can use for a name, or for text on the Button ?

1 solution

When you are creating the buttons you are setting the appButton.Text to blank.

Try setting it to ap.ToString() or the text value you are after.
 
Share this answer
 
v2

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