Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I'm a bit newbie in C#. I followed some examples found to overrides mouse actions, but they're never triggered. Some help?
Thanks in advance.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Trevo.Windows.UI
{
   public class Rectangulo : Control
   {
      #region Overrides

      protected override void OnCreateControl()
      {
         this.MouseDown += new MouseEventHandler(this.Rect_MouseDown);
         //MouseMove += new MouseEventHandler(Rect_MouseDown);
         this.MouseUp += new MouseEventHandler(this.Rect_MouseDown);
         base.OnCreateControl();
      }

      #endregion


      #region Handlers

      private void Rect_MouseDown(object sender, MouseEventArgs e)
      {
         MessageBox.Show("click");
      }
      #endregion
}
Posted

First, you are not overriding the mouse events, you are handling them. I understand you are new but this is a very essential concept that you must understand before you can proceed successfully.

Right now you have a control that displays nothing and would be difficult for you click on. Try adding a label or draw a shape. You've also must understand the effect of where you place the control. If it is in a container, the container may be getting the event before it reaches your control.
 
Share this answer
 
Thanks Mark for your answer.

The code I published, was simplified. In the constructor it creates a Rectangle. That's the control that I'am trying to click

Members area
#region Miembros

// Graficos
private Graphics _Area = null;
private Font _Letra = new Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);

// Posicion
private int _Izquierda = 0; // X
private int _Arriba = 0; // Y
private int _Ancho = 0;
private int _Altura = 0;

// Estado
private bool _Seleccionado = false;
private bool _Visible = true;

// Propiedades graficas
private Color _ColorBorde = Color.Black;
private Color _ColorBordeDefecto = Color.Black;
private float _AnchoBorde = 1;
private Color _ColorRelleno = Color.White;
redondeadas

#endregion


... the constructor

public Rectangulo(Graphics pG, int pIzquierda, int pDerecha, int pAncho, int pAltura)
{
   _Area = pG;

   _Izquierda = pIzquierda;
   _Arriba = pDerecha;
   _Ancho = pAncho;
   _Altura = pAltura;
   Mostrar();
}

... and where to show the shape


public void Mostrar()
{
   //SolidBrush oSolidBrush = new SolidBrush(_ColorRelleno);
   Brush oSolidBrush = new LinearGradientBrush(new Rectangle(_Izquierda,_Arriba,_Ancho,_Altura),_ColorRelleno,Color.White,45,false);
   Pen oPen = new Pen(_ColorBorde, _AnchoBorde);

   // Dibuja el área
   _Area.FillRectangle(oSolidBrush, _Izquierda, _Arriba, _Ancho, _Altura);
   // Dibuja el borde
   _Area.DrawRectangle(oPen, _Izquierda, _Arriba, _Ancho, _Altura);
}
 
Share this answer
 
Got it Mark! I have to handle the mouse events inside the panel that contains the rectangles.
Thanks for your support.
 
Share this answer
 

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