How to reuse event code?

Asked

Viewed 219 times

5

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

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

An example I use to capture the keystrokes and synthesize them.

I put an example of what I want to use in another form, but I put a snippet and as it is quite extensive it would look bad to copy and paste in 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");
        }}
  • What kind of code do you want to reuse? You can post an example?

  • Young man, what is sintetiza?

  • synthesises is the object created, using the Speech API, to synthesize texts..

  • Did any of the answers below solve your problem? Do you think you can accept one of them? Check out the [tour] how to do this, if you still don’t know how to do it. This helps the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site.

3 answers

4


This is very simple. Create a class with a method that can entralize 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
    }
}

Inside the Forms, use

public void Form1_KeyDown(object sender, KeyEventArgs e)
{
    SintetizadorHelper.Reproduzir(sintetiza, e.KeyCode);
}
  • Thank you very much, valeuzão

4

This is independent of being an event. This is a problem where you create a simple indirect as normal code. That is, just create a generic method with the proper parameters in an auxiliary class and call it where necessary. You don’t need anything from 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");
        }
    }

So you use:

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

Possibly you can simplify the code a little 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());
    }

I put in the Github for future reference.

This way we use mathematics to find the characters to be used as argument. 48 is the ASCII code of character "0". Then add the offset of the options obtained (obtained by the difference the code used and the first code, so if it is the first, the offset of the character will be 0, if it is the second, the offset will be 1, and so on).

1

I believe that the ideal would be to use inheritance itself, but if you don’t 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);
}
  • The ideal is really inheritance, but as my main language is not C# so I hadn’t thought of something like, I’ll just see if I can implement it here, but I believe that’s it

  • Why the ideal would use inheritance?

  • It seemed right to me in case small changes were needed for each form in particular, but now stopping to think would be indifferent to implement a override or merely add what you need before calling the method.

  • Thanks, I did kind of like you did

Browser other questions tagged

You are not signed in. Login or sign up in order to post.