유용한 정보

[C#] 문자입력컨트롤에 워터마크 지원

DevReff 2025. 4. 16. 04:46
728x90
728x90
SMALL

예제1) TextBox
/// <summary>
/// Watermark를 지원하도록 한다.
/// </summary>
/// <param name="textBox">입력컨트롤</param>
/// <param name="hintText">Watermark문자열</param>
/// <param name="hintTextColor">문자열 색상</param>
/// <param name="initvalue">초기값</param>
/// <param name="italic">워터마크를 기울임체로 할 것인지의 여부</param>
/// <param name="is_tail">워터마크가 값의 뒤에 붙는지의 여부</param>
void SetHintText(this TextBox textBox
, string hintText
, Color hintTextColor
, string initvalue = ""
, bool italic = false
, bool is_tail = false)
{
var foreTextColor = textBox.ForeColor;
Font font;

if (hintTextColor == Color.Empty)
hintTextColor = SystemColors.GrayText;

if (italic)
font = new Font(textBox.Font, FontStyle.Italic);
else
font = new Font(textBox.Font, FontStyle.Regular);

textBox.Text = string.Format("{0} {1}", initvalue, hintText);
textBox.ForeColor = hintTextColor;
textBox.Font = font;
textBox.Leave += (sender, e) =>
{
var tb = sender as TextBox;

if (tb != null)
{
string txt = tb.Text.Trim();
if (txt.Length == 0)
{
tb.Text = hintText;
tb.ForeColor = hintTextColor;
}
else
{
if (is_tail)
tb.Text = string.Format("{0} {1}", txt, hintText);
tb.ForeColor = foreTextColor;
}
}
};
textBox.Enter += (sender, e) =>
{
var tb = sender as TextBox;

if (tb != null)
{
string txt = tb.Text.Trim();
if (txt == hintText)
{
tb.Text = string.Empty;
}
else
{
tb.Text = txt.Replace(string.Format(" {0}", hintText), "");
}
tb.ForeColor = foreTextColor;
}
};
}

반응형

예제2) RichTextBox
/// <summary>
/// Watermark를 지원하도록 한다.
/// </summary>
/// <param name="richtextBox">입력컨트롤</param>
/// <param name="hintText">Watermark문자열</param>
/// <param name="hintTextColor">Watermark문자열 색상</param>
/// <param name="initvalue">초기값</param>
/// <param name="italic">워터마크를 기울임체로 할 것인지의 여부</param>
/// <param name="is_tail">워터마크가 값의 뒤에 붙는지의 여부</param>
void RichSetHintText(this RichTextBox richtextBox
, string hintText
, Color hintTextColor
, string initvalue = ""
, bool italic = false
, bool is_tail = false)
        {
            var foreTextColor = richtextBox.ForeColor;
            Font font;

            if (hintTextColor == Color.Empty)
                hintTextColor = SystemColors.GrayText;

            if (italic)
                font = new Font(richtextBox.Font, FontStyle.Italic);
            else
                font = new Font(richtextBox.Font, FontStyle.Regular);

            richtextBox.Text = string.Format("{0} {1}", initvalue, hintText);
            richtextBox.ForeColor = hintTextColor;
            richtextBox.Font = font;
            richtextBox.Leave += (sender, e) =>
            {
                var tb = sender as TextBox;

                if (tb != null)
                {
                    string txt = tb.Text.Trim();
                    if (txt.Length == 0)
                    {
                        tb.Text = hintText;
                        tb.ForeColor = hintTextColor;
                    }
                    else
                    {
                        if (is_tail)
                            tb.Text = string.Format("{0} {1}", txt, hintText);
                        tb.ForeColor = foreTextColor;
                    }
                }
            };
            richtextBox.Enter += (sender, e) =>
            {
                var tb = sender as RichTextBox;

                if (tb != null)
                {
                    string txt = tb.Text.Trim();
                    if (txt == hintText)
                    {
                        tb.Text = string.Empty;
                    }
                    else
                    {
                        tb.Text = txt.Replace(string.Format(" {0}", hintText), "");
                    }
                    tb.ForeColor = foreTextColor;
                }
            };
        }

728x90
728x90
LIST