Question

I have been searching the forums and Stack Overflow all day today trying to figure out the proper way to eliminate the annoying flicker when dragging images (picture boxes) around a panel on my form. I am trying to replicate a card game for my friends and I to be able to play from home. I have derived from the Picturebox class so that I can add some additional properties to my object. Here is the code for that class:

    class Card : PictureBox 
{
    public string CardId { get; set; }
    public string CardName { get; set; }
    public string CardColor { get; set; }
    public string CardType { get; set; }
    public string CardRarity { get; set; }
    public bool Tapped { get; set; }
    public bool Revealed { get; set; }        
}

I have a panel setup so that when a player double clicks a card in their hand it adds it to the panel. Then I have setup a drag and drop so they can move the cards around the panel. Here is the code for that.

    int posX;
    int posY;

    private void DragCard(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            dragging = true;
            posX = e.X;
            posY = e.Y;
        }
    }
    private void isDragging(object sender, MouseEventArgs e)
    {
        if (dragging == true)
        {               
            ((Card)sender).Left += e.X - posX;
            ((Card)sender).Top += e.Y - posY;               
            ((Card)sender).BringToFront();                
        }
    }
    private void DoneDrag(object sender, MouseEventArgs e)
    {
        dragging = false;
    }        

The problem is that I have a background image in the panel so when the card moves it shows a trail behind it from where it's original location was prior to the move. I have searched and searched and seen so many different methods of getting rid of this but nothing has seemed to work for me. I would greatly appreciate it if someone could explain it in lamest terms as I am very new to coding. Thank you very much for your help. Just in case I am leaving anything out I have added the rest of the code below:

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    List<Card> PlayerDeck = new List<Card>();
    Card curCard;
    bool dragging = false;
    int posX;
    int posY;

    private void DragCard(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            dragging = true;
            posX = e.X;
            posY = e.Y;
        }
    }
    private void isDragging(object sender, MouseEventArgs e)
    {
        if (dragging == true)
        {               
            ((Card)sender).Left += e.X - posX;
            ((Card)sender).Top += e.Y - posY;               
            ((Card)sender).BringToFront();                
        }
    }
    private void DoneDrag(object sender, MouseEventArgs e)
    {
        dragging = false;
    }        

    private void Form1_Load(object sender, EventArgs e)
    {
        btnLoadDeck.Enabled = true;
        btnShuffle.Enabled = true;
        numericHealth.Value = 20;
        numericHealth.Enabled = false;
        cardDeck.Enabled = true;
        cardGrave.Enabled = false;
        listStatus.Enabled = true;           
    }
    private void btnLoadDeck_Click(object sender, EventArgs e)
    {
        if (PlayerDeck.Count > 0)
        {
            MessageBox.Show("Deck Already Loaded...");
            return;
        }

        int x = 0;
        foreach (Card c in Deck.LoadDeck())
        {
            Card c1 = new Card();
            c1.CardId = c.CardId;
            c1.CardName = c.CardName;
            c1.CardColor = c.CardColor;
            c1.CardType = c.CardType;
            c1.CardRarity = c.CardRarity;
            c1.Size = new Size(100, 120);
            c1.Location = new Point(0, 0);
            c1.SizeMode = PictureBoxSizeMode.StretchImage;                
            c1.BorderStyle = BorderStyle.Fixed3D;
            c1.BackColor = Color.Black;
            c1.ContextMenuStrip = conMenuHand;
            c1.Image = Image.FromFile(@"C:\Users\vildez\Desktop\Virtual Magic\Cards\c72b2bb7-e6da-49bb-9bd5-a538db0761ee\Cards\" + c.CardId + ".jpg");
            c1.Name = "Card" + x.ToString();
            c1.MouseEnter += ViewCard;
            c1.DoubleClick += c1_DoubleClick;
            PlayerDeck.Add(c1);
            x++;  
        }
    }

    void c1_DoubleClick(object sender, EventArgs e)
    {
        PlayCard(sender,null);
    }

    void ViewCard(object sender, EventArgs e)
    {
        cardViewer.Image = Image.FromFile(@"C:\Users\vildez\Desktop\Virtual Magic\Cards\c72b2bb7-e6da-49bb-9bd5-a538db0761ee\Cards\" + ((Card)sender).CardId + ".jpg");
    }

    private void cardDeck_Click(object sender, EventArgs e)
    {            
        for (int i = 0; i < 10; i++)
        {
            panelHand.Controls.Add(PlayerDeck[0]);
            PlayerDeck.RemoveAt(0);
        }            
    }

    public void PlayCard(object sender, EventArgs e)
    {
        panelHand.Controls.Remove(((Card)sender));

        Card c1 = new Card();
        c1.CardId = ((Card)sender).CardId;
        c1.CardName = ((Card)sender).CardName;
        c1.CardColor = ((Card)sender).CardColor;
        c1.CardType = ((Card)sender).CardType;
        c1.CardRarity = ((Card)sender).CardRarity;
        c1.Size = new Size(50, 70);
        c1.SizeMode = PictureBoxSizeMode.StretchImage;
        c1.BorderStyle = BorderStyle.Fixed3D;
        c1.BackColor = Color.Black;
        c1.Location = new Point(10, 10);
        c1.Image = Image.FromFile(@"C:\Users\vildez\Desktop\Virtual Magic\Cards\c72b2bb7-e6da-49bb-9bd5-a538db0761ee\Cards\" + c1.CardId + ".jpg");
        c1.MouseDown += new MouseEventHandler(DragCard);
        c1.MouseMove += new MouseEventHandler(isDragging);
        c1.MouseUp += new MouseEventHandler(DoneDrag);
        c1.MouseEnter += new EventHandler(ViewCard);

        int x = 0;
        for (int i = 0; i < panelPlayer.Width; i++)
        {
            if (i + 50 > panelHand.Width)
            {
                i = 0;
                x += 70 + 1;
            }

            if (panelPlayer.GetChildAtPoint(new Point(i, x)) == null)
            {
                c1.Location = new Point(i, x);
                panelPlayer.Controls.Add(c1);                    
                break;
            }
        }
    }
    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
        curCard = ((ContextMenuStrip)sender).SourceControl as Card;
    }

    private void tsMenuItemPlayCard_Click(object sender, EventArgs e)
    {
        PlayCard(curCard, null);
    }

    private void panelPlayer_Paint(object sender, PaintEventArgs e)
    {

    }

}

}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top