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

📄 superlisttestform.cs

📁 I built the Superlist control whilst developing an RSS reader called FeedGhost. Although there are p
💻 CS
字号:
/////////////////////////////////////////////////////////////////////////////
//
// (c) 2007 BinaryComponents Ltd.  All Rights Reserved.
//
// http://www.binarycomponents.com/
//
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
//
// (c) 2006 BinaryComponents Ltd.  All Rights Reserved.
//
// http://www.binarycomponents.com/
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using BinaryComponents.SuperList;
using BinaryComponents.SuperList.Sections;
using BinaryComponents.Utility.Collections;

namespace SuperListTest
{
	public partial class SuperListTestForm : Form
	{
		public SuperListTestForm()
		{
			InitializeComponent();
			Column surnameColumn = new Column( "surname", "Surname", 120, delegate( object item ) { return ((Person)item).Surname; } );
			Column firstnameColumn = new Column( "firstname", "Firstname", 120, delegate( object item ) { return ((Person)item).Firstname; } );
			Column phoneColumn = new Column( "phone", "Phone", 100, delegate( object item ) { return ((Person)item).Phone; } );
			Column cityColumn = new Column( "city", "City", 60, delegate( object item ) { return ((Person)item).City; } );
			Column stateColumn = new Column( "state", "State", 70, delegate( object item ) { return ((Person)item).State; } );
			Column dateColumn = new Column( "date", "Date", 110, delegate( object item ) { return ((Person)item).Date.ToString(); } );
			dateColumn.GroupItemAccessor = new ColumnItemValueAccessor( GroupValueFromItem );
			dateColumn.MoveBehaviour = Column.MoveToGroupBehaviour.Copy;

			dateColumn.GroupSortOrder = SortOrder.Descending;
			surnameColumn.SortOrder = SortOrder.Ascending;

			_superList.Columns.Add( firstnameColumn );
			_superList.Columns.Add( phoneColumn );
			_superList.Columns.Add( stateColumn );
			_superList.Columns.Add( cityColumn );
			_superList.Columns.Add( dateColumn );
			_superList.Columns.GroupedItems.Add( dateColumn );
			_superList.Columns.GroupedItems.Add( stateColumn );
		
			_superList.SelectedItems.DataChanged += new SelectedItemsCollection.DataChangedHandler( SelectedItems_DataChanged );

			int tickStart = Environment.TickCount;
			const int iterationCount = 1; // Change this if you want to increas the number of items in the list
			for( int i = 0; i < iterationCount; i++ ) 
			{
				_superList.Items.AddRange( Person.GetData() );
			}
		}

		private string GroupValueFromItem( object o )
		{
			DateTime date = ((Person)o).Date;

			DateTime publicationDate = date;
			DateTime now = DateTime.Now.Date;
			DateTime weekStart = now.AddDays( -(int)now.DayOfWeek );
			DateTime monthStart = now.AddDays( -now.Day );

			double days = now.Subtract( publicationDate ).TotalDays;

			if( days < 1 )
			{
				return "Today";
			}
			else if( days < 2 )
			{
				return "Yesterday";
			}
			else if( publicationDate > weekStart )
			{
				return "This Week";
			}
			else if( publicationDate > weekStart.AddDays( -7 ) )
			{
				return "Last Week";
			}
			else if( publicationDate > monthStart )
			{
				return "This Month";
			}
			else if( publicationDate > monthStart.AddMonths( -1 ).AddDays( 1 ) )
			{
				return "Last Month";
			}
			else
			{
				return "Older";
			}
		}
		void SelectedItems_DataChanged( object sender, SelectedItemsChangedEventArgs e )
		{
			System.Diagnostics.Debug.WriteLine( string.Format( "Selection event {0}", e.ChangeType ) );
			foreach( RowIdentifier ri in e.Items )
			{
				string s;

				if( ri.ColumnGroup == null )
				{
					s = string.Format( "\tSelection row item: {0}", ri.Items[0] );
				}
				else
				{
					s = "\tSelection group item:";
					int index = _superList.Columns.GroupedItems.IndexOf( ri.ColumnGroup );
					for( int i = 0; i < _superList.Columns.GroupedItems.Count; i++ )
					{
						if( i > 0 )
						{
							s+= "->";
						}
						s += _superList.Columns.GroupedItems[i].GroupItemAccessor( ri.Items[0] );
					}
				}
				System.Diagnostics.Trace.WriteLine( s );
			}
		}

		private void _fileExitMenuItem_Click( object sender, EventArgs e )
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void startTimedAdditionsToolStripMenuItem_Click( object sender, EventArgs e )
		{
			if( _timer == null )
			{
				_timer = new Timer();
				_timer.Interval = 1000;
				_timer.Tick += new EventHandler( _timer_Tick );
				_timer.Start();
			}
			else
			{
				_timer.Stop();
				_timer.Dispose();
				_timer = null;
			}
		}

		void _timer_Tick( object sender, EventArgs e )
		{
			_superList.Items.AddRange( Person.GetData() );
		}

		private Timer _timer;

		private void clearListToolStripMenuItem_Click( object sender, EventArgs e )
		{
			_superList.Items.Clear();
		}

		private void selectAllToolStripMenuItem_Click( object sender, EventArgs e )
		{
			_superList.SelectedItems.SelectAll();
		}



		private void _superList_KeyDown( object sender, KeyEventArgs e )
		{
			if( e.KeyCode == Keys.Delete )
			{
				List<object> selectedItems = new List<object>();

				Set<object> groupRows = new Set<object>();
				Column groupColumn = null;
				foreach( BinaryComponents.SuperList.RowIdentifier ri in _superList.SelectedItems )
				{
					if( ri.ColumnGroup == null && groupRows.Contains( (object)ri.Items[0] ) )
					{
						groupRows.Clear();
					}

					foreach( object newsFeedItemNode in ri.Items )
					{
						if( groupColumn != ri.ColumnGroup )
						{
							selectedItems.AddRange( groupRows.ToArray() );
							groupColumn = ri.ColumnGroup;
						}

						if( ri.ColumnGroup == null )
						{
							selectedItems.Add( newsFeedItemNode );
						}
						else
						{
							groupRows.Add( newsFeedItemNode );
						}
					}
				}
				selectedItems.AddRange( groupRows.ToArray() );

				foreach( object o in selectedItems )
				{
					_superList.Items.Remove( o );
				}
			}
		}

		private const string _fileFilter = "SuperList config file(*.xml)|*.xml";

		private void _ensureItemVisibleToolStripMenuItem_Click( object sender, EventArgs e )
		{
			SelectItemForm sif = new SelectItemForm( _superList );
			if( sif.ShowDialog( this ) == DialogResult.OK && sif.SelectedItems.Length > 0 )
			{
				MessageBox.Show( this, sif.SelectedItems[0].ToString() );
				_superList.FocusedItem = sif.SelectedItems[0];
			}
		}

		private void _dumpListItemsToolStripMenuItem_Click( object sender, EventArgs e )
		{
			foreach( Person p in _superList.Items.ToArray() )
			{
				System.Diagnostics.Debug.WriteLine( p );
			}
		}

		private void _dirtyItemsToolStripMenuItem_Click( object sender, EventArgs e )
		{
			SelectItemForm sif = new SelectItemForm( _superList );
			if( sif.ShowDialog( this ) == DialogResult.OK )
			{
				_superList.Items.ItemsChanged( sif.SelectedItems );
			}
		}

		private void selectItemsToolStripMenuItem_Click( object sender, EventArgs e )
		{
			SelectItemForm sif = new SelectItemForm( _superList );
			if( sif.ShowDialog( this ) == DialogResult.OK )
			{
				_superList.SelectedItems.AddRange( sif.SelectedItems );
			}
		}

		private void toggleCustomAreaVisibilityToolStripMenuItem_Click( object sender, EventArgs e )
		{
			_superList.ShowCustomizeSection = !_superList.ShowCustomizeSection;
		}

		private void toggleHeaderVisibilityToolStripMenuItem_Click( object sender, EventArgs e )
		{
			_superList.ShowHeaderSection = !_superList.ShowHeaderSection;
		}

		private void clearSelectionToolStripMenuItem_Click( object sender, EventArgs e )
		{
			_superList.SelectedItems.Clear();
		}

		private void loadConfigToolStripMenuItem_Click_1( object sender, EventArgs e )
		{
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.CheckFileExists = true;
			ofd.AddExtension = true;
			ofd.Filter = _fileFilter;
			ofd.Title = "Load config file";
			if( ofd.ShowDialog( this ) == DialogResult.OK )
			{
				try
				{
					using( System.IO.TextReader textReader = File.OpenText( ofd.FileName ) )
					{
						_superList.DeSerializeState( textReader );
					}
				}
				catch( Exception exception )
				{
					MessageBox.Show( exception.Message );
				}
			}
		}

		private void saveConfigToolStripMenuItem_Click_1( object sender, EventArgs e )
		{
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.CheckFileExists = false;
			ofd.AddExtension = true;
			ofd.Filter = _fileFilter;
			ofd.Title = "Save config file";
			if( ofd.ShowDialog( this ) == DialogResult.OK )
			{
				try
				{
					using( System.IO.TextWriter textWriter = File.CreateText( ofd.FileName ) )
					{
						_superList.SerializeState( textWriter );
					}
				}
				catch( Exception exception )
				{
					MessageBox.Show( exception.Message );
				}
			}
		}

		#region Example of overriding rows 

		/// <summary>
		/// Storage area for the row override.
		/// </summary>
		private RowOverrideExample _rowOverride;

		private void toggleRowPaintingOverrideToolStripMenuItem_Click( object sender, EventArgs e )
		{
			if( _rowOverride == null )
			{
				//
				// Start overrride.
				_rowOverride = new RowOverrideExample( _superList );
			}
			else
			{
				//
				// Clear override.
				_rowOverride.Dispose();
				_rowOverride = null;
			}
		}

		/// <summary>
		/// Example of overriding rows giving a gradient fill look.
		/// </summary>
		private class RowOverrideExample: IDisposable
		{
			public RowOverrideExample( BinaryComponents.SuperList.ListControl listControl )
			{
				_oldFactory = listControl.SectionFactory; // store old factory as we want to leave as we came.
				_listControl = listControl;

				//
				// Replace the current SectionFactory with our override.
				listControl.SectionFactory = new MySectionFactory(); // 
				_listControl.LayoutSections();
			}


			public void Dispose()
			{
				if( _oldFactory != null ) // put things back as they were
				{
					_listControl.SectionFactory = _oldFactory;
					_listControl.LayoutSections();
				}
			}

			private class MySectionFactory : SectionFactory
			{
				public override RowSection CreateRowSection( 
					BinaryComponents.SuperList.ListControl listControl, 
					RowIdentifier rowIdenifier, 
					HeaderSection headerSection, 
					int position )
				{
					return new MyRowSection( listControl, rowIdenifier, headerSection, position );
				}
			}

			private class MyRowSection : RowSection
			{
				public MyRowSection( 
					BinaryComponents.SuperList.ListControl listControl, 
					RowIdentifier rowIdentifier, 
					HeaderSection headerSection, 
					int position )
					: base( listControl, rowIdentifier, headerSection, position )
				{
					_position = position;
				}

				public override void PaintBackground( Section.GraphicsSettings gs, Rectangle clipRect )
				{
					Color from, to;
					if( _position % 2 == 0 )
					{
						from = Color.White;
						to = Color.LightBlue;
					}
					else
					{
						to = Color.White;
						from = Color.LightBlue;
					}
					using( LinearGradientBrush lgb = new LinearGradientBrush( 
						this.Rectangle, 
						from, 
						to,
						LinearGradientMode.Horizontal ) )
					{
						gs.Graphics.FillRectangle( lgb, this.Rectangle );
					}
				}
				public override void Paint( Section.GraphicsSettings gs, Rectangle clipRect )
				{
					base.Paint( gs, clipRect );
				}
				private int _position;
			}

			private BinaryComponents.SuperList.ListControl _listControl;
			private SectionFactory _oldFactory;
		}
		#endregion
	}
}

⌨️ 快捷键说明

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