Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am new to c# programming

I want to enable user of my program to resize and move control (like Button or PictureBox).

my program will enable user to click on button (ADD) to add a new pictureBox to the form, I successfully do that. Then I tried to apply a method by set bool variable , and set it to true in Mousedown of PictureBox ,to false in Mouseup and set location of control to location of cursor in MouseMove If bool is true .
the problem that when I insert new pictureBox, I can not move the old one, all actions happen to new one only.

I hope I can explain my problem, and if not, you may tell me your idea about making a PictureBox is re-sizable or movable.

GoodBye
Posted

To move a control you only need the MouseDown and MouseMove event handlers if you do it like;
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        PictureBox picBox = sender as PictureBox;
        picBox.Tag = e.Location;
    }
}

void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    PictureBox picBox = sender as PictureBox;

    if (e.Button == MouseButtons.Left && picBox.Tag != null)
    {
        Point lastLocation = (Point)picBox.Tag;

        picBox.Top = picBox.Top + (e.Location.Y - lastLocation.Y);
        picBox.Left = picBox.Left + (e.Location.X - lastLocation.X);
    }
}
 
Share this answer
 
v2
Comments
prince_bakora 25-Oct-10 18:00pm    
Thanks
But it give me error in all (e.Button) and (e.Location) lines
Rod Kemp 25-Oct-10 18:09pm    
What is the error it gives as this code works without any errors for me?
prince_bakora 25-Oct-10 20:23pm    
Errors
======

Error 1 'System.EventArgs' does not contain a definition for 'Button' and no extension method 'Button' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

=======================

Error 4 'System.EventArgs' does not contain a definition for 'Location' and no extension method 'Location' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)
Rod Kemp 25-Oct-10 20:34pm    
The only way that error could occur is if the "MouseEventArgs e" parameter of the function is changed to simply "EventArgs e".
If I understand what you're after...

In your MouseDown event handler, the sender parameter represents the control sending the event (you have to cast it to a PictureBox (or Control) type before using it. Once you do that, you can save it to a global class variable to use elsewhere in the form.
 
Share this answer
 
v2
Comments
prince_bakora 25-Oct-10 17:59pm    
Thanks, but if you give me a simple example It will be more clear
You can see this links

Click

Click

Click
 
Share this answer
 

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