How do I create a floating text box without stealing focus from the user?

Posted on 2010-09-18
Last Modified: 2013-12-14

Let me first try to describe what I want to do.

Basically, I have a program that needs to continually be monitoring whatever the user is currently typing. At various points in time, when it has gathered sufficient information, it will show a transparent box slightly below where the user has been typing, which contains some possible autocomplete suggestions. The user can choose one of these suggestions, or just completely ignore the box and continue working.

So for example, if they were typing a memo in Microsoft Word, the suggestion box could overlay itself near where they are typing, however, without interfering with the user's workflow. That means that it is important that the box not steal focus from what the user is doing at the moment. However, there would be a special mappable hotkey, such as Ctrl-Alt-Num, where Num would correspond to one of the suggestions offered in the suggestion box, this hotkey would be redirected to my application, and the application would then paste the chosen suggestion into the currently active Windows Control.

Basically, kind of like a application-agnostic autocompletion tool.

Secondary question:
Although I'm monitoring/caching what the user is currently typing, what I would ideally prefer is the ability to see into the user's currently active windows control, determine if the control is some kind of input box such as a text box, and than iterate backwards from the cursor position to the beginning of the sentence that they were in the middle of typing.

Right now, this program is still in the design-phase, so I'm completely open to general suggestions as well as detailed help, although I would prefer the latter.

Main points in order of importance:
1. Creating a dynamic graphical box that can appear below the user's cursor without interfering with workflow.
2. Scanning the existing text in user's currently active application.
3. Trapping certain hot-keys and suppressing them from any given active application

This application is being designed in C# for Windows XP and up. Any help is much appreciated.

Shaun Christopher
Question by:shaunchristopher
  • 4
  • 3

Accepted Solution

Matt_Lebrao earned 250 total points
ID: 33709517
To create a floating TextBox set the form's following properties:
¿ FormBorderStyle = FormBorderStyle.None;
¿ BackColor = Color.Lime;
¿ TranparencyKey = Color.Lime;

So only the controls inside the form are visible.
To make it appear without stealing focus (there are a lot of complicated codes around the web some of then has DLLImport and all this hard stuff, but mine is simpler and works :D)
¿ Make the form invisible on loading
¿ Example:
        private void Form1_Load(object sender, EventArgs e)
            this.Visible = false;
¿ As the form wasn't visible when it was really open, it can't be focused.

¿ Make the form visible after it's already open
¿ Example:
        private void Form1_Shown(object sender, EventArgs e)
            this.Visible = true;
¿ As the you didn't opened the form, just made it visible, it won't steal the focus.
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 33709600
Use WS_EX_NOACTIVATE to prevent the Form from ever receiving focus.  Use WS_EX_TRANSPARENT to make it so that user clicks literally pass THRU the form and on to whatever is "below".


    "The WS_EX_NOACTIVATE value for dwExStyle prevents foreground activation by the system."


    "Specifies that a window created with this style is to be transparent. That is, any windows that are beneath the window are not obscured by the window."

Override CreateParams() to set these complicated codes necessary:
using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.Diagnostics;

public class Form1


	private const int WS_EX_TRANSPARENT = 0x20;

	private const int WS_EX_NOACTIVATE = 0x8000000;

	public Form1()



		this.BackColor = Color.Yellow;

		this.Opacity = 0.5;

		this.ShowInTaskbar = false;

		this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

		this.TopMost = true;


	protected override System.Windows.Forms.CreateParams CreateParams {

		get {

			CreateParams cp = base.CreateParams;

			cp.ExStyle = cp.ExStyle | WS_EX_NOACTIVATE | WS_EX_TRANSPARENT;

			return cp;




Open in new window

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33709603
The keyboard trapping can be done with either system wide global hotkeys or via a low level keyboard hook.
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


Expert Comment

ID: 33709606
This is great Idle_Mind!
But then how the user will select the chosen sugestion if desired?
By the way I sugest replacing the TextBox by a ListBox and adding the sugestion to it.
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33709637
"But then how the user will select the chosen sugestion if desired?"

This does make interaction with the "floating form" a little difficult eh?... ;)

The author stated though:

    "However, there would be a special mappable hotkey, such as Ctrl-Alt-Num, where Num would correspond to one of the suggestions offered in the suggestion box, this hotkey would be redirected to my application, and the application would then paste the chosen suggestion into the currently active Windows Control."

So the user would press a Hotkey combo corresponding to they entry number and the value will be pasted and I assume the form will be hidden again.

Simple Labels in a FlowLayoutPanel could be used to easily position the available suggestions.

Expert Comment

ID: 33709659
Hehe right...
But wouldn't be better if an unexpirient user (like old people) could just click on the suggestion if desired?
Neither averybody can understand what is a Hotkey combo (trust me, I know a lot of "slow" people).
making the floatable form as I described would allow that.

And another thing that I forgot...

When the suggestion is chosen (or not) instead of closing the form, just make it invisible again. (remember that invisible forms doesn't catch clicks, so user's clicking on the form while invisible will go to whatever is behind it.) and clean the suggestion on the FlowLayoutPanel with FlowLayoutPanel1.Controls.Clear(); or clean the list box with ListBox1.Items.Clear();

Expert Comment

ID: 33709661
Please forgive my English... It's not my first language.

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now