I am developing a custom TextBox to display and edit currency values. I would like to have a currency symbol visible within the TextBox on the left. Overriding OnPaint of TextBox is the horror after Googling and doing some tests. Does anyone have any other ideas? Maybe add a character as a background image in a TextBox (if it's pretty simple)?
TextBox
OnPaint
why donβt you put a shortcut in front of the text box and display the currency value?
why not just:
private void textBox1_TextChanged(object sender, EventArgs e) { if (!textBox1.Text.StartsWith("Β£")) { textBox1.Text = string.Concat("Β£", textBox1.Text); textBox1.Select(textBox1.Text.Length, 0); } }
You can do a few things:
Another option is to use a watermark in your text box - see here for an example of how to do this.
You can use MaskedTextBox instead of TextBox. http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx
For your Mask property, use the "$" symbol for the currency symbol.