Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
why Mouse Events doesn't work in this application!!!
when the mouse down event is bone on the picturebox , no reaction is done

using System.Drawing;
using System.Windows.Forms;


namespace test
{
    public partial class Form1 : Form
    {

        bool _selecting;
        Rectangle _selection;
        //---------------------------------------------------------------------
        public Form1()
        {
            InitializeComponent();
        }
        //---------------------------------------------------------------------
        private void Form1_Load(object sender, System.EventArgs e)
        {

        }
        //---------------------------------------------------------------------
        private void pictureBox1_MouseDown(object sender,MouseEventArgs e)
        {
            // Starting point of the selection:
            if (e.Button == MouseButtons.Left)
            {
                _selecting = true;
                _selection = new Rectangle(new Point(e.X, e.Y), new Size());
               
            }
        }
        //---------------------------------------------------------------------
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            // Update the actual size of the selection:
            if (_selecting)
            {
                _selection.Width = e.X - _selection.X;
                _selection.Height = e.Y - _selection.Y;

                // Redraw the picturebox:
                pictureBox1.Refresh();
            }
        }
        //---------------------------------------------------------------------
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && _selecting)
            {


                _selecting = false;
            }
        }
        //---------------------------------------------------------------------
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (_selecting)
            {
                // Draw a rectangle displaying the current selection
                Pen pen = Pens.GreenYellow;
                e.Graphics.DrawRectangle(pen, _selection);
            }
        }
        //---------------------------------------------------------------------

    }
}
Posted

Are you sure you have wired the events. Do you have in your For1.designer.cs the following lines:
C#
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Dec-11 20:54pm    
My 5, but there is more to it. First of all, PictureBox is not needed.
Please see my solution.
--SA
Wendelius 18-Dec-11 2:35am    
Thanks :)
In addition to the problem pointed out by Mika:

Your question is no more that the indication of the fact you are not using debugger, it looks like. If you want to develop anything seriously, you need to have elementary skills of using the debugger whenever you have a problem, and at lease before asking a question like that. Also, do you understand that methods like pictureBox1_MouseUp are not events? Only the name suggests that the purpose of the method was related to using them as event handlers. Besides, such names violate (good) Microsoft naming conventions. Yes, they are generated by Microsoft Designer, so what? Who told you you are supposed to keep them? You need to renamed everything to some semantic names. Even better, avoid names at all. All you need is anonymous methods:

C#
this.pictureBox1.MouseMove += (sender, eventArgs) => { /* ... */ }

//and this syntax can work even for C# v.2, where lambda was not yet introduced:
this.pictureBox1.MouseUp += delegate(object sender, MouseEventArgs eventArgs)  { /* ... */ }

Now, more important problem:

The whole idea of using the class PictureBox for any interactive behavior you are trying to achieve is completely pointless. Even though this is possible, the PictureBox functionality does not add anything useful, but only creates unwanted hassles and eats up resources and extra development time. Instead, you need to use a custom control based on System.Windows.Forms.Control.

I explain this and what to do in my past solution: How do I clear a panel from old drawing[^].

—SA
 
Share this answer
 
Comments
Wendelius 18-Dec-11 2:35am    
That's very true, 5'd
Sergey Alexandrovich Kryukov 18-Dec-11 11:57am    
Thank you, Mika.
--SA

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