Reputation: 15829
I need to remove the focus from several TextBoxes. I tried using:
textBox1.Focused = false;
Its ReadOnly
property value is true
.
I then tried setting the focus on the form, so as to remove it from all the TextBoxes, but this also fails to work:
this.Focus();
and the function returns false
when a textbox is selected.
So, how do I remove the focus from a TextBox?
Upvotes: 136
Views: 233022
Reputation: 1
I tried all of the solutions here, but none worked. However, Select() worked for me.
this.Select();//"this" still being the form.
Upvotes: 0
Reputation: 622
Kinda late to the party in 2022, however none of the solutions here worked for me (idk why) using .Net_6.0_windows, so I've come up with this solution:
Label focusoutLabel = new Label() {
Text = "",
Name = "somegenericplaceholdernamethatwillneverbeusedinmyprogram",
Visible = false,
};
this.Controls.Add(focusoutLabel);
this.ActiveControl = focusoutLabel;
^Place this code to your Form load handler^
Upvotes: 1
Reputation: 124
Try this one:
First set up tab order.
Then in form load event we can send a tab key press programmatically to application. So that application will give focus to 1st contol in the tab order.
in form load even write this line.
SendKeys.Send("{TAB}");
This did work for me.
Upvotes: 8
Reputation: 2015
Focusing on the label didn't work for me, doing something like label1.Focus()
right?
the textbox still has focus when loading the form, however trying Velociraptors
answer, worked for me, setting the Form's Active control to the label like this:
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = label1;
}
Upvotes: 78
Reputation: 4174
In the constructor of the Form or UserControl holding the TextBox write
SetStyle(ControlStyles.Selectable, false);
After the InitializeComponent(); Source: https://stackoverflow.com/a/4811938/5750078
Example:
public partial class Main : UserControl
{
public Main()
{
InitializeComponent();
SetStyle(ControlStyles.Selectable, false);
}
Upvotes: -1
Reputation: 1
Please try set TabStop
to False
for your view control which is not be focused.
For eg:
txtEmpID.TabStop = false;
Upvotes: 0
Reputation: 525
You can also set the forms activecontrol
property to null
like
ActiveControl = null;
Upvotes: 38
Reputation: 5232
If all you want is the optical effect that the textbox has no blue selection all over its contents, just select no text:
textBox_Log.SelectionStart = 0;
textBox_Log.SelectionLength = 0;
textBox_Log.Select();
After this, when adding content with .Text += "..."
, no blue selection will be shown.
Upvotes: 0
Reputation: 60124
you can do this by two method
drag two text box
tabindex value to 0 of textbox2
Upvotes: 0
Reputation: 1050
You can add the following code:
this.ActiveControl = null; //this = form
Upvotes: 89
Reputation: 1
The way I get around it is to place all my winform controls. I make all labels and non-selecting winform controls as tab order 0, then my first control as tab order 2 and then increment each selectable control's order by 1, so 3, 4, 5 etc...
This way, when my Winforms start up, the first TextBox doesn't have focus!
Upvotes: 0
Reputation: 11
//using System;
//using System.Collections.Generic;
//using System.Linq;
private void Form1_Load(object sender, EventArgs e)
{
FocusOnOtherControl(Controls.Cast<Control>(), button1);
}
private void FocusOnOtherControl<T>(IEnumerable<T> controls, Control focusOnMe) where T : Control
{
foreach (var control in controls)
{
if (control.GetType().Equals(typeof(TextBox)))
{
control.TabStop = false;
control.LostFocus += new EventHandler((object sender, EventArgs e) =>
{
focusOnMe.Focus();
});
}
}
}
Upvotes: 1
Reputation: 6045
This post lead me to do this:
ActiveControl = null;
This allows me to capture all the keyboard input at the top level without other controls going nuts.
Upvotes: 4
Reputation: 522
I made this on my custom control, i done this onFocus()
this.Parent.Focus();
So if texbox focused - it instantly focus textbox parent (form, or panel...) This is good option if you want to make this on custom control.
Upvotes: 4
Reputation: 31
I've found a good alternative! It works best for me, without setting the focus on something else.
Try that:
private void richTextBox_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
Upvotes: 3
Reputation: 13043
A simple solution would be to kill the focus, just create your own class:
public class ViewOnlyTextBox : System.Windows.Forms.TextBox {
// constants for the message sending
const int WM_SETFOCUS = 0x0007;
const int WM_KILLFOCUS = 0x0008;
protected override void WndProc(ref Message m) {
if(m.Msg == WM_SETFOCUS) m.Msg = WM_KILLFOCUS;
base.WndProc (ref m);
}
}
Upvotes: 5
Reputation: 136
It seems that I don't have to set the focus to any other elements. On a Windows Phone 7 application, I've been using the Focus method to unset the Focus of a Textbox.
Giving the following command will set the focus to nothing:
void SearchBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Focus();
}
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
It worked for me, but I don't know why didn't it work for you :/
Upvotes: 2
Reputation: 2030
Focus
sets the input focus, so setting it to the form won't work because forms don't accept input. Try setting the form's ActiveControl
property to a different control. You could also use Select
to select a specific control or SelectNextControl
to select the next control in the tab order.
Upvotes: 10