开发者

How to thru char into TextBox?

开发者 https://www.devze.com 2023-01-30 17:39 出处:网络
I have some TextBox on focus ( cursor is blinking on him ). Now, from other usercontrol ( that contain buttons ) i want to send event that will insert char into the TextBox when pressing any of the bu

I have some TextBox on focus ( cursor is blinking on him ). Now, from other usercontrol ( that contain buttons ) i want to send event that will insert char into the TextBox when pressing any of the buttons that are on the usercontrol.

This need to be without lose the focus from the TextBox ... ( blinking cursor on the TextBox )

How can i do开发者_高级运维 it ? ( i try to raise key down event - but its does not work )


Make your buttons to be not focusable (Focusable = false).


Do you want to use this virtual keyboard with other applications, or is it something that's only going on in your application? Beyond that, if it is only your application, do you only ever want to insert characters into one particular TextBox, or potentially any TextBox?

If it's a virtual keyboard intended to work with any application, then you'll want to use a Win32 API method like SendKeys. WinForms has an extremely easy interface for using SendKeys.

If it only ever needs to add characters to the one TextBox, then it's much more easy to modify the TextBox's Text property rather than trying to raise events on it to get the desired behavior. There's a CaretIndex property that will tell you where to insert the character. Beyond that, it's simple string concatenation.


        <StackPanel>
            <TextBox Name="MainTextBox" />
            <Button Content="A"
                    Focusable="False"
                    Click="Button_Click" />
            <Button Content="B"
                    Focusable="False"
                    Click="Button_Click" />
            <Button Content="C"
                    Focusable="False"
                    Click="Button_Click" />
            <Button Content="D"
                    Focusable="False"
                    Click="Button_Click" />
            <Button Content="E"
                    Focusable="False"
                    Click="Button_Click" />
        </StackPanel>

Code:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MainTextBox.Text += (sender as Button).Content.ToString();
    }


var text = button.Content as string;
textbox.SelectedText = text;
textbox.SelectionLength = 0;    // removing selection from inserted text
textbox.SelectionStart += text.Length;

This will insert button content at cursor position (and replace selected text) - the same as user inputted it from keyboard. PS. if textbox is unknown, it may be found with

var textbox = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this)) as TextBox;

Instead of FocusManager.GetFocusScope(this) you may put window.

If you need it not only for textboxes - WinAPI functions should help. See http://www.pinvoke.net/default.aspx/user32.sendinput

0

精彩评论

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