943,733 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2737
  • C# RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Apr 2nd, 2009
0

Re: Hello,windows applications,help please

Good, I am glad you have decided not to cheat.
On the otherhand, reviewing someones code is the way most everyone learns.
Okay, as for the buttons, I assume you are using Visual Studio 2008 Express, if not get an IDE, SharpDev, Mono or VS2008e (all are free).

Here is some code to get you started:
Create a Windows application, and spread out the form so it can fit the 9 buttons along the bottom of the form.

For now, I dragged a Label component from the toolbox onto the center of the screen. This is the component we will drop the buttons onto. You will want to use your own target component(s) once you understand the concept. Set the AllowDrop property in the property editor of the label to true (means yes, I want this component to allow things to be dropped on it).

Select the first button (button1 in this case), and in the property editor, select the event (lightening bolt) tab, and find the onMouseMove event handler. Type in buttonMouseMove (or whatever you desire). Press enter and let it create the event handler code. Now go back to the form and select all of the other buttons, and drop down the onMouseMove property combobox, and select the buttonMouseMove handler you just created. This ties all of the buttons to the same event handler (just makes it easier than creating and duplicating 9 handlers).

Next you want to force the button into a Drag operation by using the button's DoDragDrop method. Now you have a button in the drag-able mode, lets deal with the label (the target for the drop operation).
As a minimum, you want to set the DragOver event handler of the label (target) so the user and component will know it is okay to drop the dragged object here. You will set the event handler for the DragOver of the lable to provide a drag effect which will change the cursor as the object is dragged over it.

Finally you want to assign an event handler to the DragDrop handler of the label to accept the dragged object.

In the handler you will get the object that has been dropped. We know this is a button for this little example, so I do not perform the typical checking and validation, but rather I just cast the incoming object as a Button, and set the Label's text to be the same as the dropped button.

Hope this helps,
// Jerry

C# Syntax (Toggle Plain Text)
  1. using System.Data;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. namespace WindowsFormsApplication1
  8. {
  9. public partial class Form1 : Form
  10. {
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15.  
  16. private void buttonMouseMove(object sender, MouseEventArgs e)
  17. {
  18. if (e.Button == MouseButtons.Left)
  19. {
  20. Button btn = sender as Button;
  21. btn.DoDragDrop(sender, DragDropEffects.Copy);
  22. }
  23. }
  24.  
  25. private void label1_DragOver(object sender, DragEventArgs e)
  26. {
  27. e.Effect = DragDropEffects.Copy;
  28. }
  29.  
  30. private void label1_DragDrop(object sender, DragEventArgs e)
  31. {
  32. Button btn = e.Data.GetData(typeof(Button)) as Button;
  33. label1.Text = btn.Text;
  34. }
  35. }
  36. }
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 3rd, 2009
0

Re: Hello,windows applications,help please

amazing! very detailed explanation!!! I'm gonna try that immediately when i come back from school and report later! hands down.
thank you!
Last edited by JooClops; Apr 3rd, 2009 at 2:03 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 3rd, 2009
0

Re: Hello,windows applications,help please

Hello Jerry!
I've tried the things you've (very easy to follow, you should be a teacher/professor! and i can't get this working, I'm attaching what I've done so far, which is basically what you told me to do (didnt add anything yet),maybe i missed something(in the image there are the form and parts from the form1.cs,form.designer.cs,properties and event handler)
and when i activate it(F5) i can't drag anything to the label, and by they way, will i have to make 81 labels for the soduko?!
And i really appreciate your help!
Thank you very much,
JooClops
P.S
damn resolution is higher than allowed
so here is the link:
[img=http://img237.imageshack.us/img237/1417/winapp.jpg]
Last edited by JooClops; Apr 3rd, 2009 at 10:40 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 3rd, 2009
0

Re: Hello,windows applications,help please

ok never mind that! i found my mistake(forgot the label 1 drag drop in the event handler)
now how can i make 81 labels????than i'll try to continue by my own
thank you so much,
JooClops
Last edited by JooClops; Apr 3rd, 2009 at 1:05 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 3rd, 2009
0

Re: Hello,windows applications,help please

The Label was purely to help you experience and play with Drag & Drop (concept stuff).
Not being a Sudoko player myself, I am not all that familiar with the game board.
If I were building it, I would use graphical design methods, but those are (for now) a bit more advanced than you should tackle.

To generate 81 labels, you can create and add them at runtime within a loop. You need to perform a few steps for this.
Primarily you will create them in a for..loop, and add them to the Main form, or a panel (or some other container).

Here is a quick and dirty way, you will have to manage your own location and size values:

To add this to your code, select the form, and assign an event handler for the Load event. In the code snipet below, I just create 4 new labels, and position them on the form, add them to the container (in this case the form itself).
Assign the event handler like you did in the Label component as you did earlier.

C# Syntax (Toggle Plain Text)
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. for (int i = 0; i < 4; i++)
  4. {
  5. Label lbl = new Label();
  6. lbl.Text = i.ToString();
  7. lbl.Location = new Point( 30, 70 + (i * 20) );
  8. lbl.Size = new Size(label1.Width, label1.Height);
  9. lbl.DragDrop += label1_DragDrop;
  10. lbl.DragOver += label1_DragOver;
  11. lbl.AllowDrop = true;
  12. this.Controls.Add(lbl);
  13. }
  14. }

This being done, now you need to change the DragDrop event handler because it will be used by multiple components.

C# Syntax (Toggle Plain Text)
  1. private void label1_DragDrop(object sender, DragEventArgs e)
  2. {
  3. Label target = sender as Label;
  4. Button btn = e.Data.GetData(typeof(Button)) as Button;
  5. target.Text = btn.Text;
  6. }

All we did was cast the sender to Label, and set its text from the dropped button.

Have Fun!
//Jerry
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 3rd, 2009
0

Re: Hello,windows applications,help please

Hey , Thanks a lot again.
I think i understood some of the concepts now, ill work on it now for 2 days, and post some minor question here and there,And i'll post back what i've done in these days(i have busy week i need to get ready for the second stage of the physics Olympiad here).
The part i afraid the most is getting the values from the label to a 9*9 matrix(soduku) and maintaining the order of the labels in the form.
ok,so i'll post back soon.
Thank you very much for your help,
JooClops

P.S for making it look like a table of soduku ill have to do another for loop in the for loop right?
done ! now i shall continue
Last edited by JooClops; Apr 3rd, 2009 at 4:02 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 3rd, 2009
0

Re: Hello,windows applications,help please

another thing i encountered , i've made the 9*9 labels to drag to, but i made a table of 9*9 as well , but when i press F5 it hides the labels , i mean,it;s like the table is upon the labels, and i dont want that, i just want visual table (without coding or something) and the labels inside(coded separately) so is there a way to make the table behind the label? i can't find it in the properties of the table...but it should be there...
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 3rd, 2009
0

Re: Hello,windows applications,help please

Which "table" component are you using ?
Is this a DataGridView ? ListBox ? or something else ?

// Jerry
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 4th, 2009
0

Re: Hello,windows applications,help please

Table-Layout-Panel

Jooclops
Edit:
one more thing:
why can;t I change the size of the labels? not even in the properties of the label... I\ll make a screen shot later
thanks for your help!
Last edited by JooClops; Apr 4th, 2009 at 11:01 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 4th, 2009
1

Re: Hello,windows applications,help please

The container should be the table (panel) not "this".
So instead of creating the labels and adding them to this
C# Syntax (Toggle Plain Text)
  1. this.Controls.Add(lbl);

use
C# Syntax (Toggle Plain Text)
  1. panel1.Controls.Add(lbl);

// Jerry
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC