3

The code can generate rectangles (Rectangle rectangle) at runtime. The position of rectangles may change according to users' choices.

I want to add code in the method where it creates rectangles to make the rectangles clickable. And after user clicking the rectangle, there will be a new window to show content just like text.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
Axess
  • 101
  • 2
  • 10

3 Answers3

3

You can use Contains method of the Rectangle object.

private Rectangle _myRectangle;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (this._myRectangle.Contains(e.Location))
    {

    }
}
Hyralex
  • 990
  • 1
  • 8
  • 25
  • @user1490952 what more do you want explained? If the `if` case here is triggered then you clicked the Rectangle. – default Jun 29 '12 at 11:32
  • but this will not see if they click the border and the border may be thick too. see my answer here http://stackoverflow.com/questions/4816297/how-to-know-if-a-graphicspath-contains-a-point-in-c-sharp/34378138#34378138 – barlop Dec 20 '15 at 06:04
  • This might of worked in 2012, but doesn't work anymore. Once a Rectangle is drawn on the form, the mouse over for the form isn't triggered, where the Rectangle exists. – Chizl Jun 22 '22 at 17:55
0

Create a label control with the border property and transaparent background(so that it will look as rectangle) and add click event handler for each label you add. It will be good if you create your own Rectangle control by deriving from Label class or you can create your own control(Many other solutions).

Sudhakar B
  • 1,465
  • 9
  • 16
  • This will work, but depending on what your doing, labels mixed with Draw, cause the labels to flicker during repaint of the DrawRectangle() method. – Chizl Jun 22 '22 at 17:58
0

I would consider handling the click event on the window itself (or whatever your "background" control is), getting its coordinates, and comparing those to the known locations/sizes of your rectangles.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
  • 2
    Sorry - we're not really in the business of writing chunks of code! - I've got my own work to do! Have a go and we'll help if you have problems – Jon Egerton Jun 29 '12 at 11:14