I need the LinkLabel control to not change Focus when it is clicked. I managed to use this.SetStyle(ControlStyles.Selectable, false) for a button, like so:
class NoSelectButton : Button
{
public NoSelectButton()
{
// Button does not take focus when clicked
this.SetStyle(ControlStyles.Selectable, false);
}
}
But, doing this same thing with LinkLabels does not work.
class NoSelectLinkLabel : LinkLabel
{
public NoSelectLinkLabel()
{
// Link Label still gets focus when clicked
this.SetStyle(ControlStyles.Selectable, false);
}
}
Does anyone have any insight into how I can get this to work the way I want it to? My impression from MSDN is that doing any manipulation to the focused control in the “GotFocus”, “LostFocus” and related events is a bad idea (from the “Caution” note here: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx).
Here is a crude example that shows the behavior I’m seeing:
using System.Windows.Forms;
namespace LinkLabelTests
{
public class Form1 : Form
{
NoSelectLinkLabel nsll;
NoSelectButton nsb;
TextBox tb;
public Form1()
{
this.SuspendLayout();
this.Width = 0;
this.Height = 0;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
nsll = new NoSelectLinkLabel();
nsll.Text = "Link Label";
nsll.Top = this.Bottom;
this.Controls.Add(nsll);
nsb = new NoSelectButton();
nsb.Text = "Button";
nsb.Top = nsll.Bottom;
this.Controls.Add(nsb);
tb = new TextBox();
tb.Multiline = true;
tb.Text = "Select this text, then click the button or link";
tb.Width = 200;
tb.Height = 100;
tb.Top = nsb.Bottom;
this.Controls.Add(tb);
this.ResumeLayout();
}
}
}
Are you trying to maintain the selection in your TextBox when it loses the focus? If so, check out TextBoxBase.HideSelection. Setting it to false will allow you to achieve that.
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.hideselection.aspx