Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8230007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:59:23+00:00 2026-06-07T16:59:23+00:00

If I have multiple TextBoxes and other text insertion controls and I want to

  • 0

If I have multiple TextBoxes and other text insertion controls and I want to create some buttons that insert special characters into whichever TextBox is in focus, what is the best control for this and what properties should be set?

Requirements for the buttons:

  1. Buttons do not steal focus when clicked.
  2. Buttons can insert text (e.g. special characters) into any control that accepts keyboard input.
  3. The cursor should move as if the user had entered the text on the keyboard.

If #2 is not possible, it will suffice to limit the controls to only TextBoxes.

NOTE: I do not want to make the buttons unfocusable, only such that they do not steal focus when clicked.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:59:25+00:00Added an answer on June 7, 2026 at 4:59 pm

    I’m not aware of any button that is not stealing focus when it is clicked, but in button click event handle you can return focus to previous owner. If I had to implement this I’d create an behavior that is attached to parent panel of all special textboxes and all buttons that are to insert some text.

      <StackPanel>
        <i:Interaction.Behaviors>
            <local:TextBoxStateTracker/>
        </i:Interaction.Behaviors>
        <TextBox />
    
        <Button Content="description" Tag="?" />
    </StackPanel>
    

    For sample simplicity I’ve put text that is to be inserted to textbox in Tag property.

    public class TextBoxStateTracker : Behavior<Panel>
    {
    private TextBox _previouslySelectedElement;
    private int _selectionStart;
    private int _selectionLength;
    
    protected override void OnAttached()
    {
    //after control and all its children are created find textboxes and buttons
        AssociatedObject.Initialized += (x, y) =>
            {
                var textBoxElements = FindChildren<TextBox>(AssociatedObject);
                foreach (var item in textBoxElements)
                {
                    item.LostFocus += new RoutedEventHandler(item_LostFocus);
                }
    
                var buttons = FindChildren<Button>(AssociatedObject);
                foreach (var item in buttons)
                {
                    item.Click += new RoutedEventHandler(item_Click);
                }
            };
    }
    
    private void item_Click(object sender, RoutedEventArgs e)
    {
        if (_previouslySelectedElement == null) return;
        //simply replace selected text in previously focused textbox with whatever is in tag property
        var button = (Button)sender;
        var textToInsert = (string)button.Tag;
    
        _previouslySelectedElement.Text = _previouslySelectedElement.Text.Substring(0, _selectionStart)
            + textToInsert +
            _previouslySelectedElement.Text.Substring(_selectionStart + _selectionLength);
        _previouslySelectedElement.Focus();
        _previouslySelectedElement.SelectionStart = _selectionStart + textToInsert.Length;
    }
    
    private void item_LostFocus(object sender, RoutedEventArgs e)
    {
        //this method is fired when textboxes loose their focus note that this
        //might not be fired by button click
        _previouslySelectedElement = (TextBox)sender;
        _selectionStart = _previouslySelectedElement.SelectionStart;
        _selectionLength = _previouslySelectedElement.SelectionLength;
    }
    
    public List<TChild> FindChildren<TChild>(DependencyObject d)
       where TChild : DependencyObject
    {
        List<TChild> children = new List<TChild>();
    
        int childCount = VisualTreeHelper.GetChildrenCount(d);
    
        for (int i = 0; i < childCount; i++)
        {
            DependencyObject o = VisualTreeHelper.GetChild(d, i);
    
            if (o is TChild)
                children.Add(o as TChild);
    
            foreach (TChild c in FindChildren<TChild>(o))
                children.Add(c);
        }
    
        return children;
    }
    

    }

    This does more or less what you described but it is far from perfect I think it is enough to get you started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple input textboxes in my page. I want to reset particular text
Here's the situation: I have a custom TextBox control that contains multiple other TextBox
So I have a page, it had multiple textboxes, linkbuttons, and buttons. Currently, a
If I have multiple textboxes which have different ID's but all id's start with
I have a form with multiple textboxes which are created dynamically, now all these
I have a XAML window with multiple TextBoxes, each with a corresponding TextBlock tag
I have a C# Winforms program with multiple textboxes. I used the properties for
I have a Custom Control that has multiple textbox fields and a checkbox contained
I have a Form with multiple different controls like ComboBox , TextBox and CheckBox
the problem i have is that i have multiple nested master pages: level 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.