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
'유용한 정보' 카테고리의 다른 글
[C#] TextBox 컨트롤에 수직스크롤바가 있는지의 여부 판단하기 (0) | 2025.04.16 |
---|---|
[C#] 임의의 컨트롤을 사용하여 폼을 이동하자 (0) | 2025.04.16 |
[C#] 파일 오픈 및 읽기/쓰기 (0) | 2025.04.15 |
[C#] RDLC 리포트 (0) | 2025.04.08 |
달력 알고리즘 (0) | 2025.03.21 |