Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
For my cards game I add a click handler / event to each shuffled card.

The problem is an error when the user double clicks a card.
A single click moves the card to another panel.

            foreach (PlayingCard cardShuffled in cards)
            {
                cardShuffled.Click += card_Click;
. . .

Quote:
System.ArgumentException: "The specified Visual object is already a child of another Visual object or the root of CompositionTarget."


What I have tried:

foreach (PlayingCard cardShuffled in cards)
{
   cardShuffled.MouseDoubleClick += card_Click;
   cardShuffled.Click += card_Click;


Now the problem is that "card_Click" runs twice on card double-click.

Any ideas?
Posted
Updated 19-Jun-22 4:08am
v2
Comments
0x01AA 19-Jun-22 9:09am    
I think should be easy. In your OnClick handler just Btn.Enabled= false; and at the end back ;)
[Edit]
Btw. why you add a double click handler?
Jo_vb.net 19-Jun-22 9:38am    
It is not a button but a PlayingCard class object, which has no enabled property.
And I tried the double click handler only to see if it would help but it does not.

private void card_Click(object sender, EventArgs e)
{
PlayingCard pc = (PlayingCard)sender;
0x01AA 19-Jun-22 9:47am    
Then take the dirty way and prevent from reprocessing with a bool...
Something like:

OnClick
{
if (processing) return;
processing= true;
try
{
// Do it
}
_finally
{
procssing= false;
}
}
Jo_vb.net 19-Jun-22 10:02am    
That works fine - thank you.
0x01AA 19-Jun-22 10:20am    
You are very welcome.
Pretty incomprehensible to me that you programmed 'Schafkopf' and failed at such a small thing :-)

1 solution

According to the discussions in the comments, there is a 'lazy' solution available:

Prevent from reprocessing with something like that:
OnClick
{
  if (this.processing) return;
  this.processing= true;
  try
  {
    // Do the work here
  }
  _finally
  {
    this.procssing= false;
  }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900