Rawns Rawns -4 years ago 87
C# Question

C# - Allowing ampersands (&) to be entered in Textboxes and stored

I can find multiple solutions all involving labels, but I can't seem to find any solutions or suggestions for allowing ampersands to be used via text boxes?

I have a WinForm that users have to configure on first load, and it's not uncommon for them to enter '&' into one of the fields. Once saved, this value is then used as a

groupBox
title, but the '&' is stripped out. It's untidy asking them to enter the ampersand twice (&&) for it to display correctly.

enter image description here
enter image description here

Is it possible for a single '&' to be added into a text filed, then used without it being removed?

Edit: To clarify, the textbox value is used as the Text value for a groupbox so
UseMnemonic
is not an option here.

Answer Source

You could escape it by:

_schoolGroupBox.Text = _federationNameTextBox.Text.Replace("&", "&&");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download