mardi 31 mars 2015

C#-4.0 Winforms inherited/sub-classed Button control OnClick not firing

In C# Winforms inherited/sub-classed Button control subscribing to OnMouseDown, OnMouseUp OnPaint and OnClick events. public class MyControl : Button The OnClick Event will not fire within the control or pass to the parent control the raised event. As soon as the the subclass is changed to type UserControl the OnClick event becomes active and the OnMouseDown, OnMouseUp, OnPaint events stop working within the control let alone within the parent. What can be done to force the control class to subscribe to the button control subclass's events when using the overrides? Supplied is the entire Class.



using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace testbutton
{
public class EXButton : Button
{
MouseState state = new MouseState();
private Image _leftImage;
[Browsable(true), CategoryAttribute("Appearance"),
Description("Get/Set button Left Image"),System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
public Image LeftImage
{
get { return _leftImage; }
set
{
_leftImage = value;
this.Invalidate();
}
}

private Image _borderImage;
[Browsable(true), CategoryAttribute("Appearance"),
Description("Get/Set button Border Image"),System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
public Image BorderImage
{
get { return _borderImage; }
set
{
_borderImage = value;
this.Invalidate();
}
}

private Image _rightImage ;
[Browsable(true), CategoryAttribute("Appearance"),
Description("Get/Set button Right Image"),System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
public Image RightImage
{
get { return _rightImage; }
set
{
_rightImage = value;
this.Invalidate();
}
}

private Image _hoverIamge;
[Browsable(true), CategoryAttribute("Appearance"),
Description("Get/Set button Background Image of hover state"),System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
public Image HoverImage
{
get { return _hoverIamge; }
set
{
_hoverIamge = value;
this.Invalidate();
}
}

private Image _downIamge;
[Browsable(true), CategoryAttribute("Appearance"),
Description("Get/Set button Background Image of down/click state"),System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
public Image DownImage
{
get { return _downIamge; }
set
{
_downIamge = value;
this.Invalidate();
}
}

[Description("Extended Button Control")]
public EXButton()
{
this.InitializeComponent();
state = MouseState.Normal;
}

[Description("On Click event")]
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("Button Clicked");
//This Message will only appear if the subclass is set to USERCONTROL
}

[Description("On Paint event")]
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

e.Graphics.Clear(this.BackColor);
if(_rightImage !=null) this.Height = _rightImage.Height;
if (state == MouseState.Normal)
{
if(BackgroundImage !=null ) e.Graphics.DrawImage(BackgroundImage, new Rectangle(0, 0, this.Width, this.Height));
}
if (state == MouseState.Enter)
{
if(_hoverIamge !=null) e.Graphics.DrawImage(_hoverIamge, this.ClientRectangle);
}
if (state == MouseState.Down)
{
if(_downIamge !=null) e.Graphics.DrawImage(_downIamge, new Rectangle(0, 0, this.Width, this.Height));
}
if(_rightImage !=null ) e.Graphics.DrawImage(_rightImage, new Rectangle(this.Width - _rightImage.Width, 0, _rightImage.Width, _rightImage.Height));
if(_leftImage !=null) e.Graphics.DrawImage(_leftImage, new Rectangle(0, 0, _leftImage.Width, this.Height));
if(_borderImage !=null) e.Graphics.DrawImage(_borderImage, new Rectangle(0, 0, this.Width, this.Height));

using (SolidBrush drawBrush = new SolidBrush(ForeColor))
{
StringFormat format = new StringFormat();
format.LineAlignment = StringAlignment.Center;
format.Alignment = StringAlignment.Center;
e.Graphics.DrawString(Text, this.Font, drawBrush, new Rectangle(0, 0, this.Width, this.Height), format);
}
}


[Description("On Mouse Enter event")]
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
state = MouseState.Enter;
}

[Description("On Mouse Leave event")]
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseEnter(e);
state = MouseState.Normal;
}

[Description("On Mouse Click event")]
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
}

[Description("On Mouse Down event")]
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
state = MouseState.Down;
}

[Description("On Mouse Up event")]
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseDown(e);
state = MouseState.Enter;
}

#region Component Designer generated code
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}



/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}

#endregion
}

enum MouseState
{
Enter,
Normal,
Down
}
}


As you can see I have a MessageBox in the Controls OnClick event and the message never shows.



[Description("On Click event")]
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("Button Clicked");
//This Message will only appear if the subclass is set to USERCONTROL
}


Thanks in advance for any responses.


Aucun commentaire:

Enregistrer un commentaire