猿问

在 C# WinForm 中单击 PicutreBox 时将 List<Object>

我正在尝试编写一个 WinForm 应用程序来跟踪我的照片、照片的发布位置以及照片中的人物。我已经使用这些信息创建了一个 SQL 后端数据库。我创建了一个模型(非编程人员)类来构造该对象,然后创建了一个连接类,该连接类连接到数据库并执行查询并使用数据库中的值创建一个列表。


我能够在 flowpanel 布局中生成一组图像,循环访问并抓取照片。从 Form1_Load 的同一部分,我可以将列表中的每个元素输出到控制台。我遇到的问题是将这些值传递给 PictureBox1_Click 处理程序。我可以毫无问题地获取 URL,因为它是 PictureBox 对象的一部分,但我不知道如何获取与我单击发送的给定照片关联的整个 Model 对象。目的是,当我单击照片时,布局中的文本标签应填充数据库中的名称、位置、日期等值。


我尝试将整个列表传递给单击处理程序,但失败了,我尝试从加载事件重载传递给单击事件,这给了我在图片框上投射错误。


    private void PictureBox1_Click(object sender, EventArgs e)

    {

        PictureBox p = (PictureBox)sender;



        string j = p.ImageLocation;

        MfNameTxt.Text = "Chris";

        MessageBox.Show("Clicked! " + j);

    }


    private void Form1_Load(object sender, EventArgs e)

    {


        daoConn dc = new daoConn();

        List<Model> models = new List<Model>();

        string url;

        string fName;

        models = dc.GetAllModels();



        foreach (Model m in models)

        {

            int tempValue;

            tempValue = 1;

            PictureBox pb = new PictureBox();

            url = baseUrl + m.MhsUrl;

            fName = m.mFirstName;

            Size size = new Size(100, 100);

            pb.ImageLocation = url;

            pb.Size = size;

            pb.Click += new EventHandler(PictureBox1_Click);

            pb.SizeMode = PictureBoxSizeMode.Zoom;

            modelHsFlowLayout.Controls.Add(pb);

        }


    }

当我尝试 pb.Click += new EventHandler((s, e1) => this.PictureBox1_Click(sender, e, tempValue)); 时 只是为了发送一个临时值并演示我可以在那里传递一些东西,我得到了案例错误:Message=Unable to cast object of type '_pictures_v2.Form1' to type 'System.Windows.Forms.PictureBox'。


隔江千里
浏览 84回答 1
1回答

侃侃尔雅

这是因为在示例代码中,sender 被传递,而不是传递 s。另外你应该传入 e1 而不是 e。这将给出以下代码:pb.Click += new EventHandler((s, e1) => this.PictureBox1_Click(s, e1, tempValue))您还可以使用闭包,而不是将点击处理代码提取到单独的函数中,它可以全部内联完成。例如:private void Form1_Load(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; daoConn dc = new daoConn();&nbsp; &nbsp; &nbsp; &nbsp; List<Model> models = new List<Model>();&nbsp; &nbsp; &nbsp; &nbsp; string url;&nbsp; &nbsp; &nbsp; &nbsp; string fName;&nbsp; &nbsp; &nbsp; &nbsp; models = dc.GetAllModels();&nbsp; &nbsp; &nbsp; &nbsp; foreach (Model m in models)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tempValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempValue = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PictureBox pb = new PictureBox();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url = baseUrl + m.MhsUrl;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fName = m.mFirstName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Size size = new Size(100, 100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pb.ImageLocation = url;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pb.Size = size;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pb.Click += (s,clickEvent) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You can use of pb directly here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You also have access to things like models too.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string j = pb.ImageLocation;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MfNameTxt.Text = "Chris";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Clicked! " + j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pb.SizeMode = PictureBoxSizeMode.Zoom;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelHsFlowLayout.Controls.Add(pb);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答