dotnet / dotnet/wpf

Copying the selected text of a TextBox causes WM_CLIPBOARDUPDATE message to be sent twice

Open
#8,448 0 comments 0 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

When a text is selected and copied from a TextBox with the Ctrl+C hotkey or by using the Copy command, it causes [WM_CLIPBOARDUPDATE](https://learn.microsoft.com/en-us/windows/win32/dataxchg/wm-clipboardupdate) message to be sent twice.

### Reproduction Steps

1) Compile and run the following repo: https://github.com/rampaa/TextBoxDoubleCopyDemo
2) Observe how using the Ctrl+C or the Context Menu->Copy command to copy the text from the TextBox increases the copy counter by 2 instead of 1. Also observe how Context Menu->Handcrafted Copy button increases the copy counter by 1. Also observe how copying a text from, say, Notepad increases the copy counter by 1.

### Expected behavior

Copying the selected text should cause WM_CLIPBOARDUPDATE message to be sent only once.

### Actual behavior

Copying the selected text causes WM_CLIPBOARDUPDATE message to be sent twice.

### Regression?

I can reproduce the issue with .NET 6/7/8. I didn't test it with .NET Framework.

### Known Workarounds

Instead of using the built-in copy command, using a handcrafted copy method like I did in the aforementioned repository will prevent WM_CLIPBOARDUPDATE message from being sent twice.

As for fixing the Ctrl+C hotkey, subscribing to the PreviewKeyDown event of the TextBox and handling it like the following will prevent WM_CLIPBOARDUPDATE message from being sent twice:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (keyGesture is { Modifiers: ModifierKeys.Control, Key: Key.C })
{
e.Handled = true;
CopyTextToClipboard(MainTextBox.SelectedText);
}
}

### Impact

_No response_

### Configuration

>Which version of .NET is the code running on?

.NET 8

>What OS and version, and what distro if applicable?

Windows 10 22H2

>What is the architecture (x64, x86, ARM, ARM64)?

x64

>Do you know whether it is specific to that configuration?

I don't think it's specific to this configuration.

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.