You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
11 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using Mesnac.Controls.Base;
using System.Drawing;
using System.Text.RegularExpressions;
namespace Mesnac.Controls.Default
{
[ToolboxBitmap(typeof(ComboBox))]
public partial class MCCombobox : ComboBox, IBaseControl
{
private bool _mcVisible = true; //保存可见性
private bool _mcEnabled = true; //保存可用性
private bool _isValid = true;//保存有效性
private bool _isEmpty = true; //允许
private Color BorderColor = Color.FromArgb(107, 191, 111);
public MCCombobox()
{
InitializeComponent();
//if (this._comboboxList.Count > 0 && ((DataTable)this.BindDataSource).TableName == "")
//{
// this.datas
//}
}
public MCCombobox(IContainer container)
{
container.Add(this);
InitializeComponent();
}
/// <summary>
/// 判断字符是否存在下拉列表
/// </summary>
private bool isTextCorrect;
private bool allowTypeAllSymbols = true;
/// <summary>
/// 是否可以输入选项以外的字符
/// </summary>
[Browsable(true)]
[Description("是否可输入额外字符"), Category("Behavior")]
public bool AllowTypeAllSymbols
{
set
{
allowTypeAllSymbols = value;
}
get
{
return allowTypeAllSymbols;
}
}
List<ComboboxItems> _comboboxList = new List<ComboboxItems>();
[Description("组合框选项"), Category("Data")]
public List<ComboboxItems> ComboboxList
{
get { return _comboboxList; }
set { _comboboxList = value; }
}
[Description("是否允许为空"), Category("Behavior")]
public bool IsEmpty
{
get { return _isEmpty; }
set { _isEmpty = value; }
}
public bool IsValid
{
get { return _isValid; }
set { _isValid = value; }
}
#region 事件
private List<DesignAction> _selectedChanged = new List<DesignAction>();
public List<DesignAction> SelectedChanged
{
get { return _selectedChanged; }
set { _selectedChanged = value; }
}
#endregion
#region 自动完成
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!allowTypeAllSymbols)
{
isTextCorrect = false;
if (!char.IsControl(e.KeyChar))
{
string actual = this.Text.Substring(0, SelectionStart) + e.KeyChar;
// 从第一个字符串开始匹配
int index = this.FindString(actual);
// 如果找到匹配字符串设置为true
if (index > -1)
{
isTextCorrect = true;
base.OnKeyPress(e);
}
else
e.Handled = true;
}
}
}
protected override void OnKeyUp(KeyEventArgs e)
{
//int index;
//string actual;
//string found;
//// 这些键不坐任何事情
//if ((e.KeyCode == Keys.Back) ||
// (e.KeyCode == Keys.Left) ||
// (e.KeyCode == Keys.Right) ||
// (e.KeyCode == Keys.Up) ||
// (e.KeyCode == Keys.Down) ||
// (e.KeyCode == Keys.Delete) ||
// (e.KeyCode == Keys.PageUp) ||
// (e.KeyCode == Keys.PageDown) ||
// (e.KeyCode == Keys.Home) ||
// (e.KeyCode == Keys.End) ||
// (e.KeyCode == Keys.ShiftKey) ||
// (e.KeyCode == Keys.Tab) ||
// (e.KeyCode == Keys.Menu))
//{
// return;
//}
//// 已输入的文本
//actual = this.Text.Substring(0, this.SelectionStart);
//// 从第一列开始找
//index = this.FindString(actual);
////找到
//if (index > -1)
//{
// if ((!allowTypeAllSymbols && isTextCorrect) || (allowTypeAllSymbols))
// {
// if (this.DataSource != null && this.DisplayMember != "")
// found = ((DataRowView)(this.Items[index]))[this.DisplayMember].ToString();
// else
// found = this.Items[index].ToString();
// this.SelectedIndex = index;
// this.Text = found;
// //选中剩余的字符
// this.SelectionStart = actual.Length;
// this.SelectionLength = found.Length;
// base.OnKeyUp(e);
// }
//}
}
#endregion
#region 接口实现
public string MCKey
{
get;
set;
}
public object MCValue
{
get
{
return this.SelectedValue == null ? "" : this.SelectedValue.ToString();
}
set
{
if (value != null)
{
this.SelectedValue = value;
}
else
{
if (this.Items.Count > 0)
{
this.SelectedIndex = 0;
}
}
}
}
private string _displayFiled;
[Description("文本字段"), Category("Data")]
public string DisplayFiled
{
get { return _displayFiled; }
set { _displayFiled = value; }
}
private string _valueFiled;
[Description("值字段"), Category("Data")]
public string ValueFiled
{
get { return _valueFiled; }
set { _valueFiled = value; }
}
public IBaseControl MCRoot
{
get
{
return null;
}
set
{
;
}
}
[TypeConverter(typeof(DataSourceConverter))]
[Description("数据连接"), Category("Data")]
public string MCDataSourceID
{
get;
set;
}
public MCDataSource MCDataSource
{
get;
set;
}
[Description("是否为数据库控件"), Category("Data")]
public bool IsDbControl
{
get;
set;
}
[Description("初始化SQL"), Category("Data")]
public string InitDataSource
{
get;
set;
}
[Description("执行SQL"), Category("Data")]
public string ActionDataSource
{
get;
set;
}
[Description("绑定数据源"), Category("Data")]
public object BindDataSource
{
get { return this.DataSource; }
set
{
if (value is List<ComboboxItems> || (value is DataTable && this.DisplayFiled == null && this.ValueFiled == null))
{
this.DisplayMember = "CmbDisplay";
this.ValueMember = "CmbValue";
this.DataSource = this.ComboboxList;
}
else
{
this.DataSource = null;
this.DisplayMember = this.DisplayFiled;
this.ValueMember = this.ValueFiled;
this.DataSource = value == null ? new DataTable() : (DataTable)value;
this.AutoCompleteSource = AutoCompleteSource.ListItems;
this.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
}
}
[Description("操作类型"), Category("Data")]
public DbOptionTypes DbOptionType
{
get;
set;
}
[Description("是否可见"), Category("Behavior")]
public bool MCVisible
{
get
{
return this._mcVisible;
}
set
{
this._mcVisible = value == null ? true : value;
if (this.Site.DesignMode)
{
this.Visible = true;
}
else
{
this.Visible = _mcVisible;
}
}
}
[Description("是否可用"), Category("Behavior")]
public bool MCEnabled
{
get
{
return this._mcEnabled;
}
set
{
this._mcEnabled = value == null ? true : value;
if (this.Site.DesignMode)
{
this.Enabled = true;
}
else
{
this.Enabled = this._mcEnabled;
}
}
}
#endregion
protected override void OnLostFocus(EventArgs e)
{
if (!_isEmpty)
{
if (this.Text == string.Empty && this.DropDownStyle.ToString() != "DropDownList")
{
this.BorderColor = Color.Red;
errorProvider1.SetIconAlignment((Control)this, ErrorIconAlignment.MiddleRight);
errorProvider1.SetError((Control)this, "不允许为空!");
_isValid = false;
return;
}
else
{
errorProvider1.SetIconAlignment((Control)this, ErrorIconAlignment.MiddleRight);
errorProvider1.SetError((Control)this, "");
_isValid = true;
}
}
base.Invalidate();
base.OnLostFocus(e);
}
}
[Serializable]
public class ComboboxItems
{
private string cmbValue;
public string CmbValue
{
get { return cmbValue; }
set { cmbValue = value; }
}
private string cmbDisplay;
public string CmbDisplay
{
get { return cmbDisplay; }
set { cmbDisplay = value; }
}
}
}