In pushing forward with my application (winforms) and I have found a need to have dynamically generated content to be pushed to a button that then will display that dynamic content in a messagebox.
What I am doing is reading FTP links from a file, based on a product selection, then I want to take each line and have it append a textbox and a button so that they can fill out the file name.
I have it it working where the Label, textbox and button all appear as i would expect. Now what I want is to have the button when clicked display my content that was just generated.
So the code I have right now that generates everything as expected is as follows. I just need to know how to make the button accept my ftpLabel.Text and tb.Text data. I have tried to follow the post here: How can i create dynamic button click event on dynamic button? However, I cannot seem to make it accept any of my dynamic content.
private void productComboBox_SelectedItemChanged(object sender, EventArgs e)
{
// Read from the text file and out put the results
try
{
using (StreamReader sr = new StreamReader(selectedProduct + ".txt"))
{
string line;
int l = 0;
// build the ftp link records line by line
while ((line = sr.ReadLine()) != null)
{
Label ftpLabel = new Label();
ftpLabel.AutoSize = true;
ftpLabel.Text = line;
TextBox tb = new TextBox();
Button bt = new Button();
bt.Text = "Copy Link";
bt.Click += bt.Click;
flp.Controls.Add(ftpLabel);
flp.Controls.Add(tb);
flp.Controls.Add(bt);
l++;
}
ftpGroupBox.Controls.Add(flp);
}
}
// If the read fails then output the error message in the same section
catch (Exception ex)
{
Label ftpErrorLabel = new Label();
ftpErrorLabel.AutoSize = true;
ftpErrorLabel.ForeColor = Color.Red;
ftpErrorLabel.Text = "Error: " + ex.Message;
flp.Controls.Add(ftpErrorLabel);
ftpGroupBox.Controls.Add(flp);
}
}
void bt_Click(object sender, EventArgs e)
{
MessageBox.Show("Does this work?");
// this message displays but cannot pass my dynamicly created content to this
}
Any Advice is appreciated.
Thanks