Search code examples
c#winformson-screen-keyboard

Better solution for making on-screen keyboard


I'm trying to make on-screen keyboard (button A, button B, etc). When you press button it does add character to TextBox.

Everything is working fine but if i will create like 30+ chars my code will be huge.

Any possible way to make it shorter? Code at the moment for 3 buttons.

// Method for each button
private void tastaturasIevade(TextBox varda_ievade, string burts)
{
    if (varda_ievade.TextLength == 0)
    {
        varda_ievade.Text = burts;
    }
    else
    {
        varda_ievade.Text = varda_ievade.Text + burts;
    }
}

// Writing buttons from on-screen keyboard
private void btn_A_Click(object sender, EventArgs e)
{
    tastaturasIevade(txt_VardaIevade, "a");
}

private void btn_B_Click(object sender, EventArgs e)
{
    tastaturasIevade(txt_VardaIevade, "b");
}

private void btn_C_Click(object sender, EventArgs e)
{
    tastaturasIevade(txt_VardaIevade, "c");
}

Solution

  • Why you even needs to write code? As a better and more logical solution you can use the on-screen keyboard application that comes with windows for this purpose:

    System.Diagnostics.Process.Start("osk.exe");