⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commonquery.cs

📁 C#自定义查询控件
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Drawing.Design;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Reflection;
using System.Runtime.InteropServices;


namespace CommonComponent.CommonQuery
{	
	#region CommonQuery class
	/// <summary>
	/// Summary description for commonQuery.
	/// </summary>
	[ToolboxBitmap(typeof(CommonQuery))]	
	[DefaultProperty("Items")]
	[DesignTimeVisible(true)]
	[ClassInterface(ClassInterfaceType.None)]
	public class CommonQuery : System.ComponentModel.Component, IQuery
	{
		#region Variables
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private System.Data.SqlClient.SqlConnection connection = null;
		private string connectionString = null;
		private ItemCollection items = new ItemCollection();
		private System.Windows.Forms.BorderStyle borderStyle = System.Windows.Forms.BorderStyle.None;
		private System.Windows.Forms.FlatStyle buttonStyle = System.Windows.Forms.FlatStyle.Standard;
		private System.Data.DataTable autoTable = null;
		private bool autoClear = true;
		private System.Data.DataColumn autoField = null;

		#endregion

		public CommonQuery()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitComponent call
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// 
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}


		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}
		#endregion

		#region Properties

		[Category("Extended")]
		[Browsable(true)]
		[Description("数据库连接对象")]
		public System.Data.SqlClient.SqlConnection SqlConnection
		{
			get{ return this.connection; }
			set{ this.connection = value; }
		}

		[Category("Extended")]
		[Browsable(true)]
		[Description("数据库可连接字符串")]
		public string SqlConnectionString
		{
			get{ return this.connectionString; }
			set{ this.connectionString = value; }
		}

		[Category("Extended")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[Editor(typeof(System.ComponentModel.Design.CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
		[Description("条件字段集合")]
		public ItemCollection Items
		{
			get{ return this.items; }
			set{ this.items = value; }
		}

		[Category("Extended")]
		[Browsable(true)]
		[Description("设置组件外观")]
		public System.Windows.Forms.BorderStyle BorderStyle
		{
			get{ return this.borderStyle; }
			set{ this.borderStyle = value; }
		}

		[Category("Extended")]
		[Browsable(true)]
		[Description("设置组件按钮外观")]
		public System.Windows.Forms.FlatStyle ButtonStyle
		{
			get{ return this.buttonStyle; }
			set{ this.buttonStyle = value; }
		}

		[Category("Design")]
		[Browsable(true)]
		[Description("选择自动加载项目(Items)的表")]
		public System.Data.DataTable AutoTable
		{
			get{ return this.autoTable; }
			set			
			{ 
				this.autoTable = value;
				this.AddItems();
			}
		}

		[Category("Design")]
		[Browsable(true)]
		public System.Data.DataColumn AutoField
		{
			get{ return this.autoField; }
			set{ this.autoField = value; }
		}

		[Category("Design")]
		[Browsable(true)]
		[Description("选择自动加载项目(Items)时,是否清除已有项")]
		public bool AutoClear
		{
			get{ return this.autoClear; }
			set{ this.autoClear = value; }
		}

		#endregion

		#region Methods

		private void AddItems()
		{
			if( null != this.autoTable )
			{
				if( autoClear )
					items.Clear();

				foreach( System.Data.DataColumn col in this.autoTable.Columns )
				{
					Item item = new Item(col.ColumnName, col.Caption);
					if( !items.Contains( item) )
						this.items.Add(  item );
				}
			}
			else
				items.Clear();
		}

		// Implement Interface
		string IQuery.Condition()
		{
			string constr ="";
			using( QueryForm qf = new QueryForm(this) )
			{
				try
				{
					qf.ShowDialog();
				}
				catch(Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
				if( null != qf )
					constr = qf.Condition();
			}
			return constr;
		}

		#endregion
	}
	#endregion

	#region Item class

	[DefaultProperty("Caption")]
	[DesignTimeVisible(false)]
	[ToolboxItem(false)]
	[TypeConverter("CommonComponent.CommonQuery.ItemConverter")]
	public class Item
	{
		#region Variables

		// Design
		private string name;
		
		// Basic
		private string alias;
		private string caption;

		// Lookup
		private bool isLookup;
		private string field;
		private string tableName;
		private string database;
		
		// index
		private int itemIndex = -1;

		#endregion

		#region Constructors

		public Item()
		{
			this.alias = "";
			this.name = this.alias;
			this.caption = "";
			this.tableName = "";
			this.database = "";
			this.field = "";
		}

		public Item(string alias, string caption)
		{
			this.alias = alias;
			this.name = alias;
			this.caption = caption;
		}

		public Item(string alias, string caption, string database, string tableName, string field)
		{
			this.alias = alias;
			this.name = alias;
			this.caption = caption;
			this.database = database;
			this.tableName = tableName;
			this.field = field;
		}

		#endregion

		#region Properties

//		[Category("Design")]
		[Browsable(false)]
		public string Name
		{
			get{ return this.name; }
			set{ this.name = value; }
		}

		[Category("Basic")]
		[Browsable(true)]
		[Description("条件字段名(可带别名)")]
		public string Alias
		{
			get{ return this.alias; }
			set{ this.alias = value; }
		}

		[Category("Basic")]
		[Browsable(true)]
		[Description("条件字段描述")]
		public string Caption
		{
			get{ return this.caption; }
			set{ this.caption = value; }
		}

		[Category("Basic")]
		[Browsable(true)]
		[Description("是否取基本表数据")]
		public bool IsLookup
		{
			get{ return this.isLookup; }
			set
			{ 
				this.isLookup = value; 
				if( value == false ) 
				{
					this.Database = "";
					this.Field = "";
					this.TableName = "";
				}
			}
		}

		[Category("Lookup")]
		[Browsable(true)]
		[Description("基本表字段名")]
		public string Field
		{
			get{ return this.field; }
			set
			{
				this.field = value;
			}
		}

		[Category("Lookup")]
		[Browsable(true)]
		[Description("基本表表名")]
		public string TableName
		{
			get{ return this.tableName; }
			set
			{
				this.tableName = value;
			}
		}

		[Category("Lookup")]
		[Browsable(true)]
		[Description("基本表所在数据库名")]
		public string Database
		{
			get{ return this.database; }
			set
			{
				this.database = value;
			}
		}

		[DefaultValue(-1)]
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public int Index
		{
			get{ return itemIndex; }
			set{ itemIndex = value; }
		}

		#endregion

		#region Methods
		#endregion
	}

	#endregion

	#region ItemCollection class
	[DesignTimeVisible(true)]
	public class ItemCollection : System.Collections.CollectionBase, IEnumerable
	{
		#region Variables
		#endregion

		#region Constructors

		public ItemCollection()
		{
		}

		#endregion

		#region Methods
		public int Add(Item item)
		{
			//item.Name = "Item"+InnerList.Count.ToString();

			if( Contains(item) )
				return -1;

			return InnerList.Add( item );
		}

		public bool Contains(Item item)
		{
			return InnerList.Contains( item );
		}

		public int IndexOf(Item item)
		{
			return InnerList.IndexOf( item );
		}

		public void Remove(Item item)
		{
			InnerList.Remove( item );
		}

		public void AddRange(Item[] items)
		{
			//foreach( Item item in items )
			//	item.Name = "Item" + InnerList.Count.ToString();

			InnerList.AddRange( items );
		}

		public void Insert(int index, Item item)
		{
			//item.Name = "Item" + InnerList.Count.ToString();
			if( Contains(item) )
				return;

			InnerList.Insert( index, item );
		}

		#endregion

		#region Properties

		[Browsable(false)]
		[DefaultValue(-1)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public Item this[int index]
		{
			get
			{ 
				if( index <0 || index>Count )
					return null;
				return (Item) InnerList[index];

			}
		}
		#endregion
	}
	#endregion

	#region ItemConverter class
	public class ItemConverter : TypeConverter
	{
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			if( typeof(InstanceDescriptor) == destinationType )
			{
				return true;
			}
			return base.CanConvertTo (context, destinationType);
		}

		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if( typeof(InstanceDescriptor) ==destinationType && value is Item )
			{
				Item item = (Item) value;

				ConstructorInfo ci = typeof(Item).GetConstructor(new Type[] {});
				if( null != ci )
				{
					return new InstanceDescriptor(ci, null, false);
				}
			}
			return base.ConvertTo (context, culture, value, destinationType);
		}

	}
	#endregion
}

namespace CommonComponent.CommonQuery
{
	// Distribute interface
	#region Interface
	public interface IQuery
	{
		string Condition();
	}
	#endregion
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -