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 LYFZ.ComponentLibrary
{
public partial class ComboBoxTextEx : UserControl
{
public ComboBoxTextEx()
{
InitializeComponent();
}
#region 属性
DataTable _Bindtable;
///
/// 要绑定数据
///
public DataTable Bindtable
{
get { return _Bindtable; }
set
{
_Bindtable = value;
int an = txtValue.Height;
TextComboBoxExLoad();
}
}
string _ShowName;
///
/// 要显示的字段
///
public string ShowName
{
get { return _ShowName; }
set
{
_ShowName = value;
TextComboBoxExLoad();
}
}
string _Text;
///
/// 选择后返回的值
///
public override string Text
{
get { return _Text; }
set { _Text = value; }
}
int _LstBoxHeiht;
public int LstBoxHeiht
{
get { return _LstBoxHeiht; }
set { _LstBoxHeiht = value; }
}
#endregion
#region 属性重写
[Category("控件扩展属性"), Description("设置控件中文本字体")]
public new Font Font
{
get
{
return txtValue.Font;
}
set
{
txtValue.Font = value;
lstboxData.Font = txtValue.Font;
OnResize(null);
Invalidate(false);
}
}
///
/// 禁止调整大小
///
///
protected override void OnResize(EventArgs e)
{
if (lstboxData.Visible == true)
{
this.Height = txtValue.Height + lstboxData.Height;
if (LstBoxHeiht > lstboxData.Font.Height)
{
lstboxData.Height = LstBoxHeiht;
}
}
else
{
this.Height = txtValue.Height;
btnOpenDrop.Width = this.Height;
btnOpenDrop.Height = this.Height;
btnOpenDrop.Location = new Point(this.Width - this.Height, 0);
txtValue.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
txtValue.Width = this.Width - btnOpenDrop.Width;
this.Width = txtValue.Width + btnOpenDrop.Width;
lstboxData.Height = lstboxData.Font.Height; ;
lstboxData.Width = this.Width - 1;
lstboxData.Location = new Point(0, txtValue.Height - 1);
lstboxData.BorderStyle = BorderStyle.None;
lstboxData.BackColor = Color.FromArgb(238, 238, 238);
lstboxData.Visible = false;
}
}
#endregion
///
/// 窗体加载事件
///
///
///
private void ComboBoxTextEx_Load(object sender, EventArgs e)
{
}
///
/// 给列表画值
///
private void TextComboBoxExLoad()
{
txtValue.Text = "";
Text = "";
lstboxData.Items.Clear();
lstboxData.Items.Add("");
LstBoxHeiht = lstboxData.Font.Height;
if (Bindtable != null && ShowName != null && ShowName != "")
{
if (Bindtable.Rows.Count > 0)
{
for (int i = 0; i < Bindtable.Rows.Count; i++)
{
LstBoxHeiht += lstboxData.Font.Height;
lstboxData.Items.Add(Bindtable.Rows[i][ShowName].ToString());
}
}
}
}
///
/// 按钮的点击
///
///
///
private void btnOpenDrop_Click(object sender, EventArgs e)
{
if (lstboxData.Visible)
{
lstboxData.Visible = false;
this.Height = btnOpenDrop.Height;
}
else
{
TextComboBoxExLoad();
this.BringToFront();
lstboxData.Visible = true;
OnResize(null);
}
}
///
/// 列表选择事件
///
///
///
private void lstboxData_SelectedIndexChanged(object sender, EventArgs e)
{
Text = lstboxData.Items[lstboxData.SelectedIndex].ToString();
txtValue.Text = Text;
lstboxData.Visible = false;
this.Size = new Size(txtValue.Width + btnOpenDrop.Width, txtValue.Height);
}
private void lstboxData_Leave(object sender, EventArgs e)
{
txtValue_Leave(null, null);
}
private void txtValue_Leave(object sender, EventArgs e)
{
if (lstboxData.Visible == true)
{
lstboxData.Visible = false;
this.Height = btnOpenDrop.Height;
}
}
}
}