How to reuse event code?

5

I have events that when pressing different keys perform some actions but I need to use this thing in more than one form , how can I do this?

I found the issue of inheritance in C # Windows Forms a little complicated.

An example that I use to capture the keystroke and synthesize it.

I put an example of what I want to use in another form, but I put a snippet and as it is very extensive it would be bad to copy and paste into other forms . In the code below when pressing the keys I use the synthesizer to play them.

 public void Form1_KeyDown(object sender, KeyEventArgs e)
  {
  if (e.KeyCode == Keys.NumPad1)
        {
            sintetiza.SpeakAsync("1");
        }
        if (e.KeyCode == Keys.NumPad2)
        {
            sintetiza.SpeakAsync("2");
        }
        if (e.KeyCode == Keys.NumPad3)
        {
            sintetiza.SpeakAsync("3");
        }
        if (e.KeyCode == Keys.NumPad4)
        {
            sintetiza.SpeakAsync("4");
        }
        if (e.KeyCode == Keys.NumPad5)
        {
            sintetiza.SpeakAsync("5");
        }
        if (e.KeyCode == Keys.NumPad6)
        {
            sintetiza.SpeakAsync("6");
        }
        if (e.KeyCode == Keys.NumPad7)
        {
            sintetiza.SpeakAsync("7");
        }
        if (e.KeyCode == Keys.NumPad8)
        {
            sintetiza.SpeakAsync("8");
        }
        if (e.KeyCode == Keys.NumPad9)
        {
            sintetiza.SpeakAsync("9");
        }
        if (e.KeyCode == Keys.NumPad0)
        {
            sintetiza.SpeakAsync("0");
        }
        if (e.KeyCode == Keys.D1)
        {
            sintetiza.SpeakAsync("1");
        }
        if (e.KeyCode == Keys.D2)
        {
            sintetiza.SpeakAsync("2");
        }
        if (e.KeyCode == Keys.D3)
        {
            sintetiza.SpeakAsync("3");
        }
        if (e.KeyCode == Keys.D4)
        {
            sintetiza.SpeakAsync("4");
        }
        if (e.KeyCode == Keys.D5)
        {
            sintetiza.SpeakAsync("5");
        }
        if (e.KeyCode == Keys.D6)
        {
            sintetiza.SpeakAsync("6");
        }
        if (e.KeyCode == Keys.D7)
        {
            sintetiza.SpeakAsync("7");
        }
        if (e.KeyCode == Keys.D8)
        {
            sintetiza.SpeakAsync("8");
        }
        if (e.KeyCode == Keys.D9)
        {
            sintetiza.SpeakAsync("9");
        }
        if (e.KeyCode == Keys.D0)
        {
            sintetiza.SpeakAsync("0");
        }}
    
asked by anonymous 16.07.2016 / 22:04

3 answers

4

This is very simple. Create a class with a method that centers the code of these events. Example:

public class SintetizadorHelper //use um nome melhor
{
    // adapte o tipo recebido por parâmetro
    public static void Reproduzir(Sintetizador sintetiza, Keys key)
    {
        if (key.KeyCode == Keys.NumPad1)
        {
            sintetiza.SpeakAsync("1");
        }

        //resto do código
    }
}

Within the forms, use

public void Form1_KeyDown(object sender, KeyEventArgs e)
{
    SintetizadorHelper.Reproduzir(sintetiza, e.KeyCode);
}
    
16.07.2016 / 22:32
4

This is not an event. This is a problem where you create a simple indirection like normal code. That is, just create a generic method with the appropriate parameters in an auxiliary class and call it where it is needed. It does not need any inheritance. It would look something like this:

static class UtilAlgumaCoisa {
    public static void ProcessaTeclas(Keys key, SpeechSynthesizer sintetiza) {
        if (key == Keys.NumPad1) {
            sintetiza.SpeakAsync("1");
        }
        if (key == Keys.NumPad2) {
            sintetiza.SpeakAsync("2");
        }
        if (key == Keys.NumPad3) {
            sintetiza.SpeakAsync("3");
        }
        if (key == Keys.NumPad4) {
            sintetiza.SpeakAsync("4");
        }
        if (key == Keys.NumPad5) {
            sintetiza.SpeakAsync("5");
        }
        if (key == Keys.NumPad6) {
            sintetiza.SpeakAsync("6");
        }
        if (key == Keys.NumPad7) {
            sintetiza.SpeakAsync("7");
        }
        if (key == Keys.NumPad8) {
            sintetiza.SpeakAsync("8");
        }
        if (key == Keys.NumPad9) {
            sintetiza.SpeakAsync("9");
        }
        if (key == Keys.NumPad0) {
            sintetiza.SpeakAsync("0");
        }
        if (key == Keys.D1) {
            sintetiza.SpeakAsync("1");
        }
        if (key == Keys.D2) {
            sintetiza.SpeakAsync("2");
        }
        if (key == Keys.D3) {
            sintetiza.SpeakAsync("3");
        }
        if (key == Keys.D4) {
            sintetiza.SpeakAsync("4");
        }
        if (key == Keys.D5) {
            sintetiza.SpeakAsync("5");
        }
        if (key == Keys.D6) {
            sintetiza.SpeakAsync("6");
        }
        if (key == Keys.D7) {
            sintetiza.SpeakAsync("7");
        }
        if (key == Keys.D8) {
            sintetiza.SpeakAsync("8");
        }
        if (key == Keys.D9) {
            sintetiza.SpeakAsync("9");
        }
        if (key == Keys.D0) {
            sintetiza.SpeakAsync("0");
        }
    }

Here's how it works:

public void Form1_KeyDown(object sender, KeyEventArgs e) {
    UtilAlgumaCoisa.ProcessaTeclas(e.KeyCode, sintetiza);
}

You can possibly simplify the code a bit by using only two conditions instead of 20:

static class UtilAlgumaCoisa {
    public static void ProcessaTeclas(Keys key, SpeechSynthesizer sintetiza) {
        if (key >= Keys.NumPad0 && key <= Keys.NumPad9) {
            sintetiza.SpeakAsync(((char)(48 + Keys.NumPad0 - key)).ToString());
        }
        if (key >= Keys.D0 && key <= Keys.D9) {
            sintetiza.SpeakAsync(((char)(48 + Keys.D0 - key)).ToString());
        }
    }

In this way math is used to find the characters to use as an argument. 48 is the ASCII character code "0". Then the displacement of the obtained options is summed (obtained by the difference the code used and the first code, so if it is the first, the displacement of the character will be 0, if it is the second, the displacement will be 1, and so on).

    
16.07.2016 / 22:36
1

I believe the ideal would be to use inheritance myself, but if you do not want to use inheritance, then you can create a class with a static method, something like:

public static void KeyPressed(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.NumPad1)
    {
        sintetiza.SpeakAsync("1");
    }
    if (e.KeyCode == Keys.NumPad2)
    {
        sintetiza.SpeakAsync("2");
    }
    if (e.KeyCode == Keys.NumPad3)
    {
        sintetiza.SpeakAsync("3");
    }
    if (e.KeyCode == Keys.NumPad4)
    {
        sintetiza.SpeakAsync("4");
    }
    if (e.KeyCode == Keys.NumPad5)
    {
        sintetiza.SpeakAsync("5");
    }
    if (e.KeyCode == Keys.NumPad6)
    {
        sintetiza.SpeakAsync("6");
    }
    if (e.KeyCode == Keys.NumPad7)
    {
        sintetiza.SpeakAsync("7");
    }
    if (e.KeyCode == Keys.NumPad8)
    {
        sintetiza.SpeakAsync("8");
    }
    if (e.KeyCode == Keys.NumPad9)
    {
        sintetiza.SpeakAsync("9");
    }
    if (e.KeyCode == Keys.NumPad0)
    {
        sintetiza.SpeakAsync("0");
    }
    if (e.KeyCode == Keys.D1)
    {
        sintetiza.SpeakAsync("1");
    }
    if (e.KeyCode == Keys.D2)
    {
        sintetiza.SpeakAsync("2");
    }
    if (e.KeyCode == Keys.D3)
    {
        sintetiza.SpeakAsync("3");
    }
    if (e.KeyCode == Keys.D4)
    {
        sintetiza.SpeakAsync("4");
    }
    if (e.KeyCode == Keys.D5)
    {
        sintetiza.SpeakAsync("5");
    }
    if (e.KeyCode == Keys.D6)
    {
        sintetiza.SpeakAsync("6");
    }
    if (e.KeyCode == Keys.D7)
    {
        sintetiza.SpeakAsync("7");
    }
    if (e.KeyCode == Keys.D8)
    {
        sintetiza.SpeakAsync("8");
    }
    if (e.KeyCode == Keys.D9)
    {
        sintetiza.SpeakAsync("9");
    }
    if (e.KeyCode == Keys.D0)
    {
        sintetiza.SpeakAsync("0");
    }
}

And within each Form you just call the responsible method.

public void Form1_KeyDown(object sender, KeyEventArgs e)
{
    ClasseRecemCriada.KeyPressed(sender, e);
}
    
16.07.2016 / 22:27