2019년 4월 5일 금요일

C# - MaskedTextBox 디자인 및 효과 적용



using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace CustomControls
{
/// <summary>
/// MaskedTextBox 디자인 및 효과 적용
/// </summary>
/// <작성자>김하덕</작성자>
/// <작성일>2008-09-02 ~ 2008-09-04</작성일>
public partial class MaskTBoxStyle : Control
{
private MaskedTextBox maskTBox = new MaskedTextBox();

private int _borderWidth = 0;
private int _borderWidthMax = 1;
private int _borderWidthMin = 0;
private string _mask = "99999";
private Type _validatingType = typeof(int);
private Color _borderColor = Color.Black;
private int _width_MBox = 100;
private HorizontalAlignment _textAlign = HorizontalAlignment.Center;
private Font _font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
private bool _isFocus = false;

public int BorderWidthMax
{
get { return _borderWidthMax; }
set
{
if (value < 1)
_borderWidthMax = 1;
else
{
_borderWidthMax = value;
this.maskTBox.Location = new System.Drawing.Point(_borderWidthMax, _borderWidthMax);
_borderWidth = _borderWidthMax;
MaskTBoxResize();
}
}
}

public int BorderWidthMin
{
get { return _borderWidthMin; }
set
{
if (value < 0)
_borderWidthMin = 0;
else
{
_borderWidthMin = value;
_borderWidth = _borderWidthMin;
MaskTBoxResize();
}
}
}

public Color BorderColor
{
get { return _borderColor; }
set { _borderColor = value; }
}

public new Font Font
{
get { return _font; }
set
{
_font = value;
this.maskTBox.Font = value;
MaskTBoxResize();
}
}

public string Mask
{
get { return _mask; }
set
{
_mask = value;
this.maskTBox.Mask = value;
}
}

public Type ValidatingType
{
get { return _validatingType; }
set
{
_validatingType = value;
this.maskTBox.ValidatingType = value;
}
}

public int Width_MBox
{
get { return _width_MBox; }
set
{
_width_MBox = value;
this.maskTBox.Width = value;
MaskTBoxResize();
}
}

public HorizontalAlignment TextAlign
{
get { return _textAlign; }
set
{
_textAlign = value;
this.maskTBox.TextAlign = value;
}
}

private void MaskTBoxResize()
{
this.Width = this.maskTBox.Width + (_borderWidthMax * 2);
this.Height = this.maskTBox.Height + (_borderWidthMax * 2);
}

public MaskTBoxStyle()
{
SetMaskTBoxStyle(_font, _mask, _validatingType, _borderWidth, _borderColor, _width_MBox, _textAlign, _borderWidthMax, _borderWidthMin);
}

public MaskTBoxStyle(Font font, string mask, Type validatingType, int borderWidth, Color borderColor, int width_MBox, HorizontalAlignment textAlign, int borderWidthMax, int borderWidthMin)
{
SetMaskTBoxStyle(font, mask, validatingType, borderWidth, borderColor, width_MBox, textAlign, borderWidthMax, borderWidthMin);
}

private void SetMaskTBoxStyle(Font font, string mask, Type validatingType, int borderWidth, Color borderColor, int width_MBox, HorizontalAlignment textAlign, int borderWidthMax, int borderWidthMin)
{
InitializeComponent();

//
// maskTBox
//
this.maskTBox.Top = 0;
this.maskTBox.Left = 0;
this.maskTBox.BorderStyle = BorderStyle.None;
this.maskTBox.PromptChar = ' ';

//
// MaskTBoxStyle
//
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = System.Drawing.Color.Transparent;
this.Font = font;
this.Mask = mask;
this.ValidatingType = validatingType;
this.BorderWidthMax = borderWidthMax;
this.BorderWidthMin = borderWidthMin;
this.BorderColor = borderColor;
this.Width_MBox = width_MBox;
this.TextAlign = textAlign;

this.Controls.Add(maskTBox);

MaskedTextBoxEventHandler();
}

public string TextValue
{
get { return this.maskTBox.Text.Trim(); }
set { this.maskTBox.Text = value; }
}

private void MaskedTextBoxEventHandler()
{
this.maskTBox.TextChanged += new System.EventHandler(this.OnTextChanged);
this.maskTBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.OnTextKeyPress);
this.maskTBox.MouseHover += new System.EventHandler(this.OnMouseHover);
this.maskTBox.MouseLeave += new System.EventHandler(this.OnMouseLeave);
this.maskTBox.Enter += new System.EventHandler(this.OnEnter);
this.maskTBox.Leave += new System.EventHandler(this.OnLeave);
}

private void OnTextKeyPress(object sender, KeyPressEventArgs e)
{
if(!(Char.IsNumber(e.KeyChar)))
{
if((e.KeyChar == (char)Keys.Space) || (e.KeyChar == (char)Keys.Back))
e.Handled = false;
else
e.Handled = true;
}

/*
//if(e.KeyChar == 32) // 스페이스
if(e.KeyChar == (char)Keys.Space)
{
e.Handled = true;
}
else
{
base.OnKeyPress(e);
}
*/
}

private void OnMouseHover(object sender, EventArgs e)
{
this.BorderColor = _borderColor;
this.BorderWidthMax = _borderWidthMax;
Refresh();

base.OnMouseHover(e);
}

private void OnMouseLeave(object sender, EventArgs e)
{
if(!this._isFocus)
{
this.BorderColor = _borderColor;
this.BorderWidthMin = _borderWidthMin;
Refresh();
}

base.OnMouseLeave(e);
}

private void OnEnter(object sender, EventArgs e)
{
this._isFocus = true;

this.BorderColor = _borderColor;
this.BorderWidthMax = _borderWidthMax;
Refresh();

base.OnEnter(e);
}

private void OnLeave(object sender, EventArgs e)
{
this._isFocus = false;

this.BorderColor = _borderColor;
this.BorderWidthMin = _borderWidthMin;
Refresh();

base.OnLeave(e);
}

private void OnTextChanged(object sender, EventArgs e)
{
base.OnTextChanged(e);
}

protected override void OnPaint(PaintEventArgs e)
{
int j = 0;
for(int i = 1; i <= _borderWidth; i++)
{
e.Graphics.DrawRectangle(new Pen(_borderColor), (this.maskTBox.Top - i), (this.maskTBox.Left - i), (this.maskTBox.Width + (i + j)), (this.maskTBox.Height + (i + j)));
j++;
}

base.OnPaint(e);
}


}
}


댓글 없음:

댓글 쓰기

javascript - SQL 예약어 제거

  <script language="javascript"> //특수문자, 특정문자열(sql예약어) 제거 function checkSearchedWord(obj){ obj.value = obj.value+&quo...