开发者

Find active textbox WPF

I am creating a entry page for kiosk device using WPF. There a 3 text boxes in the page and keyboard(created using buttons). To perform the action when we press the key board button in want to display the text in the corresponding text box.

Need: How to find the currently focused text box.

code using:

    void buttonElement_Click(object sender, RoutedEventArgs e)
    {
        // create variable for holding string
        String sendString = "";

        try
        {
            // stop all event handling
            e.Handled = true;
            Button btn = ((Button)sender);

            // set sendstring to key
            if (btn.Content.ToString().Length == 1 && btn.CommandParameter.ToString() != btn.Content.ToString())
            {
                sendString = btn.Content.ToString();
            }
            else
            {
                sendString = btn.CommandParameter.ToString();
            }

            // sendString = ((Button)sender).CommandParameter.ToString();
            int position = txtAuto.SelectionStart;

            // if something to send
            if (!String.IsNullOrEmpty(sendString))
            {
                // if sending a string
                if (sendString.Length > 1)
                {
                    switch (sendString)
                    {
                        case "Del":
                            if (position != txtAuto.Text.Length)
                            {
                                txtAuto.Text = txtAuto.Text.Remove(position, 1);
                                txtAuto.SelectionStart = position;
                            }
                            break;

                        case "BACKSPACE":
                            if (position != 0)
                            {
                                txtAuto.Text = txtAuto.Text.Remove(position - 1, 1);
                                txtAuto.SelectionStart = position;
                            }
                            break;

                        case "Clear":
                            txtAuto.Text = string.Empty;
                            break;
                        case "ENTER":
                            popup.IsOpen = false;
                            // lbSuggestion.ItemsSource = null;

                            this.FetchSearchResult(txtAuto.Text.Trim());
                            if (lbResult.Items.Count != 0)
                            {
                                lbResult.ScrollIntoView(lbResult.Items[0]);
                            }

                            break;
                    }
                }
                else
                {
                    txtAuto.Text = txtAuto.Text.Insert(txtAuto.SelectionStart, sendString);
                    txtAuto.SelectionStart = position + 1;
                }

                // set keyboard focus
                System.Windows.Input.Keyboard.Focus(this.txtAuto);
                // set normal focus
   开发者_开发知识库             this.txtAuto.Focus();
            }
        }
        catch (Exception)
        {
            // do nothing - not important for now
            Console.WriteLine("Could not send key press: {0}", sendString);
        }
    }

This code is working fine for single textbox how to make it work for other textboxes.


Normaly the focus got lost if you click a button. So you can "save" the last focused text box in a class variable if a textbox is loosing the focus.

private TextBox _currentTextbox;

private void TextBoxLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    _currentTextbox = e.Source as TextBox;
}

attach this handler to all Text boxes and use _currentTextbox in your function.

See more at http://msdn.microsoft.com/en-us/library/aa969768.aspx


Need: How to find the currently focused text box.

You can use the FocusManager.GetFocusedElement method.


When you click the button, the button receives the focus and the textbox loses it. So, one approach would be to subscribe to the LostFocus events of all textboxes and remember which one lost the focus. The one that lost the focus last is the one that lost the focus because of the button click and hence was the one that had the focus before the click.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜