• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 826
  • Last Modified:

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


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
  • 4
  • 3
2 Solutions
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.
Mike TomlinsonMiddle School Assistant TeacherCommented:
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".

See: http://msdn.microsoft.com/en-us/library/ms632680(VS.85).aspx

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

See: http://msdn.microsoft.com/en-us/library/aa251511(VS.60).aspx

    "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 flags...no 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

Mike TomlinsonMiddle School Assistant TeacherCommented:
The keyboard trapping can be done with either system wide global hotkeys or via a low level keyboard hook.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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.
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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.
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();
Please forgive my English... It's not my first language.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now