Home > Net >  How to interact with buttons created using a foreach loop?
How to interact with buttons created using a foreach loop?

Time:11-24

I want these buttons created using a foreach loop from a list of items to access the information that is stored in these items.

I used a foreach loop to create buttons from a list of items that represent files in a directory and those items hold links to these files. My goal is to make pressing these buttons open a dialog with these files to write a line in them.

public List<mov.Movie> movies = new List<mov.Movie>() { };

private void writeReportToolStripMenuItem_Click(object sender, EventArgs e)
{
    foreach (string file in Directory.EnumerateFiles(@"C:\Users\sloup\OneDrive\Desktop\MovieList", "*.txt"))
    {
        var lines = File.ReadAllLines(file);
        string movieName = lines[0];

        if (movies.Select(x => x.Name).Contains(movieName))
        {
        }
        else
        {
            string movieLength = lines[1];
            string movieYear = lines[2];
            string movieReport = lines[3];
            movies.Add(new mov.Movie(movieName, movieLength, movieYear, movieReport, @"C:\Users\sloup\OneDrive\Desktop\MovieList"   movieName   ".txt"));
        }
    }
        

        int X = 1;
        int Y = 1;
        foreach (var movie in movies)
        {

            
            var movie1Button = new Button();
            movie1Button.Text = movie.Name;
            movie1Button.Font = new Font("Calibri", 12);
            movie1Button.ForeColor = Color.Black;
            movie1Button.Padding = new Padding(6);
            movie1Button.AutoSize = true;
            this.Controls.Add(movie1Button);
            movie1Button.Location = new System.Drawing.Point(20, 50 * X);
            X  ;
            movie1Button.Click  = Movie1Button_Click;

            

            var movie1Label = new Label();
            movie1Label.Text = movie.Year;
            movie1Label.Font = new Font("Calibri", 12);
            movie1Label.ForeColor = Color.Black;
            movie1Label.Padding = new Padding(6);
            movie1Label.AutoSize = true;
            this.Controls.Add(movie1Label);
            movie1Label.Location = new System.Drawing.Point(200, 50 * Y);
            Y  ;
            }
}

private void Movie1Button_Click(object? sender, EventArgs e)
{
    string[] lines1 = File.ReadAllLines();
    var lines = File.ReadAllLines();
    string movieName = lines[0];
    string movieLength = lines[1];
    string movieYear = lines[2];
}

public class Movie
{
    public string Name;
    public string Length;
    public string Year;
    public string Report;
    public string Link;

    public Movie(string movieName, string movieLength, string movieYear, string movieReport, string movieLink)
    {
        Name = movieName;
        Length = movieLength;
        Year = movieYear;
        Report = movieReport;
        Link = movieLink;
    }
}

CodePudding user response:

You can use the Tag property to attach the movie object to each button in the loop:

movie1Button.Tag = movie;

afterwards in the click event grab the button from the sender and cast the Tag-object back to a Movie

private void Movie1Button_Click(object? sender, EventArgs e)
{
    Button button = sender as Button
    (if button != null)
    {
         Movie movie = button.Tag as Movie;
         // do what ever you like afterwards
    }

}
  • Related