2
Answers

Cursor Position

J

J

17y
2.3k
1


I want that left click will draw rectangle and right click will locate the cursor into it.
Instead it I get the cursor in other place. What's wrong?

The code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
       
        Rectangle rect;

        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(new Pen(Color.BlueViolet), rect);
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                rect = new Rectangle(e.X, e.Y, 100, 100);
                panel1.Invalidate();
            }
            if (e.Button == MouseButtons.Right)
            {
                Cursor.Position = new Point(rect.X + 50, rect.Y + 50);
            }
        }
    }
}

Answers (2)