【问题标题】:C# changing the background image of multiple controlsC#更改多个控件的背景图片
【发布时间】:2015-08-07 08:17:08
【问题描述】:

在 C# 表单中,我创建了一个函数,该函数一旦被调用,就会创建一个具有所需图像、大小和位置的图片框:

private void NewPic(string nName, int locX, int locY, int SizX, int SizY, Image Img)
{
    PictureBox Pic = new PictureBox();
    Pic.Name = nName; Pic.Image = Img;
    Pic.BackColor = Color.Transparent;
    Pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    Pic.Size = new System.Drawing.Size(SizX, SizY);
    Pic.Location = new System.Drawing.Point(locX, locY);
    Controls.Add(Pic);
    Pic.Click += new EventHandler(Pic_Click);
}

现在当我需要一张图片时,我会这样做:

NewPic("FIRE", 32, 100, 120, 120, Properties.Resources.Image);

问题是,在点击事件中,当我点击图片框时,我希望它更改其背景图片,但是,如果我点击其他图片框,我希望最后一个图片框自行重置:

private void Pic_Click(object sender, System.EventArgs e)
{
    PictureBox pb = (PictureBox)sender;
    switch (pb.Name)
    {
        case "1": 
            pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
            pb.BackgroundImageLayout = ImageLayout.Stretch;
                //INSERT CODE HERE: to remove from other if it has
            break;
        case "2":
            pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
            pb.BackgroundImageLayout = ImageLayout.Stretch;
                //INSERT CODE HERE: to remove from other if it has
            break;
        }
     }

我需要可以应用于多个图片框/对象的代码,而不仅仅是两个

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    我想你也需要存储最后一个图片框的图像

    PictureBox _lastPictureBox = null;
    Image _lastPictureBoxImage = null;
    
    private void Pic_Click(object sender, System.EventArgs e)
    {
        PictureBox pb = (PictureBox)sender;
        if (_lastPictureBox != null) 
        {
          // update the previous one, eg:
           _lastPictureBox.BackgroundImage = _lastPictureBoxImage;
        }
    
        // now set it to the current one:
       _lastPictureBox = pb;
       _lastPictureBoxImage = pb.Image;
       switch (pb.Name)
       {
         case "1": 
           pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
           pb.BackgroundImageLayout = ImageLayout.Stretch;
        break;
        case "2":
          pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
          pb.BackgroundImageLayout = ImageLayout.Stretch;
        break;
      }
    

    }

    【讨论】:

    • Y>es,这就是我所缺少的,谢谢,谢谢大家。如果您有更多建议,我会全部考虑:)
    • “switch 语句”似乎也是重复的,如果每个图片框都有相同的背景图片,可以去掉。
    • 在 switch 语句的任何一种情况下,都会执行相同的以下代码。 pb.BackgroundImage = Properties.Resources.OtherImg; pb.BackgroundImageLayout = ImageLayout.Stretch;
    【解决方案2】:

    最简单的方法是在表单中添加一个成员,该成员将跟踪先前单击的 PictureBox:

    PictureBox _lastPictureBox = null;
    

    在处理程序中,检查 _lastPictureBox 是否有值,并根据需要进行更新:

    private void Pic_Click(object sender, System.EventArgs e)
    {
        PictureBox pb = (PictureBox)sender;
        if (_lastPictureBox != null) 
        {
            // update the previous one, eg:
            _lastPictureBox.BackgroundImage = Properties.Resources.FirstImg;
        }
    
        // now set it to the current one:
        _lastPictureBox = pb;
    
        switch (pb.Name)
        {
        case "1": 
            pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
            pb.BackgroundImageLayout = ImageLayout.Stretch;
            break;
        case "2":
            pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
            pb.BackgroundImageLayout = ImageLayout.Stretch;
            break;
        }
     }
    

    【讨论】:

    • 嗯,我认为这行不通。问题是我有 4 个创建的图片框,所以它们不是空的,所以当 _lastpicturebox = null 什么都不会发生,因为我没有空的图片框
    • @AntonioTehSumtin _lastPictureBox 一开始是空的,所以它什么也不做。但是当用户第一次点击图片时,_lastPictureBox 会获得对该图片的引用(因此您可以在下次点击时重置它)。
    【解决方案3】:

    如果我理解正确,你需要一个全局变量

    PictureBox lastbox;
    

    然后你可以插入这个代码:

    lastbox.Image = Properties.Resources.Image;
    lasbox = pb;
    

    【讨论】:

    • 很抱歉,但我真的不认为你帮了太多 ^^' 感谢您的尝试,非常感谢。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2012-03-29
    • 1970-01-01
    相关资源
    最近更新 更多