Yanshof
Yanshof

Reputation: 9926

How to thru char into TextBox?

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 )

Upvotes: 0

Views: 429

Answers (4)

Mykola Bohdiuk
Mykola Bohdiuk

Reputation: 1327

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

Upvotes: 1

decyclone
decyclone

Reputation: 30810

        <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();
    }

Upvotes: 1

dustyburwell
dustyburwell

Reputation: 5813

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.

Upvotes: 1

Andrei Pana
Andrei Pana

Reputation: 4502

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

Upvotes: 1

Related Questions