DragDrop a User Control

I am attempting to create a User Control (I named Token) that is circular and that the user can drag and drop on the application's form; like moving checkers around a checker board. Each Token would have a number associated with it.

My first problem is how to best design the user control. I have made several attempts at designing the user control. I can't seem to find one that works satisfactorily.

1.) My first attempt to create the Token was to use an OvalShape and a Label. I liked this approach as I had full programmatic control over Token; I could easily change the size, color and Label.Text. However I could only initiate the DragDrop by clicking outside the OvalShape and within the Token's rectangle. The second problem was that while dragging the Token would blink incessantly as it was being redrawn.

2.) My second attempt was to create the Token by adding just a BackgroundImage to the User Control. I chose to use a GIF image and I made the color outside the circle transparent. This solves the problem initiating the DragDrop. I can initiate the DragDrop from anywhere within the Token's rectangle. It would be cool if the just the image was the active area. If I use this solution I'll need to create images for each color and text combination.

3.) My third attempt totally flopped. I tried to build a class of Tokens using just OvalShapes with Labels, but I found that OvalShapes don't have an AllowDrop property; they are undraggable.

The second problem is that when I drag either design 1 or design 2 tokens, I drag a rectangle whose BackgroundColor is the BackgroundColor of the form. If I change the Background color of the Form, the BackgroundColor of the Token's rectangle also changes. As one token is dragged over another it is obvious that the entire rectangle is being dragged, not just the circular image. I can't seem to make the background of the Token truly transparent.

Any suggestions that you have regarding how to create the token and make its Background transparent would be greatly appreciated. Thanks,

[2979 byte] By [GrandpaB] at [2008-1-10]
# 1
Your key problem here is using a UserControl. It is a control with its own window and that window is rectangular. When it paints itself, it will first erase the background (a rectangle), then draw the controls inside of it. OvalShape has the opposite problem, it is not a control and therefore doesn't support control properties and methods, like DoDragDrop.

To solve your problem, you'll have to take a completely different approach. You'll need to give up on controls and make the form itself manage a collection of OvalShapes. When it sees the MouseDown event, it should look in its list of OvalShapes to see which one is located at the e.Location position. Mark that one as the "selected" oval, then change its position in the MouseMove event.

nobugz at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Nobugz,

Thanks for the redirection. Your suggestion is logical and makes sense to me. I'll begin with a new Token class that contains the functionality that I need. I'll probably get stuck and need to post another question, but it is good to know that I was headed down a no-win path.

Thanks again,

GrandpaB at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...