tags:

views:

180

answers:

3

Hi, I'm new to C#. Please help me with this: I want to click inside a square and then, an "X" should appear. How to implement this is C#. Please help.

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

namespace VTest
{
    public partial class Form1 : Form
    {
        Rectangle rect; // single rect
  int sqsize, n;
        int margin;

        public Form1()
        {
            n = 3;
            margin = 25;
            sqsize = 50;
            rect = new Rectangle(10, 10, 150, 150);
InitializeComponent();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
          what goes here?
        }
private void Form1_Paint(object sender, PaintEventArgs e)
        {
whatg goes here?
          }
private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
what goes here?
      }

enter code here
A: 

You may want to put a homework tag on this, but you don't need both a mousedown and mouseup event handler.

Pick one to react to, I tend to react to the MouseDown event instead.

But, when you will want to look at the MouseEventArgs properties and you should be able to determine if you are inside the square.

You will probably want to call:

System.Diagnostics.Debug.WriteLine(...)

using the x and y properties in MouseEventArgs, so you can see where the mouse clicks are, and determine when you are in the square.

Once you are there, then you can draw the X.

You may want to write a function to draw an X and test it by having it draw an X at 300,300 so that you can ensure it looks as you want, while you are experimenting with MouseDown.

James Black
+6  A: 

In your MouseDown event, determining whether the click has occurred within your rectangle is easy:

if (rect.Contains(e.Location))
{
    // the user has clicked inside your rectangle
}

Drawing the "X" on the form is also easy:

Graphics g = this.CreateGraphics();
g.DrawString("X", this.Font, SystemBrushes.WindowText,
    (float)e.X, (float)e.Y);

However, the "X" in this case will not be persistent, meaning that if you drag another form over your form and then move it away, the "X" will not be there anymore. To draw a persistent "X", create a form-level Point variable like this:

private Point? _Xlocation = null;

Use your MouseDown event to set this variable if the user clicks in your Rectangle:

if (rect.Contains(e.Location))
{
    _Xlocation = e.Location;
    this.Invalidate(); // this will fire the Paint event
}

Then, in your form's Paint event, draw the "X":

if (_Xlocation != null)
{
    e.Graphics.DrawString("X", this.Font, SystemBrushes.WindowText,
        (float)e.X, (float)e.Y);
}
else
{
    e.Graphics.Clear(this.BackColor);
}

If you want the "X" to then disappear when the user lets go of the mouse button, just put this code in the MouseUp event:

_Xlocation = null;
this.Invalidate();

You can make this as much more complicated as you like. With this code, the "X" will be drawn just below and to the right of wherever you click on the form. If you want the "X" to be centered on the click location, you can use the Graphics object's MeasureString method to determine how high and how wide the "X" will be, and offset the DrawString location accordingly.

MusiGenesis
A: 

Update: I like the Rectangle.contains(location) method demonstrated by MusiGenesis.

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

namespace DemoWindowApp
{
    public partial class frmDemo : Form
    {
        Rectangle rec;
        public frmDemo()
        {
            InitializeComponent();
        }

        private void frmDemo_Load(object sender, EventArgs e)
        {
            rec = new Rectangle(150,100,100,100);
        }

        private void frmDemo_Paint(object sender, PaintEventArgs e)
        {   
            Pen p = new Pen(Color.Blue);
            Graphics g = e.Graphics;

            g.DrawRectangle(p,rec);
        }

        private void frmDemo_MouseMove(object sender, MouseEventArgs e)
        {
            if(rec.Contains(e.Location))
            {
                Cursor = Cursors.Cross;
            }else
            {
                Cursor = Cursors.Default;
            }
        }

        private void frmDemo_MouseDown(object sender, MouseEventArgs e)
        {   
            if(rec.Contains(e.Location))
            {
                //mouse position adjust for window postion and border size.
                //you may have to adjust the borders depending your
                //windows theme
                int x = MousePosition.X - this.Left -  4;
                int y = MousePosition.Y - this.Top  - 29;

                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Black);
                Point p1 = new Point(x-10,y-10);
                Point p2 = new Point(x+10,y+10);
                Point p3 = new Point(x-10,y+10);
                Point p4 = new Point(x+10,y-10);

                g.DrawLines(p,new Point[]{p1,p2});
                g.DrawLines(p,new Point[]{p3,p4});
            }
        }
    }
}
J.Hendrix