ListViewItemSimpleForm1.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //
  2. // Test application for the ListView class implementation
  3. //
  4. // Author:
  5. // Jordi Mas i Hernàndez, [email protected]
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Windows.Forms;
  10. using System.Drawing;
  11. public class MyListViewForm : System.Windows.Forms.Form
  12. {
  13. ColumnHeader column1 = null;
  14. ColumnHeader column2 = null;
  15. ColumnHeader column3 = null;
  16. ColumnHeader column4 = null;
  17. myListView listViewCtrl = null;
  18. ListView.SelectedListViewItemCollection sel = null;
  19. int nColInserted = 100;
  20. public static void Main(string[] args)
  21. {
  22. Application.Run(new MyListViewForm());
  23. }
  24. // Clear all columns
  25. public void ClearColumnsButton()
  26. {
  27. listViewCtrl.Columns.Clear();
  28. }
  29. public void ShowColumnsButton()
  30. {
  31. string sTxt = "";
  32. // How the elements are order once an element in deleted
  33. for (int i=0; i < listViewCtrl.Columns.Count; i++)
  34. sTxt+=("Column: \"" + listViewCtrl.Columns[i].Text + "\" idx: " + listViewCtrl.Columns[i].Index + " witdh:"+listViewCtrl.Columns[i].Width + "\r");
  35. MessageBox.Show(sTxt);
  36. }
  37. public void AddColumnsButton()
  38. {
  39. string sColText;
  40. sColText = "Column " + nColInserted;
  41. Console.WriteLine ("AddColumnsButton->" + sColText);
  42. listViewCtrl.Columns.Insert(0, sColText, 150, HorizontalAlignment.Left);
  43. nColInserted++;
  44. }
  45. public void ClearButton()
  46. {
  47. Console.WriteLine ("MyListViewForm.Clear");
  48. listViewCtrl.Clear();
  49. // How the elements are order once an element in deleted
  50. for (int i=0; i < listViewCtrl.Columns.Count; i++)
  51. Console.WriteLine ("Column " + listViewCtrl.Columns[i].Text + " idx: " + listViewCtrl.Columns[i].Index);
  52. // Items
  53. for (int i=0; i < listViewCtrl.Items.Count; i++)
  54. Console.WriteLine ("Item->" + listViewCtrl.Items[i].Text + " idx: " + listViewCtrl.Items[i].Index);
  55. // Selected Items
  56. for (int i=0; i < listViewCtrl.SelectedItems.Count; i++)
  57. Console.WriteLine ("Sel Item->" + listViewCtrl.SelectedItems[i].Text + " idx: " + listViewCtrl.SelectedItems[i].Index);
  58. }
  59. public void DelColumnButton()
  60. {
  61. listViewCtrl.Columns.RemoveAt(1); /*Base on 0 index*/
  62. }
  63. public void DumpSelButton()
  64. {
  65. // Show selected items
  66. if (sel==null)
  67. {
  68. Console.WriteLine ("Col init");
  69. sel = listViewCtrl.SelectedItems;
  70. }
  71. Console.WriteLine ("Selected---------------");
  72. for (int i=0; i < sel.Count; i++)
  73. Console.WriteLine ("Item->" + sel[i].Text + " idx: " + sel[i].Index);
  74. }
  75. public void DelItemButton()
  76. {
  77. Console.WriteLine ("Elements ");
  78. listViewCtrl.Items.RemoveAt(2);
  79. // How the elements are order once an element in deleted
  80. for (int i=0; i < listViewCtrl.Items.Count; i++)
  81. Console.WriteLine ("Items " + listViewCtrl.Items[i].Text + " idx: " + listViewCtrl.Items[i].Index);
  82. }
  83. public void ShowClassDefaults()
  84. {
  85. Console.WriteLine ("Sorting " + listViewCtrl.Sorting);
  86. Console.WriteLine ("Label Edit " + listViewCtrl.LabelEdit);
  87. Console.WriteLine ("FullRowSelect " + listViewCtrl.FullRowSelect);
  88. Console.WriteLine ("GridLines " + listViewCtrl.GridLines);
  89. Console.WriteLine ("AutoArrange " + listViewCtrl.AutoArrange);
  90. Console.WriteLine ("LabelWrap " + listViewCtrl.LabelWrap);
  91. Console.WriteLine ("MultiSelect " + listViewCtrl.MultiSelect);
  92. Console.WriteLine ("ForeColor " + listViewCtrl.ForeColor);
  93. Console.WriteLine ("BackColor " + listViewCtrl.BackColor);
  94. Console.WriteLine ("ItemActivation " + listViewCtrl.Activation);
  95. Console.WriteLine ("ColumnHeaderStyle " + listViewCtrl.HeaderStyle);
  96. }
  97. public MyListViewForm()
  98. {
  99. InitializeComponent();
  100. }
  101. private void ColumnSample()
  102. {
  103. listViewCtrl = new myListView();
  104. ShowClassDefaults();
  105. listViewCtrl.HeaderStyle = ColumnHeaderStyle.None;
  106. // Set params
  107. listViewCtrl.View = View.Details;
  108. //listViewCtrl.LabelEdit = true;
  109. listViewCtrl.AllowColumnReorder=true;
  110. listViewCtrl.FullRowSelect = true;
  111. listViewCtrl.GridLines = true;
  112. listViewCtrl.Activation = ItemActivation.OneClick;
  113. listViewCtrl.Bounds = new Rectangle(new Point(10,60), new Size(600, 550));
  114. ListViewItem item1 = new ListViewItem("item1");
  115. ListViewItem item2 = new ListViewItem("item2");
  116. ListViewItem item3 = new ListViewItem("item3");
  117. ListViewItem item4 = new ListViewItem("item4");
  118. ListViewItem item5 = new ListViewItem("item5");
  119. ListViewItem item6 = new ListViewItem("item6");
  120. ListViewItem item7 = new ListViewItem("item7");
  121. ListViewItem item8 = new ListViewItem("item8");
  122. ListViewItem item9 = new ListViewItem("item9");
  123. ListViewItem item10 = new ListViewItem("item10 aaaaaaaaaaaaaaaaaaaaaaaaa");
  124. Console.WriteLine ("*Column 1");
  125. column1 = listViewCtrl.Columns.Add("Column 1", -1, HorizontalAlignment.Left);
  126. Console.WriteLine ("*Column 2");
  127. column2 = listViewCtrl.Columns.Add("Column 2", -2, HorizontalAlignment.Right);
  128. column3 = listViewCtrl.Columns.Add("Column 3", 50, HorizontalAlignment.Right);
  129. column4 = new ColumnHeader();
  130. column4.Text="Column 4";
  131. column4.Width= 150;
  132. //listViewCtrl.Columns.AddRange(new ColumnHeader[]{column4});
  133. listViewCtrl.Items.Add(item1);
  134. listViewCtrl.Items.Add(item2);
  135. listViewCtrl.Items.AddRange(new ListViewItem[]{item3,item4,item5,item6,item7,item8,item9,item10});
  136. item1.SubItems.Add("sub item 1");
  137. item1.SubItems.Add("sub item 2");
  138. listViewCtrl.Items.Add( new ListViewItem(new string[]{"boy 1", "boy 2", "boy 3"}));
  139. DelColumnButton button = new DelColumnButton(this);
  140. button.Location = new System.Drawing.Point(5, 10);
  141. button.Name = "button1";
  142. button.Size = new System.Drawing.Size(100, 30);
  143. button.Text = "Delete Column 2";
  144. button.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  145. Controls.Add(button);
  146. ClearColumnsButton button5 = new ClearColumnsButton(this);
  147. button5.Location = new System.Drawing.Point(115, 10);
  148. button5.Name = "button5";
  149. button5.Size = new System.Drawing.Size(100, 30);
  150. button5.Text = "Clear Columns";
  151. button5.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  152. Controls.Add(button5);
  153. AddColumnsButton button6 = new AddColumnsButton(this);
  154. button6.Location = new System.Drawing.Point(225, 10);
  155. button6.Name = "button6";
  156. button6.Size = new System.Drawing.Size(100, 30);
  157. button6.Text = "Add Column";
  158. button6.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  159. Controls.Add(button6);
  160. ShowColumnsButton button7 = new ShowColumnsButton(this);
  161. button7.Location = new System.Drawing.Point(335, 10);
  162. button7.Name = "button7";
  163. button7.Size = new System.Drawing.Size(100, 30);
  164. button7.Text = "Show Columns";
  165. button7.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  166. Controls.Add(button7);
  167. DelItemButton button2 = new DelItemButton(this);
  168. button2.Location = new System.Drawing.Point(630, 90);
  169. button2.Name = "button2";
  170. button2.Size = new System.Drawing.Size(100, 30);
  171. button2.Text = "Delete Item 3";
  172. button2.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  173. Controls.Add(button2);
  174. DumpSelButton button3 = new DumpSelButton(this);
  175. button3.Location = new System.Drawing.Point(630, 120);
  176. button3.Name = "button3";
  177. button3.Size = new System.Drawing.Size(100, 30);
  178. button3.Text = "Show selection";
  179. button3.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  180. Controls.Add(button3);
  181. ClearButton button4 = new ClearButton(this);
  182. button4.Location = new System.Drawing.Point(630, 150);
  183. button4.Name = "button4";
  184. button4.Size = new System.Drawing.Size(100, 30);
  185. button4.Text = "Clear";
  186. button4.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  187. Controls.Add(button4);
  188. Controls.Add(listViewCtrl);
  189. }
  190. private void InitializeComponent()
  191. {
  192. ClientSize = new System.Drawing.Size(750, 650);
  193. ColumnSample();
  194. return;
  195. }
  196. }
  197. // Delete column
  198. public class DelColumnButton : System.Windows.Forms.Button{
  199. MyListViewForm form = null;
  200. public DelColumnButton(MyListViewForm frm) : base()
  201. {
  202. form = frm;
  203. }
  204. /* User clicks the button*/
  205. protected override void OnClick(EventArgs e)
  206. {
  207. form.DelColumnButton();
  208. }
  209. }
  210. // Delete item
  211. public class DelItemButton : System.Windows.Forms.Button{
  212. MyListViewForm form = null;
  213. public DelItemButton(MyListViewForm frm) : base()
  214. {
  215. form = frm;
  216. }
  217. /* User clicks the button*/
  218. protected override void OnClick(EventArgs e)
  219. {
  220. form.DelItemButton();
  221. }
  222. }
  223. // Show selection
  224. public class DumpSelButton : System.Windows.Forms.Button{
  225. MyListViewForm form = null;
  226. public DumpSelButton(MyListViewForm frm) : base()
  227. {
  228. form = frm;
  229. }
  230. /* User clicks the button*/
  231. protected override void OnClick(EventArgs e)
  232. {
  233. form.DumpSelButton();
  234. }
  235. }
  236. // Show columns
  237. public class ShowColumnsButton : System.Windows.Forms.Button{
  238. MyListViewForm form = null;
  239. public ShowColumnsButton(MyListViewForm frm) : base()
  240. {
  241. form = frm;
  242. }
  243. /* User clicks the button*/
  244. protected override void OnClick(EventArgs e)
  245. {
  246. form.ShowColumnsButton();
  247. }
  248. }
  249. // ClearColumnsButton
  250. public class ClearColumnsButton : System.Windows.Forms.Button{
  251. MyListViewForm form = null;
  252. public ClearColumnsButton(MyListViewForm frm) : base()
  253. {
  254. form = frm;
  255. }
  256. /* User clicks the button*/
  257. protected override void OnClick(EventArgs e)
  258. {
  259. form.ClearColumnsButton();
  260. }
  261. }
  262. // AddColumnsButton
  263. public class AddColumnsButton : System.Windows.Forms.Button{
  264. MyListViewForm form = null;
  265. public AddColumnsButton(MyListViewForm frm) : base()
  266. {
  267. form = frm;
  268. }
  269. /* User clicks the button*/
  270. protected override void OnClick(EventArgs e)
  271. {
  272. form.AddColumnsButton();
  273. }
  274. }
  275. // ClearButton
  276. public class ClearButton : System.Windows.Forms.Button{
  277. MyListViewForm form = null;
  278. public ClearButton(MyListViewForm frm) : base()
  279. {
  280. form = frm;
  281. }
  282. /* User clicks the button*/
  283. protected override void OnClick(EventArgs e)
  284. {
  285. form.ClearButton();
  286. }
  287. }
  288. public class myListView : System.Windows.Forms.ListView
  289. {
  290. protected override void OnColumnClick(ColumnClickEventArgs e) {
  291. Console.WriteLine ("Column " + Columns[e.Column].Text + " idx: " + Columns[e.Column].Index);
  292. }
  293. protected override void OnBeforeLabelEdit(LabelEditEventArgs e){
  294. Console.WriteLine ("OnBeforeLabelEdit. CancelEdit->" + e.CancelEdit + " Item-> "+e.Item + " Label->"+e.Label );
  295. //e.CancelEdit = true;
  296. }
  297. protected override void OnAfterLabelEdit(LabelEditEventArgs e){
  298. Console.WriteLine ("OnAfterLabelEdit. CancelEdit->" + e.CancelEdit + " Item-> "+e.Item + " Label->"+e.Label );
  299. e.CancelEdit = true;
  300. }
  301. protected override void OnItemActivate(EventArgs ice){
  302. Console.WriteLine ("OnItemActivate");
  303. }
  304. }