0

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

7
  • before I answer, your saying you have 2 lines in your streamreader, and you want to make 1 button tha corresponds to each label and textbox? Commented Sep 2, 2014 at 16:00
  • why don't you create a method that accepts string ftpLabel and within that method create the Button object and it's related event handler..? Commented Sep 2, 2014 at 16:02
  • @CalvinSmith That is exactly it. Commented Sep 2, 2014 at 16:09
  • @DJKRAZE That sounds like an option except i am still pretty new at c#, so i am not quite sure how to do something like that Commented Sep 2, 2014 at 16:09
  • create a private or public method since you are new then google C# how to create methods with a single parameter Commented Sep 2, 2014 at 16:11

1 Answer 1

1

You need a hook between the button and the corresponding label and textbox. This is probably not the correct way to do this but the easiest way it to induce a naming convention as a hook.

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 lFTPtextext = new Label()
                 {
                    AutoSize = true,
                    Text = line,
                    Tag = l
                 };


                Button bt = new Button()
                 {
                   Text = "Copy Link",
                   Tag = l
                 }

                 bt.Click += bt.Click;

                TextBox tb = new TextBox()
                {
                   Tag = l
                }

                flp.Controls.Add(lFTPtextext);
                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)
    {
        string tagNumber = ((Button)sender).Tag.ToString();

        var tbText= this.Controls.OfType<TextBox>()
                           .Where(x => x.Tag.ToString() == tagNumber)
                           .FirstOrDefault()

        var lblText = this.Controls.OfType<Label>()
                           .Where(x => x.Tag.ToString() == tagNumber)
                           .FirstOrDefault()

        MessageBox.Show(tbText.ToString() + " " + lblText.ToString());
    }

Maybe not the best answer, but its how I would solve it.

Sign up to request clarification or add additional context in comments.

13 Comments

I think it may be missing something? I get Error Operator '==' cannot be applied to operands of type 'object' and 'int'
hmm... now the select does not appear to like the string values: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string' Sorry for being such a noob here :(
Any thoughts on how to fix that last error anyone? If i convert it to an INT then it says it needs to be a string and if i convert it to a string then it says the above error... Any help would be apprecated
Select returns an IEnumerable. You can wrap the whole thing in (...).FirstOrDefault(); or you can rewrite it as var lblText = this.Controls.OfType<Label>().Where(x => x.Tag.ToString() == tagNumber).FirstOrDefault();
@Loathing Ok so i tried both methods and even though I dont get any errors but I also do not get any results (lblText appears empty). Sorry for being a pain. I do get the tag number each time. When I debug, lblText = null and tagNumber = 0 (which is expected).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.