1

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");
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Roberts Šensters
  • 585
  • 1
  • 7
  • 23

4 Answers4

3

Yes, there is. You can set the "Tag"-Property of the Controls manually, then apply the same Handler to all the Click-Events and add the Content of the "Tag" to the TextBox accordingly.

TextBox.Text += (string)(((Button)sender).Tag);
NikxDa
  • 4,137
  • 1
  • 26
  • 48
  • You can use .Text as well since it is the character your are "typing." I'd use the .Tag as a flag to allow upper case or not then iterate over all with a .ToUpper or .ToLower to deal with Shift. Also you can tie all "keys" to the same click event and just use the Sender.Text as the value. – rheitzman Jan 06 '16 at 22:29
  • I like the idea, though I doubt it'd really work out because some keys need to store up to 3 different characters, making this approach harder. It's easier to just store all the characters in the Tag-Property and then append the first, second or third character in the .Tag-Property, according to which modifier keys are active. – NikxDa Jan 06 '16 at 22:33
1

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");
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • This is quite nice. Still i want to write my own. But thanks for this! – Roberts Šensters Jan 06 '16 at 20:38
  • And for w8 and above, you can just enable focus tracking and it works like a touch application. See [How can I make the touch keyboard appear automatically when focus enters an edit control in my program?](https://blogs.msdn.microsoft.com/oldnewthing/20150601-00/?p=45481) – Mitch Jan 06 '16 at 20:39
0

Set the tag property for each button in the designer to the corresponding text. Then, under your form initialize event, add a single event handler for all the buttons. In tastaturasIevade method use the tag property of the button.

swiftcode
  • 195
  • 9
0

Simple example using .Text of the keys. A .Tag="c" is a character. More complex tags can be used for logic flow. btnQ is the only named button in the simple example. Shift has a dedicated click event.

Sub ShiftCaseLower()
    For Each c As Control In FlowLayoutPanel1.Controls
        If c.Tag = "c" Then c.Text = c.Text.ToLower
    Next
End Sub
Sub ShiftCaseUpper()
    For Each c As Control In FlowLayoutPanel1.Controls
        If c.Tag = "c" Then c.Text = c.Text.ToUpper
    Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, btnQ.Click
    ' all "key" click events handled here
    TextBox1.AppendText(sender.text)
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
    ' Shift key
    If Asc(btnQ.Text) = Asc("Q") Then
        ShiftCaseLower()
    Else
        ShiftCaseUpper()
    End If
End Sub
rheitzman
  • 2,247
  • 3
  • 20
  • 36