控件创建过程:
解决方案->右键添加新项目->类库为Common->添加C_XML.cs类
C_XML.cs
namespace Common
{
/// <summary>
/// XML操作类
/// </summary>
public class C_XML
{
private XmlDocument _doc;
private string _filepath;
public C_XML(string filepath)
{
_filepath = filepath;
_doc = new XmlDocument();
_doc.Load(_filepath);
}
/// <summary>
/// 返回doc
/// </summary>
public XmlDocument XmlDoc
{
get
{
if (_doc != null)
return _doc;
else
return null;
}
}
/// <summary>
/// 根节点名称
/// </summary>
public string Root
{
get
{
if (_doc.DocumentElement != null)
{
return _doc.DocumentElement.LocalName;
}
else
{
return null;
}
}
}
/// <summary>
/// 获取节点的第一个子集名称
/// </summary>
public string RootFirst
{
get
{
if (_doc.DocumentElement.FirstChild != null)
{
return _doc.DocumentElement.FirstChild.LocalName;
}
else
{
return null;
}
}
}
/// <summary>
/// XML字节属性名称
/// </summary>
public XmlAttributeCollection FirstChilesAttributes
{
get
{
if (_doc.DocumentElement.FirstChild.Attributes != null)
{
return _doc.DocumentElement.FirstChild.Attributes;
}
else
{
return null;
}
}
}
/// <summary>
/// 保存
/// </summary>
public void Save()
{
_doc.Save(_filepath);
}
}
}
解决方案->右键添加新项目->windows控件库命名为DropDownTextBox
添加对Common的引用
添加information文件->创建info.xml文件
info.xml
DropDownTextBox.cs
namespace DropDownTextBox
{
public partial class DropDownTextBox : UserControl
{
private Int32 _dropdowncount = 6;
private Int32 _height;
private Boolean _tem = false;
private Common.C_XML _doc;
private ListBox _listbox = new ListBox();
public DropDownTextBox()
{
InitializeComponent();
}
/// <summary>
/// textbox记录信息数量
/// </summary>
public Int32 DropDownCount
{
get {
return _dropdowncount;
}
set { _dropdowncount = value; }
}
/// <summary>
/// textbox文本信息
/// </summary>
public String Tb_Text
{
get { return tb_text.Text.Trim(); }
}
private void DropDownTextBox_Load(object sender, EventArgs e)
{
_doc = new C_XML(Path.GetFullPath("info.xml"));
XmlNode _node = _doc.XmlDoc.SelectSingleNode(_doc.Root);
if (_node.ChildNodes.Count > DropDownCount)
{
for (int i = 0; i < _node.ChildNodes.Count - DropDownCount;i++ )
{
_node.RemoveChild(_node.LastChild);
}
_doc.Save();
}
_height = (DropDownCount + 1) * 12;
//
// listBox1
//
this._listbox.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
this._listbox.FormattingEnabled = true;
this._listbox.ItemHeight = 12;
this._listbox.Location = new System.Drawing.Point(this.Location.X, this.Location.Y + this.tb_text.Height - 1);
this._listbox.Name = "_listbox";
this._listbox.Size = new System.Drawing.Size(112, _height);
this._listbox.TabIndex = 1;
this._listbox.Visible = false;
this._listbox.SelectedValueChanged += new System.EventHandler(this._listbox_SelectedValueChanged);
Form parform = this.FindForm();
parform.Controls.Add(this._listbox);
this._listbox.BringToFront();
}
//查询XML文件
private void tb_text_TextChanged(object sender, EventArgs e)
{
if (_tem && !_listbox.Visible)
return;
_doc = new C_XML(Path.GetFullPath("info.xml"));
XmlNodeList nodelist = _doc.XmlDoc.SelectNodes(_doc.Root + "/" + _doc.RootFirst + "[contains(@" + _doc.FirstChilesAttributes[0].LocalName + ",'" + this.tb_text.Text.Trim() + "')]");
if (nodelist.Count > 0)
{
_listbox.Items.Clear();
foreach (XmlNode node in nodelist)
{
_listbox.Items.Add(node.Attributes[0].InnerText);
}
if (nodelist.Count < _dropdowncount * 12 / this._listbox.ItemHeight)
this._listbox.Height = nodelist.Count * this._listbox.ItemHeight + 13;
else
this._listbox.Height = _height;
this._listbox.Visible = true;
}
else
{
this._listbox.Visible = false;
}
}
private void _listbox_SelectedValueChanged(object sender, EventArgs e)
{
if (!(_listbox.SelectedIndex < 0))
{
_listbox.Visible = false;
_tem = true;
this.tb_text.Text = _listbox.SelectedItem.ToString();
_tem = false;
}
}
}
}
DropDownTextBox.Designer.cs
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tb_text = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// tb_text
//
this.tb_text.Dock = System.Windows.Forms.DockStyle.Left;
this.tb_text.Location = new System.Drawing.Point(0, 0);
this.tb_text.Name = "tb_text";
this.tb_text.Size = new System.Drawing.Size(112, 21);
this.tb_text.TabIndex = 0;
this.tb_text.TextChanged += new System.EventHandler(this.tb_text_TextChanged);
//
// DropDownTextBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.tb_text);
this.Name = "DropDownTextBox";
this.Size = new System.Drawing.Size(112, 21);
this.Load += new System.EventHandler(this.DropDownTextBox_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox tb_text;
}
}
解决方案->右键添加新项目->windows应用程序命名为MyTest
Form1.cs
namespace MyTest
{
public partial class Form1 : Form
{
private C_XML _doc;
public Form1()
{
InitializeComponent();
_doc = new C_XML(Path.GetFullPath("info.xml"));
dropDownTextBox1.DropDownCount = 6;
}
private void button1_Click(object sender, EventArgs e)
{
XmlNode _node = _doc.XmlDoc.SelectSingleNode(_doc.Root);
if (_node.ChildNodes.Count >= dropDownTextBox1.DropDownCount)
_node.RemoveChild(_node.LastChild);
XmlElement information = _doc.XmlDoc.CreateElement(_doc.RootFirst);
XmlAttribute newAttr = _doc.XmlDoc.CreateAttribute(_doc.FirstChilesAttributes[0].LocalName);
newAttr.Value = this.dropDownTextBox1.Tb_Text;
information.Attributes.Append(newAttr);
_node.PrependChild(information);
_doc.Save();
}
}
}
Form1.Designer.cs
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.dropDownTextBox1 = new DropDownTextBox.DropDownTextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(268, 207);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dropDownTextBox1
//
this.dropDownTextBox1.DropDownCount = 6;
this.dropDownTextBox1.Location = new System.Drawing.Point(252, 88);
this.dropDownTextBox1.Name = "dropDownTextBox1";
this.dropDownTextBox1.Size = new System.Drawing.Size(112, 21);
this.dropDownTextBox1.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(607, 392);
this.Controls.Add(this.dropDownTextBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private DropDownTextBox.DropDownTextBox dropDownTextBox1;
private System.Windows.Forms.Button button1;
}
}