0

I'm working on a small diagram program in Winforms. I have currently been able to create an array of rectangles but I am just not sure how to implement them to be selected via a mouse click and be able to show the properties (i.e like the name) of each individual rectangle.

This is what I have right now.

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

namespace NodeGraph
{
    public partial class NodeGraphPanel : UserControl
    {
        Point cursorPoint;
        Rectangle[] NodeRectange = new Rectangle[1];
        public string name;
        int id = 0;

        public NodeGraphPanel()
        {
            InitializeComponent();
        }

        public void DrawText()
        {
            cursorPoint = PointToClient(Cursor.Position);
            for(int i = 0; i < NodeRectange.Length; i++)
            {

                NodeRectange[i] = new Rectangle();
                NodeRectange[i].Size = new Size(200, 100);
                NodeRectange[i].Location = cursorPoint;
                name = "NodeRectange" + id;
                id++;

                Pen pen = new Pen(Color.Black, 2);
                pen.Alignment = PenAlignment.Center;

                Graphics g;
                g = this.CreateGraphics();

                SolidBrush myBrush = new SolidBrush(Color.FromArgb(0, 174, 219));
                SolidBrush myBrush2 = new SolidBrush(Color.FromArgb(85, 85, 85));

                using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
                {
                    g.DrawString(name, font1, Brushes.Black, NodeRectange[i].X,NodeRectange[i].Y);
                    g.DrawRectangle(pen, NodeRectange[i]);
                    g.FillRectangle(myBrush, NodeRectange[i]);
                    g.FillRectangle(myBrush2, new Rectangle(cursorPoint.X, cursorPoint.Y, 200, 30));

                    g.Dispose();
                    myBrush.Dispose();
                }
                label1.Text = name;

            }

        }
    }
}

I tried the Line example shown and it worked. But I changed it to a Rectangle class and recreated it to fit the Rectangle method. When running I don't have any errors, but it does not draw. Here is the code:

class Rectangle
{

    public Point LocX { get; set; }
    public Point LocY { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }

    public Rectangle(Point x, Point y, int v1, int v2)
    {
        LocX = x; LocY = y; Width = v1; Height = v2;
    }

    public void Draw(Graphics G)
    { using (Brush brush = new SolidBrush(Color.AliceBlue)) G.FillRectangle(brush, LocX.X,LocY.Y,Width,Height); }
}

And this is what I am using to run it:

List<Rectangle> rectangles = new List<Rectangle>();

rectangles.Add(new Rectangle(new Point(cursorPoint.X), new Point(cursorPoint.Y),100,50));
jermy9999
  • 13
  • 1
  • 5
  • 2
    A `Rectangle` is not "clickable". So you have a decision to make: 1) Ditch `Rectangle`, and implement your rectangles as some rectangular WinForms object that is already clickable, like a Form or a Button, or 2) Keep `Rectangle`, create code to manually track each Rectangle's location, and when someone clicks on the Form, use your own code to determine if a Rectangle was at that location on the Form, and take appropriate action. – mbeckish May 06 '16 at 17:50
  • 3
    Don't use `CreateGraphics` in that way. All drawing should be in the `OnPaint` override, using the graphics object supplied. – Chris Dunaway May 06 '16 at 17:53
  • a) do take the advice in the comments above. b) see [here](http://stackoverflow.com/questions/32919918/how-to-draw-line-and-select-it-in-panel/32920894?s=1|0.1992#32920894) for an example that does that with an unnamed List of lines. c) Avoid Arrays, go for List instead or n your case maybe for a Disctionary! – TaW May 06 '16 at 17:55
  • 1
    Depends on how much interaction/reaction you want, but you should consider a `UserControl` that encapsulates both the rendering and events/behaviors. Could get into loads of work, but the payoff is many times more control over all of it. – DonBoitnott May 06 '16 at 19:16
  • Sorry for late reply. I tried both examples and I think the List<> would be the better option for my case as I played around with the UserControl but didn't get the results I wanted. I tried the List<> using the example and recreated it for a Rectangle. Now when I run the code it does not give me any errors, but does not show the rectangle. I updated the question with the new code. – jermy9999 May 09 '16 at 07:15
  • Sorry for the bump, I managed to get it to draw using the Invalidate(), but now its not drawing at the mouse position. Using cursorPoint = PointToClient(Cursor.Position); and setting it to the new points for the rectangle. – jermy9999 May 09 '16 at 07:34

0 Answers0