Add static text to TextBox
I'm developing a custom TextBox
for displaying and editing currency values. What I would like is to have a currency symbol v开发者_Go百科isible within the TextBox
on the left side. Overriding OnPaint
of a TextBox
is sort of horror after Googling and doing some tests. Anybody have other ideas? Maybe add the symbol as a background picture to the TextBox
(if that is rather simple)?
why don't you put a label before 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 could do several things:
- Add a "$" in your get property of the textbox
- Add a label with a static $
- Create a user control with a $ as a label and reuse it with your custom textbox
Another option would be to use a watermark in your textbox--please see here for an example of how to do this.
You can use a MaskedTextBox instead of a TextBox. http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx
For your Mask property, use "$" for the currency symbol.
精彩评论