Domanda

Iam creating this project which uses on screen keyboard. The problem is I have several textboxes in my window. The question is, when I selected the textbox and start using the on screen keyboard, the text should be displayed on the textbox that I selected.

there will be a possibility that not all the textbox are being used depending on the user's preference.

Here's my sample code when I click the button

private void button_numeric_1_Click(object sender, RoutedEventArgs e)     
{

       if (txtThousand.Focus())
        {

            txtThousand.Text += "1";
            // txtThousand.Focus();
            txtThousand.SelectionStart = txtThousand.Text.Length;

        }
        else  if (txtFivehundred.Focus())
        {

            txtFivehundred.Text += "1";
            txtFivehundred.Focus();
            txtFivehundred.SelectionStart = txtThousand.Text.Length;

        }
    }

My problem now is how can I determine which text box is active.

When I used this code:

private void StartKeyBoardProcess(TextBox tb)   
{
        try
        {
            if (tb != null)
            {
                MessageBox.Show("Pumasok!");
                tb.Focus();

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error");
        }
}

private void txtThousand_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    TextBox tb = sender as TextBox;
    StartKeyBoardProcess(tb);
}

Nothing happens.

When I Click my button, It only input text in the First Textbox. When I click the other textbox, It continue inputting in the first text box.

Can anyone tell me how to work this out? I'm so new with WPF.

È stato utile?

Soluzione

Something like this is the simple solution. You call your method with the textbox you clicked on etc. and start the process and force focus to the texbox you touched.

 private void StartKeyBoardProcess(Textbox tb) {
    try {
      if (tb != null) {
          Process.Start("osk.exe", "/C");
          tb.Focus();
        }
    }
    catch (Exception ex) {
      Messagebox.Show("Error: StartKeyBoardProcess: "+ex);
    }
  }

 private void TextBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
 {
   Textbox tb = sender as Textbox;
   StartKeyBoardProcess(tb);
 }

Something like this :)

Altri suggerimenti

I use PreviewMouseLeftButtonDown event:

private void txtThousand_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    TextBox tb = sender as TextBox;
    StartKeyBoardProcess(tb);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top