Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 17th, 2008, 3:58 AM   #11
beni_dude
Newbie
 
beni_dude's Avatar
 
Join Date: Apr 2008
Posts: 19
Rep Power: 0 beni_dude is on a distinguished road
Re: Dragging and Dropping

First thing you are right about this being in some new kind of thread but I wasn't sure because it was already on the subject of d&d, If you ask me start an new thread I will do so Happily!

I'm using right now visual studio 2008 and its an full version.

What I'm trying to do is this:
I want to create an win form that has in it lets say 12 labels and 12 text box and I want the user to d&d the right label in the right text box (I already created something like that in VB6 (but that was a bit easier because their it wasn't hard to create an array of controls) but it wasn't with the d&d option!

it sounds simple enough, after all that I need to do is make sure that once the user clicks on the label (or press on it and I think that click event is the one I need to catch ) I will give the control (the label) the x&y location of the mouse and then make sure that he (the user) can drop it only on text box (he can place it in the wrong text box and then he will receive some kind of error message)

It will be really helpfully if some one could find some kind of simple example that shows how to d&d a control in run time! god knows that I TRIED and failed!
__________________
NULL is all around me!
beni_dude is offline   Reply With Quote
Old Apr 17th, 2008, 7:56 AM   #12
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 864
Rep Power: 3 lectricpharaoh is on a distinguished road
Re: Dragging and Dropping

Quote:
Originally Posted by beni_dude
I want to create an win form that has in it lets say 12 labels and 12 text box and I want the user to d&d the right label in the right text box (I already created something like that in VB6 (but that was a bit easier because their it wasn't hard to create an array of controls) but it wasn't with the d&d option!
Ahh, I see. From your previous posts, I wasn't sure whether this was something you wanted at design time, or run time. However, I now understand what you mean.

Is this for one of those 'match items in column B with those in column A' kinds of things?
Quote:
Originally Posted by beni_dude
it sounds simple enough, after all that I need to do is make sure that once the user clicks on the label (or press on it and I think that click event is the one I need to catch ) I will give the control (the label) the x&y location of the mouse and then make sure that he (the user) can drop it only on text box (he can place it in the wrong text box and then he will receive some kind of error message)
Hmm. Well, drag-and-drop isn't the answer here. When you drag-and-drop, the control on which you drop a file just receives a notification with the path to the file you're dropping on it. In other words, there's no context for using it in the way you want to; the messages would be meaningless.

Now, you could play with mouse events and properties in conjunction with this. I've played with it for a few minutes, and it doesn't work yet, but I'm sure if I spent a while on it, I could get it working. However, it would be rather messy. If all you're trying to do is a matching game thing, then there's a much easier alternative.

Have your textboxes in one column, and your labels in another. Each label recognizes the Click event, and when clicked, it will change color, and set a flag (you have a flag for each label). You also have a handler for the textbox Click events, and the handler here will simply exit if none of the flags for the labels are set. If a flag is set, it will assign that label's text to the textbox, and do whatever other checking you want.

By using arrays and shared handlers, you can keep the amount of code to a minimum. By 'shared handlers', I mean all the labels will use one handler, and all the textboxes another. You can tell which label or textbox invoked a handler by checking the sender parameter that gets passed to the event-handling delegate. Here is a small example:
C# Syntax (Toggle Plain Text)
  1. // this example assumes your main form class is called 'Form1', and
  2. // that it contains three labels called 'label1', 'label2', and
  3. // 'label3' (I was too lazy to rename the defaults)
  4. public partial class Form1 : Form
  5. {
  6. // class variables; these make it easier to process things in
  7. // loops
  8. bool[] labelClickedFlag;
  9. Label[] labelArray;
  10.  
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15.  
  16. // initialize our class-scope arrays here, and set up event
  17. // handlers. for more generic code, you will not want to use a
  18. // hardcoded constant array size here, but instead a value
  19. // determined at run time. the logic can be extended to cover
  20. // as many labels as you like.
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. labelClickedFlag = new bool[3];
  24. labelArray = new Label[3];
  25. labelArray[0] = label1;
  26. labelArray[1] = label2;
  27. labelArray[2] = label3;
  28. for (int x = 0; x < labelArray.Length; ++x)
  29. labelArray[x].Click += label_Click;
  30. }
  31.  
  32. // this is the handler for the Click event for all three labels.
  33. // it toggles a flag according to which label was clicked, sets
  34. // the clicked label's foreground color to crimson, and sets
  35. // the foreground colors for the other labels to the default
  36. private void label_Click(object sender, EventArgs e)
  37. {
  38. Label clickedLabel = (Label)sender;
  39. for (int x = 0; x < labelArray.Length; ++x)
  40. {
  41. if (labelArray[x] == clickedLabel)
  42. labelClickedFlag[x] = !labelClickedFlag[x];
  43. else
  44. labelClickedFlag[x] = false;
  45. labelArray[x].ForeColor = Control.DefaultForeColor;
  46. if (labelClickedFlag[x])
  47. clickedLabel.ForeColor = Color.Crimson;
  48. }
  49. }
  50. }
Now, you can apply a similar logic to clicking the textboxes. You can also determine which, if any, of the labels had been clicked by checking the array of flags, and act appropriately.

You can also create the labels and textboxes dynamically through code, and initialize their properties in a loop. Assuming you want them aligned in columns or something, this is fairly straightforward; you just loop through setting their positions and sizes. If you're generating your content on the fly, or fetching it from a database, this kind of approach will probably work best.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC