Comment faire un Winform Custom Button avec Événement Custom en C# VS 2010
Bonjour,
J’ai besoin d’un bouton qui clignote donc dont le fond change de couleur alternativement et ce suite à la réception d’un événement custom généré par un contrôle timer. En effet je lis des données sur un port série et je veux que lorsque les données sont disponible (il y en a pas toujours) que le bouton change d’aspect, en ce moment j’écris les données dans un TextBox je pourrais faire un beep mais ça devient un peu fatiguant à la longue.
J’ai bien trouvé l’article MSDN How to: Publish Events that Conform to .NET Framework Guidelines (C# Programming Guide) que j’ai adapté.
Je veux aussi que ce nouveau bouton affiche le nouvel événement dans l’inspecteur de propriété section Events de l’IDE pour pouvoir ajouter du code comme pour les autres événements. Exemple : supposons que l’usager veuille ajouter un Beep() au scintillement (je ne pense pas c’est juste un exemple).
J’ai réussi à faire quelque chose qui fonctionne mais c’est incomplet.
Voici le code de la fiche je ne sais pas mais ça ne me semble pas correct.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
…
// ------------------------------------------------------------------------------
private void timer1_Tick(object sender, EventArgs e)
{
blinkEvent.Raise(); // Class that publishes an event
}
// ------------------------------------------------------------------------------
BlinkEvent blinkEvent = new BlinkEvent(); // Class that publishes an event
// ------------------------------------------------------------------------------
private void ToggleButton_Click(object sender, EventArgs e)
{
blinkButton1.SetPublisher(blinkEvent); // Subscribers
blinkButton2.SetPublisher(blinkEvent);
timer1.Enabled = timer1.Enabled == false;
}
… |
Voici le code du control custom largement inspiré de l’article MSDN mais ce ne me semble pas la bonne forme.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace Custom_Events
{
// De: http://msdn.microsoft.com/en-us/library/w369ty8x(v=vs.100).aspx
// Define a class to hold custom event info
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(string s)
{
message = s;
}
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
}
// Class that publishes an event
class BlinkEvent
{
// Declare the event using EventHandler<T>
public event EventHandler<CustomEventArgs> RaiseCustomEvent;
public void Raise()
{
// Write some code that does something useful here
// then raise the event. You can also raise an event
// before you execute a block of code.
OnRaiseCustomEvent(new CustomEventArgs("Did something"));
}
// Wrap event invocations inside a protected virtual method
// to allow derived classes to override the event invocation behavior
protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
EventHandler<CustomEventArgs> handler = RaiseCustomEvent;
// Event will be null if there are no subscribers
if (handler != null)
{
// Use the () operator to raise the event.
handler(this, e);
}
}
}
// ------------------------------------------------------------------------------
class BlinkButton : Button
{
public void SetPublisher(BlinkEvent pub)
{
// Subscribe to the event using C# 2.0 syntax
pub.RaiseCustomEvent += HandleCustomEvent;
}
// J'aimerais pouvoir voir le nouvel event dans la liste
[Category("Events")]
[Description("Fires when the blinking event is raised")]
// Define what actions to take when the event is raised.
void HandleCustomEvent(object sender, CustomEventArgs e)
{
Text = e.Message;
if (BackColor == Color.Yellow)
{
BackColor = System.Drawing.SystemColors.Control;
}
else
{
BackColor = Color.Yellow;
}
}
}
} |
Merci.