简体   繁体   English

按钮背景图像检查?

[英]Button Background Image Check?

Pls i have a problem with trying to check the background Image of a button. 请问我在尝试检查按钮的背景图像时遇到问题。 I want to delete the button if its image matches an Image in the Resource Folder. 如果按钮的图像与资源文件夹中的图像匹配,我想删除该按钮。 I have Tried 我努力了

    private void DeleteCard(Button btn)
    {
        if (btn.BackgroundImage.Equals( FunWaysInc.Properties.Resources.Card_1))
        {
            playersCards.Remove(Properties.Resources.Card_1);
            MessageBox.Show(playersCards.Count.ToString());
        }
        else
        {
            MessageBox.Show("NO");
        }
    }

    private void frmQuickSpark_Load(object sender, EventArgs e)
    {
        Button tstbtn = new Button();
        tstbtn.BackgroundImage = Properties.Resources.Card_1;
        DeleteCard(tstbtn);
    }

but the message box displayed is the one that displays "NO".. 但是显示的消息框是显示“否”的消息。

Pls what is happening???? 请问发生了什么事?

when adding the button 添加按钮时

button.Tag = "ToDelete";

then later 然后再

foreach (Button b in this.Controls.OfType<Button>())
{
    if(b.Tag == "ToDelete")
    {
        //delete
    }
}

Here what you have to do. 在这里您必须要做的。 You need to enumerate all your images and store pointer in Tag property of the button. 您需要枚举所有图像并将指针存储在按钮的Tag属性中。

private Dictionary<string, Image> _table = new Dictionary<string, Image>();

private void frmQuickSpark_Load(object sender, EventArgs e)
    {
        _table.Add("Image1", Properties.Resources.Card_1);
        _table.Add("Image2", Properties.Resources.Card_2);
        Button btn = new Button();
        SetButtonImage(btn);
        DeleteCard(btn);
    }
 private void SetButtonImage(Button button)
    {

        button.BackgroundImage = _table["Image1"];
        button.BackgroundImage.Tag = "Image1";

    }

    private void DeleteCard(Button btn)
    {
         if (btn.BackgroundImage.Tag == "Image1") 
         {
             playersCards.Remove(btn); // not sure what your logic of removal
             MessageBox.Show(playersCards.Count.ToString());
         }
         else
         {
             MessageBox.Show("NO");
         }
     }

I already found an answer to my question.. I just modified my code 我已经找到了问题的答案。我刚刚修改了代码

private void DeleteCard(Image img)
{
    playersCards.Add(Properties.Resources.Card_1);
    if (img == playersCards[0])
    {
        playersCards.Remove(Properties.Resources.Card_1);
        MessageBox.Show(playersCards.Count.ToString());
    }
    else
    {
        MessageBox.Show("NO");
    }
}

private void frmQuickSpark_Load(object sender, EventArgs e)
{
    Button tstbtn = new Button();
    tstbtn.BackgroundImage = Properties.Resources.Card_1;
    Image img = tstbtn.BackgroundImage;
    DeleteCard(img);
}

It works perfectly. 它运作完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM