您的位置:首页 > 其它

Winform DBText 数据感知控件开源示例

2017-04-01 15:29 375 查看
//设计参考源于某网上开源项目
//此为重写的控件,将替换现有devexpress的解决方案
//此类控件需与窗体中的BinSource捆绑,只需设置字段名与字段标题即可.
//此类控件已于去年10月布置,此版本为重新设计的Text控件,后续将陆续剥离Devexpress控件
//Create By ChimHsiung 2017-4
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyControl
{
//事件代表的声明RefreshDateEventArgs
public delegate void ValueChangeEventHandler(object sender, System.EventArgs e);
public delegate void EditKeyEventHandler(object sender, KeyEventArgs e);

public partial class DBText : UserControl
{
private event ValueChangeEventHandler eventHandler;//事件
private event EditKeyEventHandler keyEventHandler;
private string _field = "";
private object _binSource;
private TextType _textType=TextType.Default;
private bool _allowSpace = false;

public bool Request = true;//是否为空
public bool Unique = false;//是否唯一

public DBText()
{
InitializeComponent();
}

//事件属性
[Description("当数据改变时发生")]
public event ValueChangeEventHandler DB_ValueChanged
{
add
{
eventHandler += value;
textBox1.Leave += new EventHandler(eventHandler);
}
remove
{
eventHandler -= value;
}
}

//事件属性
[Description("当按下某键时发生"), Category("UserControl")]
public event EditKeyEventHandler DB_EditKeyDown
{
add
{
keyEventHandler += value;
textBox1.KeyDown += new KeyEventHandler(keyEventHandler);
}
remove
{
keyEventHandler -= value;
}
}

[Description("标题"), Category("UserControl")]
public string DB_Label
{
get { return label.Text; }
set
{
label.Text = value;
textBox1.Left = label.Left + label.Width + 2;
textBox1.Width = this.Width - (label.Left + label.Width + 2);
}
}

[Description("数据源,DataTable类型"), Category("UserControl")]
public object DB_DataSource
{
get { return _binSource; }
set { _binSource = value; }
}

[Description("字段名"), Category("UserControl")]
public string DB_Field
{
get { return _field; }
set { _field = value; }
}

[Description("文本格式"), Category("UserControl")]
public TextType DB_TextType
{
get { return _textType; }
set { _textType = value; }
}

[Description("多行显示"), Category("UserControl")]
public bool DB_MulitLine
{
get
{
return textBox1.Multiline;
}
set
{
textBox1.Multiline = value;
}
}

[Description("文本水平对齐方式"), Category("UserControl")]
public HorizontalAlignment DB_HAlignment
{
get { return textBox1.TextAlign; }
set { textBox1.TextAlign = value; }
}

[Description("只读"), Category("UserControl")]
public bool DB_ReadOnly
{
get { return textBox1.ReadOnly; }
set { textBox1.ReadOnly = value; }
}

[Description("密文显示"), Category("UserControl")]
public char DB_Pass
{
get { return textBox1.PasswordChar; }
set { textBox1.PasswordChar = value; }
}

[Description("是否允许空格"), Category("UserControl")]
public bool DB_AllowSpace
{
get { return _allowSpace; }
set { _allowSpace = value; }
}

[Description("文本对齐方式"), Category("UserControl")]
public HorizontalAlignment DB_TextAlign
{
get { return textBox1.TextAlign; }
set { textBox1.TextAlign = value; }
}

[Description("滚动条样式"), Category("UserControl")]
public ScrollBars DB_Scroll
{
get { return textBox1.ScrollBars; }
set { textBox1.ScrollBars = value; }
}

//此段改为自动设置
//[Description("是否可空"), Category("UserControl")]
//public bool Request
//{
//    get
//    {
//        return _Request;
//    }
//    set
//    {
//        _Request = value;
//        if (_Request == false)
//            label.ForeColor = Color.Green;
//        else
//            label.ForeColor = Color.Black;
//    }
//}

/// <summary>
/// 数据与字段绑定
/// </summary>
public void BindDataField()
{
if (_binSource == null) return;
if (_field == "") return;

textBox1.DataBindings.Clear();
textBox1.DataBindings.Add("Text", _binSource, _field, false, DataSourceUpdateMode.OnPropertyChanged);
//DataSourceUpdateMode.OnPropertyChanged 解决实时刷新的问题

DataTable dt = (DataTable)_binSource;
//判断字段类型,小数/整数/文本
foreach (DataColumn dc in dt.Columns)
{
if (dc.Caption == _field)
{
//判断是否为空
Request = dc.AllowDBNull;
Unique = dc.Unique;
if (Request) label.ForeColor = Color.Black; else label.ForeColor = Color.Red;

//设置最大长度//防止-1时报错
textBox1.MaxLength = (dc.MaxLength <= 0 ? 0 : dc.MaxLength);

if (_textType == TextType.Default && dc.DataType != typeof(string))//排除手动设置与文本
{
if (dc.DataType == typeof(int) || dc.DataType == typeof(Int16) || dc.DataType == typeof(Int32) || dc.DataType == typeof(Int64))
{ _textType = TextType.Int; }
//double类型会映射为decimal,待验证
else if (dc.DataType == typeof(Single) || dc.DataType == typeof(double) || dc.DataType == typeof(Decimal))
{ _textType = TextType.Decimal; }
//其他类型暂不处理,待验证
//else MessageBox.Show(dc.DataType.ToString());
}
//if (_textType == TextType.Decimal || _textType == TextType.Minus)  //因text取不出数据而暂时失效 //小数显示时去掉末尾0
//{
//    MessageBox.Show(textBox1.Text);
//    textBox1.Text=(textBox1.Text.TrimEnd('0'));
//}
break;
}
}
}

public object GetValue()
{
return textBox1.Text;
}

public void SetValue(object value)
{
textBox1.Text = Convert.ToString(value);
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
DBClass.DB_textKeyPress(sender, e, _textType, _allowSpace);
}

}
}



文中所需类

/// <summary>
/// 文本格式枚举
/// </summary>
public enum TextType
{
//默认,即可输入任意字符
Default = 0,
//正整数
Int = 1,
//正小数
Decimal = 2,
//负数
Minus = 3,
}


/// <summary>
/// 定义文本框输入格式限制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void DB_textKeyPress(object sender, KeyPressEventArgs e,TextType textType,bool allowSpace=false)
{
string tbx = (sender as TextBox).Text;
if (!Char.IsControl(e.KeyChar))
{
if (!allowSpace && Char.IsWhiteSpace(e.KeyChar)) { e.Handled = true; }
else
{
switch (textType)
{
case TextType.Default:
default: { break; }
case TextType.Int:
{
if (!Char.IsNumber(e.KeyChar)) { e.Handled = true; }
break;
}
case TextType.Decimal:
{
if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar)) { e.Handled = true; }
else if (e.KeyChar == '.')
{
if (tbx.Length == 0) { e.Handled = true; }
if (tbx.LastIndexOf('.') != -1) { e.Handled = true; }
}
else if (tbx == "0") { e.Handled = true; }//第一位是0时,第二位必须为小数点
break;
}
case TextType.Minus:
{
if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar)) { e.Handled = true; }
else if (e.KeyChar == '.')
{
if (tbx.Length == 0) { e.Handled = true; }
if (tbx.LastIndexOf('.') != -1) { e.Handled = true; }
if (tbx == "-") { e.Handled = true; }//第一位是负号,第二位不能为小数点
}
else if (e.KeyChar == (Char)'-')
{
if (tbx.LastIndexOf('-') != -1) { e.Handled = true; }
else //负号强制移动到第一位
{
(sender as TextBox).Text = "-" + tbx;
(sender as TextBox).Select((sender as TextBox).TextLength, 0);
e.Handled = true;
}
}
else if (tbx == "0") { e.Handled = true; }//第一位是0时,第二位必须为小数点
break;
}
}
}
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐