2

When I double-click a LinkLabel in WindowsForms it copies its text; how can I prevent this?

BTW, it's a .Net 2.0 application, if that makes any difference.

Thanks

TheBlueSky
  • 5,526
  • 7
  • 35
  • 65
  • its text means link label text? If yes, there must be a code written behind for this. – Nikhil Agrawal Sep 08 '12 at 05:35
  • No, there is no code written behind that copies anything. Try it yourself; add a LinkLabel to a Form, double-click it and go paste in NotePad. – TheBlueSky Sep 08 '12 at 05:43
  • It doesn't seem to be specific to `LinkLabel` - `Label` behaves the same way (`LinkLabel` derives from `Label`). – ChrisWue Sep 08 '12 at 06:49

3 Answers3

2

You can always clear the clipboard using :

Clipboard.Clear();

Update :

You can use this code in mouse double click event.

Try this :

private void linkLabel1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        Clipboard.Clear();
    }

Update 2 :

Use these following codes it won't copy value of linklable and also it keeps your clipboard. you must use these codes with mouse enter event and mouse double click event.

Try this :

public string str;

    private void linkLabel1_MouseEnter(object sender, EventArgs e)
    {
        str = Clipboard.GetText();
        linkLabel1.MouseDoubleClick+=new MouseEventHandler(linkLabel1_MouseDoubleClick);
    }

    private void linkLabel1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        Clipboard.SetText(str);
    }
Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
  • 1
    No, that seems like a hack; I don't want to clear my users' Clipboard :) – TheBlueSky Sep 08 '12 at 05:44
  • I couldn't find a solution to override the default behaviour. I marked this as an answer because it's a workaround that can be used in some situations. If nayone has better solution please share. – TheBlueSky Oct 26 '12 at 07:14
1

It seems that this behaviour is built into the LinkLabel and that there's no way to override it.

Testing reveals the clipboard has already changed by the time the MouseDoubleClick event is triggered.

FWIW, I've never needed this control - a regular Label with some styling and use of the MouseEnter/MouseLeave events has served me well in many projects.

Bevan
  • 43,618
  • 10
  • 81
  • 133
0

What you can do is to create your own label and derive it from Control as public class MyLabel : Control and then draw the text in it yourself as

protected override void OnPaint(PaintEventArgs e)
{
        SolidBrush TextBrush = new SolidBrush(this.ForeColor);
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter );
}
liftarn
  • 429
  • 3
  • 21