This is really annoying. I'm using the label as part of a list item user control, where the user can click it to select the list item and double-click it to rename it. However, if you had a name in the clipboard, double-clicking the label will replace it with the text of the label!
I've also check the other labels in the application, and they will also copy to the clipboard on a doubleclick. I have not written any clipboard code in this program, and I am using the standard .NET labels.
Is there any way to disable this functionality?
I was able to do it using a combination of the other answers given. Try creating this derived class and replace any labels you wish to disable the clipboard functionality with it:
Public Class LabelWithOptionalCopyTextOnDoubleClick
Inherits Label
Private Const WM_LBUTTONDCLICK As Integer = &H203
Private clipboardText As String
<DefaultValue(False)> _
<Description("Overrides default behavior of Label to copy label text to clipboard on double click")> _
Public Property CopyTextOnDoubleClick As Boolean
Protected Overrides Sub OnDoubleClick(e As System.EventArgs)
If Not String.IsNullOrEmpty(clipboardText) Then Clipboard.SetData(DataFormats.Text, clipboardText)
clipboardText = Nothing
MyBase.OnDoubleClick(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If Not CopyTextOnDoubleClick Then
If m.Msg = WM_LBUTTONDCLICK Then
Dim d As IDataObject = Clipboard.GetDataObject()
If d.GetDataPresent(DataFormats.Text) Then
clipboardText = d.GetData(DataFormats.Text)
End If
End If
End If
MyBase.WndProc(m)
End Sub
End Class
When internal text value is empty then double clicking on label not trying to copy text value to clipboard. This method more cleaner than other alternatives I think:
using System;
using System.Windows.Forms;
public class LabelNoCopy : Label
{
private string text;
public override string Text
{
get
{
return text;
}
set
{
if (value == null)
{
value = "";
}
if (text != value)
{
text = value;
Refresh();
OnTextChanged(EventArgs.Empty);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With