How to Remove the Focus from a Textbox in Winforms

How to remove the focus from a TextBox in WinForms?

You need some other focusable control to move the focus to.

Note that you can set the Focus to a Label. You might want to consider where you want the [Tab] key to take it next.

Also note that you cannot set it to the Form. Container controls like Form and Panel will pass the Focus on to their first child control. Which could be the TextBox you wanted it to move away from.

How to make TextBox lose its focus?

    /// <summary>
/// Makes virtual keyboard disappear
/// </summary>
/// <param name="sender"></param>
private void LoseFocus(object sender) {
var control = sender as Control;
var isTabStop = control.IsTabStop;
control.IsTabStop = false;
control.IsEnabled = false;
control.IsEnabled = true;
control.IsTabStop = isTabStop;
}

/// <summary>
/// Makes virtual keyboard disappear when user taps enter key
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LooseFocusOnEnter(object sender, KeyRoutedEventArgs e) {
if (e.Key == Windows.System.VirtualKey.Enter) {
e.Handled = true; LoseFocus(sender);
}
}

It's ugly. But it works. The key part is IsTabStop property. If I don't touch it - the keyboard disappers for a fraction of a second and reapears again.

Remove Focus from TextBox by Clicking Anywhere/Anything Else

In order to detect if the mouse is clicked anywhere outside of the TextBox would require a mouse hook, or you can try a simple hack by placing a timer on your UserControl:

public UserControl1() {
InitializeComponent();
mouseTimer.Interval = 16;
mouseTimer.Tick += mouseTimer_Tick;
mouseTimer.Enabled = true;
}

private void mouseTimer_Tick(object sender, EventArgs e) {
if (this.ActiveControl != null && this.ActiveControl.Equals(textBox1)) {
if (MouseButtons != MouseButtons.None) {
if (!textBox1.ClientRectangle.Contains(textBox1.PointToClient(MousePosition))) {
if (this.ParentForm != null) {
this.ParentForm.ActiveControl = null;
} else {
this.ActiveControl = null;
}
}
}
}
}

Remove focus from textbox when it is only editable control

set the text box to:

tabindex = -1

or

tabstop = false

The above depends on the type of control you are using, whether it is a asp control or html control.
Then add a label like:

<label id="lblHide" name="lblHide" hidden="hidden">

then set focus to that label.

UPDATE

So, here's what it will look like for you:

<asp:TextBox ID="TextBox1" runat="server" TabIndex="-1" ReadOnly="True"></asp:TextBox>
<asp:Label ID="Label1" runat="server"></asp:Label>

and then on page load this:

protected void Page_Load(object sender, EventArgs e)
{
Label1.Focus();
}

Destroy/completely remove focus programmatically

You could always use:

Keyboard.ClearFocus();

How to change the text of focused textbox in C#?

By the time the click event for the button has fired, the button now has focus instead of the textbox. So you need to capture which textbox last had the focus and use that.

Here is a rough and quick implementation, which should work as long as all your textboxes are on the form on load. It will work even with textboxes which aren't a direct child of the form (for example, contained within a panel or tab page):

    public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();

return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}

private TextBox lastFocussedTextbox;

private void Form1_Load(object sender, EventArgs e)
{
foreach(TextBox textbox in GetAll(this, typeof(TextBox)))
{
textbox.LostFocus += (_s, _e) => lastFocussedTextbox = textbox;
}
}

private void button1_Click(object sender, EventArgs e)
{
if(lastFocussedTextbox != null)
{
lastFocussedTextbox.Text = button1.Text;
}
}

Credit for GetAll function: https://stackoverflow.com/a/3426721/13660130

How to prevent text box from gaining focus unless clicked?

textBox1.TabStop = false;

The above should stop it receiving focus from tabbing.

How can I remove the input focus from textbox?

Hmm, not sure if I understand. If the user can type into the edit box, then it can have focus. If he clicks outside of it, on a blank are of the form, then it loses focus.

If you want to be able to 1) type into the edit box and 2) move the edit box, then you need a separate mechanism to enter "move mode".

I would suggest either a "click here to move selected control" button, or a right-click context menu on the control with a "move control option".

You would also have to conisder how the user indicates that moving has ended.

Hope this helps.



Related Topics



Leave a reply



Submit