using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;
using MoveGraphLibrary;
namespace UserDrivenApplications
{
public partial class Form_YearsSelection : Form
{
const string strAddRegKey = "Form_YearsSelection";
Mover mover;
Group groupAll, groupSelected;
string [] titles = new string [] { "All years", "Selected" };
Size [] sizeTitles;
Spaces spaces;
// -------------------------------------------------
public Form_YearsSelection ()
{
InitializeComponent ();
mover = new Mover (this);
for (int i = 1880; i <= DateTime .Now .Year; i++)
{
listYearsAll .Items .Add (i .ToString ());
}
sizeTitles = Auxi_Geometry .RoundMeasureStrings (this, titles);
spaces = new Spaces (this);
Rectangle rc = Auxi_Geometry .FrameAroundControls (new Control [] { listYearsAll, btnSelect }, spaces);
groupAll = new Group (this, rc, new RectRange (sizeTitles [0] .Width + 20, 180, 100, 500), titles [0], MoveAll);
rc = Auxi_Geometry .FrameAroundControls (new Control [] { listYearsSelected, btnExclude }, spaces);
groupSelected = new Group (this, rc, new RectRange (sizeTitles [1] .Width + 20, 180, 100, 500), titles [1], MoveSelected);
RestoreFromRegistry ();
RenewMover ();
}
// ------------------------------------------------- OnFormClosing
private void OnFormClosing (object sender, FormClosingEventArgs e)
{
SaveInfoToRegistry ();
}
// ------------------------------------------------- RenewMover
// order: btnOK - groupSelected - groupAll
//
public void RenewMover ()
{
mover .Clear ();
mover .Insert (0, groupAll);
mover .Insert (0, groupSelected);
mover .Insert (0, btnOK);
}
// ------------------------------------------------- MoveAll
void MoveAll ()
{
Rectangle rc = groupAll .RectAround;
int cxL = rc .Left + spaces .Left_inFrame;
int cxR = rc .Right - spaces .Right_inFrame;
btnSelect .Location = new Point (cxL, rc .Bottom - (spaces .Btm_inFrame + btnSelect .Height));
btnSelect .Width = cxR - cxL;
int cyT = rc .Top + spaces .Top_inFrame;
int cyB = btnSelect .Top - spaces .VerMin;
listYearsAll .Bounds = new Rectangle (cxL, cyT, btnSelect .Width, cyB - cyT);
listYearsAll .Columns [0] .Width = listYearsAll .Width - (SystemInformation .VerticalScrollBarWidth + 4);
}
// ------------------------------------------------- MoveSelected
void MoveSelected ()
{
Rectangle rc = groupSelected .RectAround;
int cxL = rc .Left + spaces .Left_inFrame;
int cxR = rc .Right - spaces .Right_inFrame;
btnExclude .Location = new Point (cxL, rc .Bottom - (spaces .Btm_inFrame + btnExclude .Height));
btnExclude .Width = cxR - cxL;
int cyT = rc .Top + spaces .Top_inFrame;
int cyB = btnExclude .Top - spaces .VerMin;
listYearsSelected .Bounds = new Rectangle (cxL, cyT, btnExclude .Width, cyB - cyT);
listYearsSelected .Columns [0] .Width = listYearsSelected .Width - (SystemInformation .VerticalScrollBarWidth + 4);
}
// ------------------------------------------------- OnMouseDown
private void OnMouseDown (object sender, MouseEventArgs e)
{
mover .Catch (e .Location, e .Button);
}
// ------------------------------------------------- OnMouseUp
private void OnMouseUp (object sender, MouseEventArgs e)
{
mover .Release ();
}
// ------------------------------------------------- OnMouseMove
private void OnMouseMove (object sender, MouseEventArgs e)
{
if (mover .Move (e .Location))
{
Invalidate ();
}
}
// ------------------------------------------------- OnPaint
private void OnPaint (object sender, PaintEventArgs e)
{
Graphics grfx = e .Graphics;
groupAll .Draw (grfx);
groupSelected .Draw (grfx);
}
// ------------------------------------------------- Click_btnSelect
private void Click_btnSelect (object sender, EventArgs e)
{
if (listYearsAll .SelectedIndices .Count > 0)
{
for (int i = 0; i < listYearsAll .SelectedIndices .Count; i++)
{
string str = listYearsAll .Items [listYearsAll .SelectedIndices [i]] .Text;
if (null == listYearsSelected .FindItemWithText (str))
{
listYearsSelected .Items .Add (str);
}
}
}
}
// ------------------------------------------------- Click_btnExclude
private void Click_btnExclude (object sender, EventArgs e)
{
for (int i = listYearsSelected .SelectedIndices .Count - 1; i >= 0; i--)
{
listYearsSelected .Items .RemoveAt (listYearsSelected .SelectedIndices [i]);
}
}
const string nameLocation = "Location";
// ------------------------------------------------- SaveInfoToRegistry
private void SaveInfoToRegistry ()
{
string strkey = Form_Main .strRegKey + strAddRegKey;
RegistryKey regkey = null;
try
{
regkey = Registry .CurrentUser .CreateSubKey (strkey);
if (regkey != null)
{
string [] strSizes = { ClientSize .Width .ToString (), // 0
ClientSize .Height .ToString (), // 1
groupAll .RectAround .X .ToString (), // 2
groupAll .RectAround .Y .ToString (), // 3
groupAll .Width .ToString (), // 4
groupAll .Height .ToString (), // 5
groupSelected .RectAround .X .ToString (), // 6
groupSelected .RectAround .Y .ToString (), // 7
groupSelected .Width .ToString (), // 8
groupSelected .Height .ToString (), // 9
btnOK .Left .ToString (), // 10
btnOK .Top .ToString (), // 11
};
regkey .SetValue (nameLocation, strSizes, RegistryValueKind .MultiString);
}
}
catch
{
}
finally
{
if (regkey != null) regkey .Close ();
}
}
// ------------------------------------------------- RestoreFromRegistry
private void RestoreFromRegistry ()
{
string strkey = Form_Main .strRegKey + strAddRegKey;
RegistryKey regkey = null;
try
{
regkey = Registry .CurrentUser .OpenSubKey (strkey);
if (regkey != null)
{
string [] strSizes = (string []) regkey .GetValue (nameLocation);
if (strSizes != null && strSizes .Length == 12)
{
ClientSize = Auxi_Convert .ToSize (strSizes, 0);
groupAll .RectAround = Auxi_Convert .ToRectangle (strSizes, 2);
groupSelected .RectAround = Auxi_Convert .ToRectangle (strSizes, 6);
btnOK .Location = Auxi_Convert .ToPoint (strSizes, 10);
}
}
}
catch
{
}
finally
{
if (regkey != null) regkey .Close ();
}
}
}
}