| | |
Hello,windows applications,help please
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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
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)
using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void buttonMouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Button btn = sender as Button; btn.DoDragDrop(sender, DragDropEffects.Copy); } } private void label1_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } private void label1_DragDrop(object sender, DragEventArgs e) { Button btn = e.Data.GetData(typeof(Button)) as Button; label1.Text = btn.Text; } } }
•
•
Join Date: Apr 2009
Posts: 44
Reputation:
Solved Threads: 0
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]
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.
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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.
This being done, now you need to change the DragDrop event handler because it will be used by multiple components.
All we did was cast the sender to Label, and set its text from the dropped button.
Have Fun!
//Jerry
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)
private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < 4; i++) { Label lbl = new Label(); lbl.Text = i.ToString(); lbl.Location = new Point( 30, 70 + (i * 20) ); lbl.Size = new Size(label1.Width, label1.Height); lbl.DragDrop += label1_DragDrop; lbl.DragOver += label1_DragOver; lbl.AllowDrop = true; this.Controls.Add(lbl); } }
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)
private void label1_DragDrop(object sender, DragEventArgs e) { Label target = sender as Label; Button btn = e.Data.GetData(typeof(Button)) as Button; target.Text = btn.Text; }
All we did was cast the sender to Label, and set its text from the dropped button.
Have Fun!
//Jerry
•
•
Join Date: Apr 2009
Posts: 44
Reputation:
Solved Threads: 0
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
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.
•
•
Join Date: Apr 2009
Posts: 44
Reputation:
Solved Threads: 0
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...
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
The container should be the table (panel) not "this".
So instead of creating the labels and adding them to this
use
// Jerry
So instead of creating the labels and adding them to this
C# Syntax (Toggle Plain Text)
this.Controls.Add(lbl);
use
C# Syntax (Toggle Plain Text)
panel1.Controls.Add(lbl);
// Jerry
![]() |
Similar Threads
- QBASIC under Windows XP (Visual Basic 4 / 5 / 6)
- Creating Windows Applications (C++)
- Skinning C# Windows Applications (C#)
- XP not able to run MS-DOS and MS Windows applications (Windows NT / 2000 / XP)
- windows error service spyware message (Viruses, Spyware and other Nasties)
- Problem with config.nt on Windows 2000 Pro (Windows NT / 2000 / XP)
- c:/windows?system32?autoexec.nt (Windows NT / 2000 / XP)
- Windows to Linux Migration (Getting Started and Choosing a Distro)
Other Threads in the C# Forum
- Previous Thread: App takes time to load after restoring from minizied state
- Next Thread: how to programmatically compile C code using C# language
Views: 2060 | Replies: 38
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ http httpwebrequest image index input install java label list listbox login mandelbrot math mysql networking operator oracle path photoshop picturebox pixelinversion prime programming radians regex remote remoting resource richtextbox save saving serialization server socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml






