ListViewItemSimpleForm1.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. using System.Threading;
  12. public class MyListViewForm : System.Windows.Forms.Form
  13. {
  14. ColumnHeader column1 = null;
  15. ColumnHeader column2 = null;
  16. ColumnHeader column3 = null;
  17. ColumnHeader column4 = null;
  18. myListView listViewCtrl = null;
  19. ListView.SelectedListViewItemCollection sel = null;
  20. int nColInserted = 100;
  21. static void SimpleHandler(object Sender, ThreadExceptionEventArgs Args)
  22. {
  23. Exception e = (Exception)Args.Exception;
  24. Console.WriteLine ( "Caught : " + e.Message ) ;
  25. }
  26. public static void Main(string[] args)
  27. {
  28. //Application.ThreadException += new ThreadExceptionEventHandler(SimpleHandler) ;
  29. Application.Run(new MyListViewForm());
  30. }
  31. // Clear all columns
  32. public void ClearColumnsButton()
  33. {
  34. listViewCtrl.Columns.Clear();
  35. }
  36. public void ItemPropButton()
  37. {
  38. //IList lst = (IList) listViewCtrl.CheckedItems;
  39. string sText = "Item properties---------------\n";
  40. for (int i=0; i < listViewCtrl.Items.Count; i++)
  41. sText+=("Item->" + listViewCtrl.Items[i].Text + " idx: " + listViewCtrl.Items[i].Index +
  42. " Backcolor: "+ listViewCtrl.Items[i].BackColor + " Selected: " + listViewCtrl.Items[i].Selected +
  43. " Checked: " + listViewCtrl.Items[i].Checked + " Focused: " + listViewCtrl.Items[i].Focused +
  44. " Bounds: " + listViewCtrl.Items[i].Bounds+
  45. "\n");
  46. MessageBox.Show(sText);
  47. }
  48. public void CheckedItemButton()
  49. {
  50. listViewCtrl.CheckBoxes = !listViewCtrl.CheckBoxes;
  51. }
  52. public void ShowColumnsButton()
  53. {
  54. string sTxt = "";
  55. // How the elements are order once an element in deleted
  56. for (int i=0; i < listViewCtrl.Columns.Count; i++)
  57. sTxt+=("Column: \"" + listViewCtrl.Columns[i].Text + "\" idx: " + listViewCtrl.Columns[i].Index + " witdh:"+listViewCtrl.Columns[i].Width + "\r");
  58. MessageBox.Show(sTxt);
  59. }
  60. public void AddColumnsButton()
  61. {
  62. string sColText;
  63. sColText = "Column " + nColInserted;
  64. Console.WriteLine ("AddColumnsButton->" + sColText);
  65. listViewCtrl.Columns.Insert(0, sColText, 150, HorizontalAlignment.Left);
  66. nColInserted++;
  67. }
  68. public void ClearButton()
  69. {
  70. Console.WriteLine ("MyListViewForm.Clear");
  71. listViewCtrl.Clear();
  72. // How the elements are order once an element in deleted
  73. for (int i=0; i < listViewCtrl.Columns.Count; i++)
  74. Console.WriteLine ("Column " + listViewCtrl.Columns[i].Text + " idx: " + listViewCtrl.Columns[i].Index);
  75. // Items
  76. for (int i=0; i < listViewCtrl.Items.Count; i++)
  77. Console.WriteLine ("Item->" + listViewCtrl.Items[i].Text + " idx: " + listViewCtrl.Items[i].Index);
  78. // Selected Items
  79. for (int i=0; i < listViewCtrl.SelectedItems.Count; i++)
  80. Console.WriteLine ("Sel Item->" + listViewCtrl.SelectedItems[i].Text + " idx: " + listViewCtrl.SelectedItems[i].Index);
  81. }
  82. public void DelColumnButton()
  83. {
  84. listViewCtrl.Columns.RemoveAt(1); /*Base on 0 index*/
  85. }
  86. // Show selected items
  87. public void DumpSelButton()
  88. {
  89. if (sel==null)
  90. sel = listViewCtrl.SelectedItems;
  91. string sText;
  92. if (sel==null)
  93. sel = listViewCtrl.SelectedItems;
  94. sText = "Selected---------------\n";
  95. for (int i=0; i < sel.Count; i++)
  96. sText+=("Item->" + sel[i].Text + " idx: " + sel[i].Index + "\n");
  97. sText+= "Checked Items---------------\n";
  98. for (int i=0; i < listViewCtrl.CheckedItems.Count; i++)
  99. sText+=("Item->" + listViewCtrl.CheckedItems[i].Text + " idx: " + listViewCtrl.CheckedItems[i].Index + "\n");
  100. MessageBox.Show(sText);
  101. }
  102. public void DelItemButton()
  103. {
  104. Console.WriteLine ("Elements ");
  105. listViewCtrl.Items.RemoveAt(2);
  106. // How the elements are order once an element in deleted
  107. for (int i=0; i < listViewCtrl.Items.Count; i++)
  108. Console.WriteLine ("Items " + listViewCtrl.Items[i].Text + " idx: " + listViewCtrl.Items[i].Index);
  109. }
  110. public void ShowClassDefaults()
  111. {
  112. Console.WriteLine ("ListView defaults----");
  113. Console.WriteLine ("Sorting " + listViewCtrl.Sorting);
  114. Console.WriteLine ("Label Edit " + listViewCtrl.LabelEdit);
  115. Console.WriteLine ("FullRowSelect " + listViewCtrl.FullRowSelect);
  116. Console.WriteLine ("GridLines " + listViewCtrl.GridLines);
  117. Console.WriteLine ("AutoArrange " + listViewCtrl.AutoArrange);
  118. Console.WriteLine ("LabelWrap " + listViewCtrl.LabelWrap);
  119. Console.WriteLine ("MultiSelect " + listViewCtrl.MultiSelect);
  120. Console.WriteLine ("ForeColor " + listViewCtrl.ForeColor);
  121. Console.WriteLine ("BackColor " + listViewCtrl.BackColor);
  122. Console.WriteLine ("ItemActivation " + listViewCtrl.Activation);
  123. Console.WriteLine ("ColumnHeaderStyle " + listViewCtrl.HeaderStyle);
  124. Console.WriteLine ("BorderStyle " + listViewCtrl.BorderStyle);
  125. Console.WriteLine ("HideSelection " + listViewCtrl.HideSelection);
  126. Console.WriteLine ("HoverSelection " + listViewCtrl.HoverSelection);
  127. ListViewItem item = new ListViewItem();
  128. Console.WriteLine ("ListView item----");
  129. Console.WriteLine ("BackColor " + item.BackColor);
  130. Console.WriteLine ("ForeColor " + item.ForeColor);
  131. Console.WriteLine ("UseItemStyleForSubItems " + item.UseItemStyleForSubItems);
  132. }
  133. public MyListViewForm()
  134. {
  135. InitializeComponent();
  136. }
  137. private void ColumnSample()
  138. {
  139. listViewCtrl = new myListView();
  140. ShowClassDefaults();
  141. // Set params
  142. listViewCtrl.View = View.Details;
  143. //listViewCtrl.LabelEdit = true;
  144. listViewCtrl.AllowColumnReorder=true;
  145. listViewCtrl.FullRowSelect = true;
  146. listViewCtrl.GridLines = true;
  147. //listViewCtrl.Activation = ItemActivation.OneClick;
  148. listViewCtrl.Bounds = new Rectangle(new Point(10,60), new Size(600, 550));
  149. ListViewItem item1 = new ListViewItem("item1");
  150. ListViewItem item2 = new ListViewItem("Yellow item");
  151. ListViewItem item3 = new ListViewItem("item3");
  152. ListViewItem item4 = new ListViewItem("item4");
  153. ListViewItem item5 = new ListViewItem("item5");
  154. ListViewItem item6 = new ListViewItem("Green item");
  155. ListViewItem item7 = new ListViewItem("item7");
  156. ListViewItem item8 = new ListViewItem("item8");
  157. ListViewItem item9 = new ListViewItem("item9");
  158. ListViewItem item10 = new ListViewItem("This is a long text");
  159. ListViewItem.ListViewSubItem subItem11_1 = new ListViewItem.ListViewSubItem();
  160. ListViewItem.ListViewSubItem subItem11_2 = new ListViewItem.ListViewSubItem();
  161. subItem11_1.Text = "subitem 11-1";
  162. subItem11_2.Text = "subitem 11-2";
  163. column1 = listViewCtrl.Columns.Add("Column 1", -1, HorizontalAlignment.Left);
  164. column2 = listViewCtrl.Columns.Add("Column 2", -2, HorizontalAlignment.Right);
  165. column3 = listViewCtrl.Columns.Add("Column 3", 50, HorizontalAlignment.Right);
  166. column4 = new ColumnHeader();
  167. column4.Text="Column 4";
  168. column4.Width= 150;
  169. item2.BackColor = Color.Yellow;
  170. item2.ForeColor = Color.Blue;
  171. item2.SubItems.Add("yellow-blue subitem 1");
  172. item2.SubItems.Add("yellow-blue subitem 2");
  173. item6.BackColor = Color.Green;
  174. item6.UseItemStyleForSubItems = false;
  175. ListViewItem.ListViewSubItem subItem= item6.SubItems.Add("Red subitem 1");
  176. subItem.BackColor = Color.Red;
  177. subItem.ForeColor = Color.White;
  178. listViewCtrl.Items.Add(item1);
  179. listViewCtrl.Items.Add(item2);
  180. listViewCtrl.Items.AddRange(new ListViewItem[]{item3,item4,item5,item6,item7,item8,item9,item10});
  181. item1.SubItems.Add("sub item 1");
  182. item1.SubItems.Add("sub item 2");
  183. listViewCtrl.Items.Add( new ListViewItem(new string[]{"boy 1", "boy 2", "boy 3"}));
  184. listViewCtrl.Items.Add( new ListViewItem(new string[]{"burger 1", "burger 2", "burger 3"},
  185. 0, Color.White, Color.Black, new Font("Arial", (float)9.0, FontStyle.Regular) ));
  186. DelColumnButton button = new DelColumnButton(this);
  187. button.Location = new System.Drawing.Point(5, 10);
  188. button.Name = "button1";
  189. button.Size = new System.Drawing.Size(100, 30);
  190. button.Text = "Delete Column 2";
  191. button.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  192. Controls.Add(button);
  193. ClearColumnsButton button5 = new ClearColumnsButton(this);
  194. button5.Location = new System.Drawing.Point(115, 10);
  195. button5.Name = "button5";
  196. button5.Size = new System.Drawing.Size(100, 30);
  197. button5.Text = "Clear Columns";
  198. button5.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  199. Controls.Add(button5);
  200. AddColumnsButton button6 = new AddColumnsButton(this);
  201. button6.Location = new System.Drawing.Point(225, 10);
  202. button6.Name = "button6";
  203. button6.Size = new System.Drawing.Size(100, 30);
  204. button6.Text = "Add Column";
  205. button6.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  206. Controls.Add(button6);
  207. ShowColumnsButton button7 = new ShowColumnsButton(this);
  208. button7.Location = new System.Drawing.Point(335, 10);
  209. button7.Name = "button7";
  210. button7.Size = new System.Drawing.Size(100, 30);
  211. button7.Text = "Show Columns";
  212. button7.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  213. Controls.Add(button7);
  214. DelItemButton button2 = new DelItemButton(this);
  215. button2.Location = new System.Drawing.Point(630, 90);
  216. button2.Name = "button2";
  217. button2.Size = new System.Drawing.Size(100, 30);
  218. button2.Text = "Delete Item 3";
  219. button2.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  220. Controls.Add(button2);
  221. CheckedItemButton button8 = new CheckedItemButton(this);
  222. button8.Location = new System.Drawing.Point(630, 50);
  223. button8.Name = "button2";
  224. button8.Size = new System.Drawing.Size(100, 30);
  225. button8.Text = "Checked on/off";
  226. button8.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  227. Controls.Add(button8);
  228. DumpSelButton button3 = new DumpSelButton(this);
  229. button3.Location = new System.Drawing.Point(630, 120);
  230. button3.Name = "button3";
  231. button3.Size = new System.Drawing.Size(100, 30);
  232. button3.Text = "Show selection";
  233. button3.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  234. Controls.Add(button3);
  235. ClearButton button4 = new ClearButton(this);
  236. button4.Location = new System.Drawing.Point(630, 150);
  237. button4.Name = "button4";
  238. button4.Size = new System.Drawing.Size(100, 30);
  239. button4.Text = "Clear";
  240. button4.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  241. Controls.Add(button4);
  242. ItemPropButton button9 = new ItemPropButton(this);
  243. button9.Location = new System.Drawing.Point(630, 180);
  244. button9.Name = "button9";
  245. button9.Size = new System.Drawing.Size(100, 30);
  246. button9.Text = "Item Properties";
  247. button9.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  248. Controls.Add(button9);
  249. Controls.Add(listViewCtrl);
  250. }
  251. private void InitializeComponent()
  252. {
  253. ClientSize = new System.Drawing.Size(750, 650);
  254. ColumnSample();
  255. return;
  256. }
  257. }
  258. // Delete column
  259. public class DelColumnButton : System.Windows.Forms.Button{
  260. MyListViewForm form = null;
  261. public DelColumnButton(MyListViewForm frm) : base()
  262. {
  263. form = frm;
  264. }
  265. /* User clicks the button*/
  266. protected override void OnClick(EventArgs e)
  267. {
  268. form.DelColumnButton();
  269. }
  270. }
  271. // Delete item
  272. public class DelItemButton : System.Windows.Forms.Button{
  273. MyListViewForm form = null;
  274. public DelItemButton(MyListViewForm frm) : base()
  275. {
  276. form = frm;
  277. }
  278. /* User clicks the button*/
  279. protected override void OnClick(EventArgs e)
  280. {
  281. form.DelItemButton();
  282. }
  283. }
  284. // Show selection
  285. public class DumpSelButton : System.Windows.Forms.Button{
  286. MyListViewForm form = null;
  287. public DumpSelButton(MyListViewForm frm) : base()
  288. {
  289. form = frm;
  290. }
  291. /* User clicks the button*/
  292. protected override void OnClick(EventArgs e)
  293. {
  294. form.DumpSelButton();
  295. }
  296. }
  297. // Show columns
  298. public class ShowColumnsButton : System.Windows.Forms.Button{
  299. MyListViewForm form = null;
  300. public ShowColumnsButton(MyListViewForm frm) : base()
  301. {
  302. form = frm;
  303. }
  304. /* User clicks the button*/
  305. protected override void OnClick(EventArgs e)
  306. {
  307. form.ShowColumnsButton();
  308. }
  309. }
  310. // ClearColumnsButton
  311. public class ClearColumnsButton : System.Windows.Forms.Button{
  312. MyListViewForm form = null;
  313. public ClearColumnsButton(MyListViewForm frm) : base()
  314. {
  315. form = frm;
  316. }
  317. /* User clicks the button*/
  318. protected override void OnClick(EventArgs e)
  319. {
  320. form.ClearColumnsButton();
  321. }
  322. }
  323. // CheckedItemButton
  324. public class CheckedItemButton : System.Windows.Forms.Button{
  325. MyListViewForm form = null;
  326. public CheckedItemButton(MyListViewForm frm) : base()
  327. {
  328. form = frm;
  329. }
  330. /* User clicks the button*/
  331. protected override void OnClick(EventArgs e)
  332. {
  333. form.CheckedItemButton();
  334. }
  335. }
  336. // ItemPropButton
  337. public class ItemPropButton : System.Windows.Forms.Button{
  338. MyListViewForm form = null;
  339. public ItemPropButton(MyListViewForm frm) : base()
  340. {
  341. form = frm;
  342. }
  343. /* User clicks the button*/
  344. protected override void OnClick(EventArgs e)
  345. {
  346. form.ItemPropButton();
  347. }
  348. }
  349. // AddColumnsButton
  350. public class AddColumnsButton : System.Windows.Forms.Button{
  351. MyListViewForm form = null;
  352. public AddColumnsButton(MyListViewForm frm) : base()
  353. {
  354. form = frm;
  355. }
  356. /* User clicks the button*/
  357. protected override void OnClick(EventArgs e)
  358. {
  359. form.AddColumnsButton();
  360. }
  361. }
  362. // ClearButton
  363. public class ClearButton : System.Windows.Forms.Button{
  364. MyListViewForm form = null;
  365. public ClearButton(MyListViewForm frm) : base()
  366. {
  367. form = frm;
  368. }
  369. /* User clicks the button*/
  370. protected override void OnClick(EventArgs e)
  371. {
  372. form.ClearButton();
  373. }
  374. }
  375. public class myListView : System.Windows.Forms.ListView
  376. {
  377. protected override void OnColumnClick(ColumnClickEventArgs e) {
  378. Console.WriteLine ("Column " + Columns[e.Column].Text + " idx: " + Columns[e.Column].Index);
  379. }
  380. protected override void OnBeforeLabelEdit(LabelEditEventArgs e){
  381. Console.WriteLine ("OnBeforeLabelEdit. CancelEdit->" + e.CancelEdit + " Item-> "+e.Item + " Label->"+e.Label );
  382. //e.CancelEdit = true;
  383. }
  384. protected override void OnAfterLabelEdit(LabelEditEventArgs e){
  385. Console.WriteLine ("OnAfterLabelEdit. CancelEdit->" + e.CancelEdit + " Item-> "+e.Item + " Label->"+e.Label );
  386. e.CancelEdit = true;
  387. }
  388. protected override void OnItemActivate(EventArgs ice){
  389. Console.WriteLine ("OnItemActivate");
  390. }
  391. }