先看效果:自定义下拉控件,可以放任意自己的空间。
设计思路:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UCLibs
{
public partial class UCLookUpEdit : UserControl
{
public UCLookUpEdit()
{
InitializeComponent();
}
public bool LabelClickAble { get; set; } = true;
public UCLookUpPanel ContentPanel { get; set; }
public string FilterDes { get; set; }
public Dictionary<string, string> FilterDic { get; set; } = new Dictionary<string, string>();
//查询完成,点击【确定】按钮事件
public Action SearchEnter { get; set; }
private void UCLookUpEdit_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 绑定查询的PANEL
/// </summary>
/// <param name="pn"></param>
public void BindContentPanel(UCLookUpPanel pn)
{
this.ContentPanel = pn;
this.ContentPanel.Enter = () =>
{
this.FilterDes = this.ContentPanel.iContent.GetFilterDes();
this.lbDis.Text = this.FilterDes;
this.FilterDic=this.ContentPanel.iContent.GetFilterDic();
this.ContentPanel.Hide();
if(this.SearchEnter != null)
{
this.SearchEnter();
}
};
this.ContentPanel.Clear = () =>
{
this.FilterDes = "";
this.lbDis.Text = "--请选择条件--";
this.FilterDic = new Dictionary<string, string>();
};
this.ContentPanel.FindForm().Click += (s, e) =>
{
//点击了,其他位置,隐藏查询
this.ContentPanel.Hide();
};
this.ContentPanel.Parent.Click += (s, e) =>
{
//查询PANEL失去焦点,隐藏查询
this.ContentPanel.Hide();
};
}
/// <summary>
/// 显示查询界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lbLookUp_Click(object sender, EventArgs e)
{
var ctl = this.ContentPanel as Control;
if(this.Top+this.Height + ctl.Height >this.FindForm().Height)
{
ctl.Top = this.Top - 25 - ctl.Height;
}
else
{
ctl.Top = this.Top + 25;
}
ctl.Visible = !ctl.Visible;
}
private void lbDis_Click(object sender, EventArgs e)
{
if(this.LabelClickAble)
{
lbLookUp_Click(null, null);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UCLibs
{
public partial class UCLookUpPanel : UserControl
{
public UCLookUpPanel()
{
InitializeComponent();
}
public ILookUpCotent iContent;
public Action Enter { get; set; }
public Action Clear { get; set; }
private void UCLookUpPanel_Load(object sender, EventArgs e)
{
}
public void AddContent(ILookUpCotent content)
{
this.iContent = content;
var ctl = content as Control;
ctl.Dock = DockStyle.Fill;
this.pnMain.Controls.Add(ctl);
}
private void btnEnter_Click(object sender, EventArgs e)
{
this.Enter();
}
private void btnClear_Click(object sender, EventArgs e)
{
this.iContent.Clear();
this.Clear();
}
private void lbClose_Click(object sender, EventArgs e)
{
this.Hide();
}
private void lbClose_MouseEnter(object sender, EventArgs e)
{
}
private void lbClose_MouseLeave(object sender, EventArgs e)
{
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Hide();
}
private void lbClear_Click(object sender, EventArgs e)
{
this.iContent.Clear();
this.Clear();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UCLibs
{
public interface ILookUpCotent
{
/// <summary>
/// 查询编辑控件
/// </summary>
UCLookUpEdit ContainerLookUp { get; set; }
/// <summary>
/// 描述
/// </summary>
string Des { get; }
/// <summary>
/// 获取查询条件的文字描述
/// </summary>
string GetFilterDes();
/// <summary>
/// 查询条件集合
/// </summary>
/// <returns></returns>
Dictionary<string, string> GetFilterDic();
/// <summary>
/// 清空条件
/// </summary>
void Clear();
/// <summary>
/// 初始化数据
/// </summary>
void InitData(object data);
}
}
调用方法:界面上拉EDIT,PANEL,和接口控件
//初始化查询控件容器
this.ucLookUpEdit1.BindContentPanel( this.ucLookUpPanel1);
LKProductSearch sr = new LKProductSearch();
//自己写的 ILookUpCotent 控件加进来
this.ucLookUpPanel1.AddContent(sr);
var tb = this.SearchDB();
//初始化查询UC里面的数据,实际按项目里的自己来查
sr.InitData(tb);
//查询点击确定按钮
this.ucLookUpEdit1.SearchEnter = () =>
{
this.Search1();
};
private void Search1()
{
if (this.ucLookUpEdit1.FilterDic.Count == 0)
{
MessageBox.Show("ucLookUpEdit1 未选择任何查询条件!请重新选择!");
return;
}
//自己用FilterDic写查询条件
//列如
string codes = this.ucLookUpEdit1.FilterDic["CODES"];
string sql = "select * from xx where p_code in (" + codes + ")";
//查询参数字典 this.ucLookUpEdit1.FilterDic
//查询描述 this.ucLookUpEdit1.FilterDes
MessageBox.Show("ucLookUpEdit1 查询条件是:" + this.ucLookUpEdit1.FilterDes + "\r\n" +
"查询SQL:" + sql);
}