winforms 如何与使用foreach循环创建的按钮交互?

cnjp1d6j  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(136)

我希望使用foreach循环从项目列表中创建这些按钮,以访问存储在这些项目中的信息。
我使用foreach循环从一个目录中的一系列代表文件的项创建按钮,这些项包含指向这些文件的链接。我的目标是使按下这些按钮打开一个包含这些文件的对话框,以便在其中写入一行。

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;
    }
}
wqlqzqxt

wqlqzqxt1#

您可以使用Tag属性,将movie对象附加至循环中的每个按钮:

movie1Button.Tag = movie;

然后在click事件中从发送方获取按钮并将Tag-object转换回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
    }

}

相关问题