Initial commit almost identical to SVN-repo rev.340
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace TBF.UiControls
|
||||
{
|
||||
public class BaseWithListViewEx<MyItem> : ListViewEx
|
||||
{
|
||||
/// <summary>
|
||||
/// List of items to be edited
|
||||
/// </summary>
|
||||
public IList<MyItem> MyItems;
|
||||
|
||||
/// <summary>
|
||||
/// Number of items/rows with lowest ItemNr = 0,...
|
||||
/// that cannot be deleted, moved down, renamed, etc.
|
||||
/// </summary>
|
||||
public int FixedRowsCount;
|
||||
|
||||
/// <summary>
|
||||
/// List of removed items that have to be removed from the database
|
||||
/// when completed with OK.
|
||||
/// </summary>
|
||||
public IList<MyItem> ToBeRemovedItems;
|
||||
|
||||
/// <summary>
|
||||
/// true = editing was unlocked
|
||||
/// </summary>
|
||||
public bool Unlocked { get { return unlocked; } } /// read only
|
||||
bool unlocked;
|
||||
|
||||
public BaseWithListViewEx()
|
||||
: base()
|
||||
{
|
||||
ToBeRemovedItems = new List<MyItem>();
|
||||
|
||||
/// Initialize the base (ListViewEx)
|
||||
this.SuspendLayout();
|
||||
AllowColumnReorder = false;
|
||||
Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
DoubleClickActivation = false;
|
||||
FullRowSelect = true;
|
||||
GridLines = true;
|
||||
Location = new System.Drawing.Point(0, 0);
|
||||
Name = "baseWithListViewEx";
|
||||
Size = new System.Drawing.Size(150, 150);
|
||||
TabIndex = 0;
|
||||
UseCompatibleStateImageBehavior = false;
|
||||
View = System.Windows.Forms.View.Details;
|
||||
ResumeLayout(false);
|
||||
}
|
||||
|
||||
public void Unlock()
|
||||
{
|
||||
unlocked = true;
|
||||
|
||||
if (base.SelectedItems.Count == 1)
|
||||
{
|
||||
int ix = SelectedIndices[0];
|
||||
Focus();
|
||||
Items[ix].Selected = true;
|
||||
Items[ix].EnsureVisible();
|
||||
}
|
||||
}
|
||||
|
||||
protected void RedrawAll()
|
||||
{
|
||||
Items.Clear();
|
||||
foreach (var myItem in MyItems)
|
||||
{
|
||||
DrawOne(myItem);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void DrawOne(object myItem)
|
||||
{
|
||||
}
|
||||
|
||||
void UpdateAll()
|
||||
{
|
||||
if (MyItems == null || (MyItems.Count != Items.Count)) return;
|
||||
|
||||
foreach (ListViewItem lvi in Items)
|
||||
{
|
||||
UpdateOne(lvi, (MyItem)lvi.Tag);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void UpdateOne(ListViewItem lvi, object myItem)
|
||||
{
|
||||
}
|
||||
|
||||
public void OkBtnClicked()
|
||||
{
|
||||
UpdateAll();
|
||||
}
|
||||
|
||||
public void CancelBtnClicked()
|
||||
{
|
||||
}
|
||||
|
||||
public void AddOne(MyItem myItem)
|
||||
{
|
||||
MyItems.Add(myItem);
|
||||
DrawOne(myItem);
|
||||
|
||||
Focus();
|
||||
SelectedItems.Clear();
|
||||
Items[Items.Count - 1].Selected = true;
|
||||
Items[Items.Count - 1].EnsureVisible();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when 'Remove' button was clicked.
|
||||
/// </summary>
|
||||
/// <returns>true when the last item was removed and 'Remove' button should be disabled</returns>
|
||||
public bool RemoveSelected()
|
||||
{
|
||||
if (SelectedItems.Count != 1) return false;
|
||||
|
||||
UpdateAll();
|
||||
|
||||
int removeItemNr = SelectedIndices[0];
|
||||
if (removeItemNr < FixedRowsCount) return false;
|
||||
|
||||
MyItem selectedItem = (MyItem)SelectedItems[0].Tag;
|
||||
|
||||
if ((selectedItem as Entities.IHasItemNr).Id != 0) ToBeRemovedItems.Add(selectedItem);
|
||||
MyItems.RemoveAt(removeItemNr);
|
||||
if (selectedItem is Entities.IHasItemNr)
|
||||
{
|
||||
for (int i = removeItemNr; i < MyItems.Count; i++)
|
||||
{
|
||||
(MyItems[i] as Entities.IHasItemNr).ItemNr--;
|
||||
}
|
||||
}
|
||||
|
||||
RedrawAll();
|
||||
|
||||
if (removeItemNr < base.Items.Count)
|
||||
{
|
||||
Focus();
|
||||
Items[removeItemNr].Selected = true;
|
||||
Items[removeItemNr].EnsureVisible();
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveUpSelected()
|
||||
{
|
||||
if (SelectedItems.Count != 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int ix = base.SelectedIndices[0];
|
||||
if (ix <= FixedRowsCount || ix >= MyItems.Count) return;
|
||||
|
||||
UpdateAll();
|
||||
|
||||
MyItem item1 = (MyItem)(Items[ix - 1].Tag);
|
||||
MyItem item2 = (MyItem)(Items[ix].Tag);
|
||||
(item1 as Entities.IHasItemNr).ItemNr++;
|
||||
(item2 as Entities.IHasItemNr).ItemNr--;
|
||||
MyItems.RemoveAt(ix);
|
||||
MyItems.Insert(ix - 1, item2);
|
||||
|
||||
RedrawAll();
|
||||
|
||||
Focus();
|
||||
Items[ix - 1].Selected = true;
|
||||
Items[ix - 1].EnsureVisible();
|
||||
}
|
||||
|
||||
public void MoveDownSelected()
|
||||
{
|
||||
if (base.SelectedItems.Count != 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int ix = base.SelectedIndices[0];
|
||||
if (ix < FixedRowsCount || ix >= MyItems.Count - 1) return;
|
||||
|
||||
UpdateAll();
|
||||
|
||||
MyItem item1 = (MyItem)(Items[ix].Tag);
|
||||
MyItem item2 = (MyItem)(Items[ix + 1].Tag);
|
||||
(item1 as Entities.IHasItemNr).ItemNr++;
|
||||
(item2 as Entities.IHasItemNr).ItemNr--;
|
||||
MyItems.RemoveAt(ix + 1);
|
||||
MyItems.Insert(ix, item2);
|
||||
|
||||
RedrawAll();
|
||||
|
||||
Focus();
|
||||
Items[ix + 1].Selected = true;
|
||||
Items[ix + 1].EnsureVisible();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user