dotnet / dotnet/winforms

Correct `TextBox.PlaceHolderText` implementations

Open
#4,089 8 comments 0 reactions 0 assignees View on GitHub
:beetle: bug help wanted tenet-performance
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

**Is your feature request related to a problem? Please describe.**

The current implementation of `TextBox.TextPlaceHolderText` API added in #96/#238 suffers from a number of deficiencies and shortcomings as listed below.

1. It is unaware of the native implementation and visually inconsistent with it:
```cs
textBox1.PlaceholderText = "Custom!";
SendMessageW(textBox1.Handle, EM_SETCUEBANNER, IntPtr.Zero, "NATIVE PlaceHolder..");
```
![image](https://user-images.githubusercontent.com/4403806/95421317-f70da180-0988-11eb-9e61-9eaa5a8cea2e.png)

2. It is visually inconsistent with the native implementation:
* This implementation - `TextBox` control with `PlaceHolderText` property set:
![image](https://user-images.githubusercontent.com/4403806/95421535-4b188600-0989-11eb-8c6f-59ecab5399ce.png)
* Native implementation - `TextBox` control with the placeholder text set via the `EM_SETCUEBANNER`:
![image](https://user-images.githubusercontent.com/4403806/95421558-579cde80-0989-11eb-960e-68b7626c463b.png)

- different locations,
- different font colours, and
- different shortening strategies.

3. The current implementation is also not UIA aware:
* This implementation
![image](https://user-images.githubusercontent.com/4403806/95421178-a1d19000-0988-11eb-96a5-cadf62bed4d5.png)
* Native implementation:
![image](https://user-images.githubusercontent.com/4403806/95421112-76e73c00-0988-11eb-8b3d-ae55f89124ca.png)

**Describe the solution you'd like and alternatives you've considered**

* Keep the existing public API surface in tact
* Rework `PlaceHolderText` implementation as follows:
- Use Win32 API [`EM_SETCUEBANNER`](https://docs.microsoft.com/windows/win32/controls/em-setcuebanner) for single line textboxes when visual styles are enabled.
- Align location of the custom drawn text with the position of the Win32 implementation.
- Align the font of the custom drawn text with the size/style of the Win32 implementation.
- Align the color of the custom drawn text with the color of the Win32 implementation.
- Align the shortening strategy of the custom drawn text with the color of the Win32 implementation - i.e. clip the text instead of "..."
* Fallback to the current implementation in the following situations:
- `TextBox.Multiline = true`.
- Visual styles are disabled.

❗ Tests must verify correct behaviours when switching single<-->multiline.

❕ Accessibility support for the custom drawn text at this stage remains optional.

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.