一、实现效果 1.1、Winform的Combox下拉框效果
1.2、Dev中Winform的ComboxEdit与LookUpEdit下拉框效果
二、实现核心 2.1、Winform的Combox下拉框
/***
* Title:"Winfrom" 项目
* 主题:Winform的UI帮助类
* Description:
* 功能:
* 1、给comboBox组件添加信息和获取选中的信息
* Date:2021
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Utils
{
class WinformUIHelper
{
#region ComboxEdit组件
///
/// 给Combox组件添加内容
///
/// comboBox组件
/// 信息列表
/// 是否显示第一个值(true:表示显示)
/// 返回添加结果(true:表示添加成功)
public static bool AddInfoToCombox(ComboBox comboBox, List infoList,
bool showFirstValue = true)
{
bool result = false;
if (infoList != null && infoList.Count > 0)
{
comboBox.Items.Clear();
comboBox.DataSource = infoList;
//默认显示第一个值
if (showFirstValue)
{
comboBox.SelectedIndex = 0;
}
else
{
comboBox.SelectedIndex = -1;
}
result = true;
}
return result;
}
///
/// 给Combox组件添加键值对内容
///
/// comboBox组件
/// 信息键值对
/// 是否显示第一个值(true:表示显示)
/// 返回添加结果(true:表示添加成功)
public static bool AddInfoToCombox(ComboBox comboBox, Dictionary infoDic,
bool showFirstValue = true)
{
bool result = false;
if (infoDic != null && infoDic.Count > 0)
{
comboBox.Items.Clear();
BindingSource bs = new BindingSource();
bs.DataSource = infoDic;
comboBox.DataSource = bs;
//实际要用的字段
comboBox.ValueMember = "Key";
//展示的字段
comboBox.DisplayMember = "Value";
//默认显示第一个值
if (showFirstValue)
{
comboBox.SelectedIndex = 0;
}
else
{
comboBox.SelectedIndex = -1;
}
result = true;
}
return result;
}
///
/// 给Combox组件添加键值对内容
///
/// 实体
/// Combox组件
/// 实体列表
/// id字段名称
/// 显示的字段名称
/// 是否显示第一个值(true:表示显示)
/// 返回添加结果(true:表示添加成功)
public static bool AddInfoToCombox(ComboBox comboBox, List listModel,
string idFieldName,string displayInfoFieldName, bool showFirstValue = true)
{
bool result = false;
if (listModel != null && listModel.Count>0)
{
comboBox.Items.Clear();
comboBox.DataSource = listModel;
comboBox.ValueMember = idFieldName;
comboBox.DisplayMember = displayInfoFieldName;
//默认显示第一个值
if (showFirstValue)
{
comboBox.SelectedIndex = 0;
}
else
{
comboBox.SelectedIndex = -1;
}
result = true;
}
return result;
}
///
/// 获取Combox组件当前选择的内容
///
/// comboBox组件
/// 返回当前选择的键值对
public static Dictionary GetComboxInfoOfSelected(ComboBox comboBox)
{
Dictionary tmpDic = new Dictionary();
if (comboBox!=null)
{
if (comboBox.SelectedItem!=null)
{
string keyValue = comboBox.SelectedItem.ToString();
string key = comboBox.SelectedValue.ToString();
string value = comboBox.Text;
tmpDic.Add(key, value);
}
}
return tmpDic;
}
#endregion
}//Class_end
}
2.2、Dev中Winform的ComboxEdit与LookUpEdit下拉框
/***
* Title:"Winfrom" 项目
* 主题:Winform的UI帮助类
* Description:
* 功能:
* 1、给comboBox组件添加信息和获取选中的信息
* 2、给lookUpEdit组件添加信息和获取选中的信息
* Date:2021
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Utils
{
class WinformUIHelper
{
#region ComboxEdit组件
///
/// 给ComboxEdit组件添加内容
///
/// comboBoxEdit组件
/// 信息列表
/// 是否显示第一个值(true:表示显示)
/// 返回添加结果(true:表示添加成功)
public static bool AddInfoToComboxEdit(ComboBoxEdit comboBoxEdit, List infoList,
bool showFirstValue = true)
{
bool result = false;
if (infoList != null && infoList.Count > 0)
{
comboBoxEdit.Properties.Items.Clear();
foreach (var item in infoList)
{
comboBoxEdit.Properties.Items.Add(item);
}
//默认显示第一个值
if (showFirstValue)
{
comboBoxEdit.SelectedIndex = 0;
}
else
{
comboBoxEdit.SelectedIndex = -1;
}
result = true;
}
return result;
}
///
///是否禁用ComboxEidt手动输入
///
public static void IsDisableInputOfComboxEdit(bool isDisable=true)
{
if (isDisable)
{
comboBoxEdit_PageSize.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
}
else
{
comboBoxEdit_PageSize.Properties.TextEditStyle = TextEditStyles.Standard;
}
}
///
/// 给LookupEdit组件添加内容
///
/// lookUpEdit组件
/// 信息字典
/// 匹配自动搜索的字段
/// 手动输入(true:表示开启)
/// 是否显示第一个值(true:表示显示)
///
public static bool AddInfoToLookupEdit(LookUpEdit lookUpEdit,Dictionary infoDic,
string matchFieldName="",bool manualInput=true,bool showFirstValue=true)
{
bool result = false;
if (infoDic!=null && infoDic.Count>0)
{
lookUpEdit.Properties.DataSource = infoDic;
//实际需要使用的字段
lookUpEdit.Properties.ValueMember = "Key";
//显示的字段
lookUpEdit.Properties.DisplayMember = "Value";
//自动搜索datasouse,选择与之匹配的值,没有的情况下赋值null ,value的值必须与valuemember的数据类型一致。
lookUpEdit.EditValue = matchFieldName;
//自适应宽度
lookUpEdit.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
//默认允许手动输入
if (manualInput)
{
lookUpEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
}
else
{
lookUpEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
}
//默认显示第一个值
if (showFirstValue)
{
lookUpEdit.ItemIndex = 0;
}
else
{
lookUpEdit.Properties.NullText = "";
lookUpEdit.ItemIndex = -1;
}
result = true;
}
return result;
}
///
/// 给LookupEdit组件添加内容
///
/// lookUpEdit组件
/// 信息字典
/// 匹配自动搜索的字段
/// 手动输入(true:表示开启)
/// 是否显示第一个值(true:表示显示)
///
public static bool AddInfoToLookupEdit(LookUpEdit lookUpEdit, List tmpList,
string matchFieldName = "", bool manualInput = true, bool showFirstValue = true)
{
bool result = false;
if (tmpList != null && tmpList.Count > 0)
{
lookUpEdit.Properties.DataSource = tmpList;
//实际需要使用的字段
lookUpEdit.Properties.ValueMember = "Key";
//显示的字段
lookUpEdit.Properties.DisplayMember = "Value";
//自动搜索datasouse,选择与之匹配的值,没有的情况下赋值null ,value的值必须与valuemember的数据类型一致。
lookUpEdit.EditValue = matchFieldName;
//自适应宽度
lookUpEdit.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
//默认允许手动输入
if (manualInput)
{
lookUpEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
}
else
{
lookUpEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
}
//默认显示第一个值
if (showFirstValue)
{
lookUpEdit.ItemIndex = 0;
}
else
{
lookUpEdit.Properties.NullText = "";
lookUpEdit.ItemIndex = -1;
}
result = true;
}
return result;
}
///
/// 给LookupEdit组件添加模型内容
///
/// 模型
/// lookUpEdit组件
/// 模型列表
/// 模型字段名称列表
/// 模型字段的起始索引
/// 下拉框显示内容的字段名称
/// 下拉框显示内容实际对应使用的字段名称
/// 下拉框搜索匹配的字段名称
/// 手动输入(true:表示开启)
/// 是否显示第一个值(true:表示显示)
///
public static bool AddModelInfoToLookupEdit(LookUpEdit lookUpEdit, List tmpList,
List modelFieldNameList,int modelFieldStartIndex,
string displayFieldName,string valueFieldName,string matchFieldName = "",
bool manualInput = true, bool showFirstValue = true)
{
bool result = false;
if (tmpList != null && tmpList.Count > 0 &&
modelFieldNameList!=null && modelFieldNameList.Count>0)
{
lookUpEdit.Properties.DataSource = tmpList;
//实际需要使用的字段
lookUpEdit.Properties.ValueMember = valueFieldName;
//显示的字段
lookUpEdit.Properties.DisplayMember = displayFieldName;
//自动搜索datasouse,选择与之匹配的值,没有的情况下赋值null ,value的值必须与valuemember的数据类型一致。
lookUpEdit.EditValue = matchFieldName;
//自适应宽度
lookUpEdit.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
//填充列
lookUpEdit.Properties.PopulateColumns();
//设置列属性
int count = (modelFieldStartIndex+modelFieldNameList.Count);
for (int i = modelFieldStartIndex; i < count; i++)
{
//lookUpEdit.Properties.Columns[0].Visible = false;
lookUpEdit.Properties.Columns[i].Caption = modelFieldNameList[i];
}
//lookUpEdit.Properties.Columns[1].Width = 120;
//lookUpEdit.Properties.Columns[2].Width = 300;
控制选择项的总宽度
//lookUpEdit.Properties.PopupWidth = 200;
//默认允许手动输入
if (manualInput)
{
lookUpEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
}
else
{
lookUpEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
}
//默认显示第一个值
if (showFirstValue)
{
lookUpEdit.ItemIndex = 0;
}
else
{
lookUpEdit.Properties.NullText = "";
lookUpEdit.ItemIndex = -1;
}
result = true;
}
return result;
}
///
/// 获取comboBoxEdit组件当前选择的内容
///
/// comboBoxEdit组件
/// 返回当前选择的键值对
public static Dictionary GetComboxInfoOfSelected(ComboBoxEdit comboBoxEdit)
{
Dictionary tmpDic = new Dictionary();
if (comboBoxEdit != null)
{
if (comboBoxEdit.SelectedItem != null)
{
string key = comboBoxEdit.EditValue.ToString();
string value = comboBoxEdit.Text.Trim();
tmpDic.Add(key, value);
}
}
return tmpDic;
}
///
/// 获取lookUpEdit组件当前选择的内容
///
/// lookUpEdit组件
/// 返回当前选择的键值对
public static Dictionary GetLookUpEditInfoOfSelected(LookUpEdit lookUpEdit)
{
Dictionary tmpDic = new Dictionary();
if (lookUpEdit != null)
{
if (lookUpEdit.EditValue != null)
{
string key = lookUpEdit.EditValue.ToString();
string value = lookUpEdit.Text.Trim();
tmpDic.Add(key, value);
}
}
return tmpDic;
}
#endregion
}//Class_end
}
三、使用方法 3.1、Winform的Combox下拉框 ①引用命名空间【using Utils;】
②添加内容到下拉组件中
#region UI
///
/// 添加列表信息到下拉框中
///
private void AddListInfoToCombox()
{
List tmpList = new List();
for (int i = 0; i < 3; i++)
{
tmpList.Add($"内容{i}");
}
WinformUIHelper.AddInfoToCombox(comboBox_UI,tmpList);
}
///
/// 添加键值对信息到下拉框中
///
private void AddDicInfoToCombox()
{
Dictionary tmpDic = new Dictionary();
for (int i = 0; i < 3; i++)
{
string key = $"0076yurdkh0{i}";
string value = $"车间{i}";
tmpDic.Add(key,value);
}
WinformUIHelper.AddInfoToCombox(comboBox_UI2, tmpDic);
}
///
/// 添加实体信息到下拉框中
///
private void AddModelInfoToCombox()
{
List peoples = new List()
{
new people("20346584368001","杨思思",25,"女","3536728921@qq.com","测试地址1","高级人事经理"),
new people("20346584368002","张茜",23,"女","3536728922@qq.com","测试地址2","产品经理"),
new people("20346584368003","张伊伊",23,"女","3536728923@qq.com","测试地址3","销售经理"),
};
people people = new people();
WinformUIHelper.AddInfoToCombox(comboBox_UI3, peoples,"Email","Name",false);
}
private void ShowInfo(string info)
{
textBox_Info.Text = info;
}
#endregion
public class people
{
public string Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string Work { get; set; }
public people()
{
}
public people(string id,string name,int age,string sex,string email,string address,string work)
{
this.Id = id;
this.Name = name;
this.Age = age;
this.Sex = sex;
this.Email = email;
this.Address = address;
this.Work = work;
}
}
③ 编写选择值改变的方法内容
private void comboBox_UI_SelectedValueChanged(object sender, EventArgs e)
{
Dictionary tmpDic = WinformUIHelper.GetComboxInfoOfSelected(comboBox_UI);
if (tmpDic != null && tmpDic.Count > 0)
{
string info = $"当前选择的内容为: 键:{tmpDic.ElementAt(0).Key} 值:{tmpDic.ElementAt(0).Value}";
ShowInfo(info);
}
}
private void comboBox_UI2_SelectedValueChanged(object sender, EventArgs e)
{
Dictionary tmpDic = WinformUIHelper.GetComboxInfoOfSelected(comboBox_UI2);
if (tmpDic!=null && tmpDic.Count>0)
{
string info = $"当前选择的内容为: 键:{tmpDic.ElementAt(0).Key} 值:{tmpDic.ElementAt(0).Value}";
ShowInfo(info);
}
}
private void comboBox_UI3_SelectedValueChanged(object sender, EventArgs e)
{
Dictionary tmpDic = WinformUIHelper.GetComboxInfoOfSelected(comboBox_UI3);
if (tmpDic != null && tmpDic.Count > 0)
{
string info = $"当前选择的内容为: 键:{tmpDic.ElementAt(0).Key} 值:{tmpDic.ElementAt(0).Value}";
ShowInfo(info);
}
}
3.2、Dev中Winform的ComboxEdit与LookUpEdit下拉框 ①引用命名空间【using Utils;】
②添加内容到下拉组件中
#region UI
///
/// 添加列表信息到下拉框中
///
private void AddListInfoToComboxEdit()
{
List tmpList = new List();
for (int i = 0; i < 3; i++)
{
tmpList.Add($"内容{i}");
}
WinformUIHelper.AddInfoToComboxEdit(comboBoxEdit_UI1, tmpList);
}
///
/// 添加键值对信息到下拉框中
///
private void AddInfoToLookupEdit()
{
Dictionary tmpDic = new Dictionary();
for (int i = 0; i < 3; i++)
{
string key = $"0076yurdkh0{i}";
string value = $"车间{i}";
tmpDic.Add(key, value);
}
WinformUIHelper.AddInfoToLookupEdit(lookUpEdit_UI1,tmpDic);
}
///
/// 添加实体信息到下拉框中
///
private void AddModelInfoToLookupEdit()
{
List peoples = new List()
{
new people("20346584368001","杨思思",25,"女","3536728921@qq.com","测试地址1","高级人事经理"),
new people("20346584368002","张茜",23,"女","3536728922@qq.com","测试地址2","产品经理"),
new people("20346584368003","张伊伊",23,"女","3536728923@qq.com","测试地址3","销售经理"),
};
people people = new people();
List diplayFieldNameList = new List() {
"编号","姓名","年龄","邮箱","地址","职位"
};
WinformUIHelper.AddModelInfoToLookupEdit(lookUpEdit_UI2, peoples, diplayFieldNameList,
0, "Name", "Id","Name",true, true);
}
private void ShowInfo(string info)
{
memoEdit_Info.Text = info;
}
#endregion
public class people
{
public string Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string Work { get; set; }
public people()
{
}
public people(string id, string name, int age, string sex, string email, string address, string work)
{
this.Id = id;
this.Name = name;
this.Age = age;
this.Sex = sex;
this.Email = email;
this.Address = address;
this.Work = work;
}
}
③ 编写选择值改变的方法内容
private void comboBoxEdit_UI1_SelectedValueChanged(object sender, EventArgs e)
{
Dictionary tmpDic = WinformUIHelper.GetComboxInfoOfSelected(comboBoxEdit_UI1);
if (tmpDic != null && tmpDic.Count > 0)
{
string info = $"当前选择的内容: 键:{tmpDic.ElementAt(0).Key} 值:{tmpDic.ElementAt(0).Value}";
ShowInfo(info);
}
}
private void lookUpEdit_UI1_EditValueChanged(object sender, EventArgs e)
{
Dictionary tmpDic = WinformUIHelper.GetLookUpEditInfoOfSelected(lookUpEdit_UI1);
if (tmpDic != null && tmpDic.Count > 0)
{
string info = $"当前选择的内容: 键:{tmpDic.ElementAt(0).Key} 值:{tmpDic.ElementAt(0).Value}";
ShowInfo(info);
}
}
private void lookUpEdit_UI2_EditValueChanged(object sender, EventArgs e)
{
Dictionary tmpDic = WinformUIHelper.GetLookUpEditInfoOfSelected(lookUpEdit_UI2);
if (tmpDic != null && tmpDic.Count > 0)
{
string info = $"当前选择的内容: 键:{tmpDic.ElementAt(0).Key} 值:{tmpDic.ElementAt(0).Value}";
ShowInfo(info);
}
}