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

Posted on 2010-09-18
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Accepted Solution

Matt_Lebrao earned 1000 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 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".

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

LVL 86

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.
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!


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 86

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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.
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

752 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