簡體   English   中英

按鈕背景圖像檢查?

[英]Button Background Image Check?

請問我在嘗試檢查按鈕的背景圖像時遇到問題。 如果按鈕的圖像與資源文件夾中的圖像匹配,我想刪除該按鈕。 我努力了

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

但是顯示的消息框是顯示“否”的消息。

請問發生了什么事?

添加按鈕時

button.Tag = "ToDelete";

然后再

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

在這里您必須要做的。 您需要枚舉所有圖像並將指針存儲在按鈕的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");
         }
     }

我已經找到了問題的答案。我剛剛修改了代碼

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

它運作完美。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM