ListBox.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. // TODO:
  26. // - Keyboard navigation
  27. // - Horizontal item scroll
  28. // - Performance testing
  29. //
  30. //
  31. // NOT COMPLETE
  32. using System;
  33. using System.Drawing;
  34. using System.Collections;
  35. using System.ComponentModel;
  36. using System.Reflection;
  37. namespace System.Windows.Forms
  38. {
  39. public class ListBox : ListControl
  40. {
  41. internal class ListBoxInfo
  42. {
  43. internal int item_height; /* Item's height */
  44. internal int top_item; /* First item that we show the in the current page */
  45. internal int last_item; /* Last visible item */
  46. internal int page_size; /* Number of listbox items per page. In MultiColumn listbox indicates items per column */
  47. internal Rectangle textdrawing_rect; /* Displayable Client Rectangle minus the scrollbars and with IntegralHeight calculated*/
  48. internal bool show_verticalsb; /* Is Vertical scrollbar show it? */
  49. internal bool show_horizontalsb; /* Is Horizontal scrollbar show it? */
  50. internal Rectangle client_rect; /* Client Rectangle. Usually = ClientRectangle except when IntegralHeight has been applied*/
  51. internal int max_itemwidth; /* Maxium item width within the listbox */
  52. public ListBoxInfo ()
  53. {
  54. last_item = 0;
  55. item_height = 0;
  56. top_item = 0;
  57. page_size = 0;
  58. max_itemwidth = 0;
  59. show_verticalsb = false;
  60. show_horizontalsb = false;
  61. }
  62. }
  63. internal class ListBoxItem
  64. {
  65. internal int Index;
  66. internal bool Selected;
  67. internal CheckState State;
  68. public ListBoxItem (int index)
  69. {
  70. Index = index;
  71. Selected = false;
  72. State = CheckState.Unchecked;
  73. }
  74. }
  75. private BorderStyle border_style;
  76. private int column_width;
  77. private DrawMode draw_mode;
  78. private int horizontal_extent;
  79. private bool horizontal_scrollbar;
  80. private bool integral_height;
  81. private bool multicolumn;
  82. private bool scroll_always_visible;
  83. private int selected_index;
  84. private SelectedIndexCollection selected_indices;
  85. private object selected_item;
  86. private SelectedObjectCollection selected_items;
  87. private SelectionMode selection_mode;
  88. private bool sorted;
  89. private bool use_tabstops;
  90. private int preferred_height;
  91. private int top_index;
  92. private int column_width_internal;
  93. private VScrollBar vscrollbar_ctrl;
  94. private HScrollBar hscrollbar_ctrl;
  95. private bool suspend_ctrlupdate;
  96. internal StringFormat string_format;
  97. internal ListBoxInfo listbox_info;
  98. internal ObjectCollection items;
  99. public ListBox ()
  100. {
  101. border_style = BorderStyle.Fixed3D;
  102. draw_mode = DrawMode.Normal;
  103. horizontal_extent = 0;
  104. horizontal_scrollbar = false;
  105. integral_height = true;
  106. multicolumn = false;
  107. preferred_height = 7;
  108. scroll_always_visible = false;
  109. selected_index = -1;
  110. selected_item = null;
  111. selection_mode = SelectionMode.One;
  112. sorted = false;
  113. top_index = 0;
  114. use_tabstops = true;
  115. BackColor = ThemeEngine.Current.ColorWindow;
  116. ColumnWidth = 0;
  117. suspend_ctrlupdate = false;
  118. items = new ObjectCollection (this);
  119. selected_indices = new SelectedIndexCollection (this);
  120. selected_items = new SelectedObjectCollection (this);
  121. listbox_info = new ListBoxInfo ();
  122. string_format = new StringFormat ();
  123. listbox_info.item_height = FontHeight;
  124. /* Vertical scrollbar */
  125. vscrollbar_ctrl = new VScrollBar ();
  126. vscrollbar_ctrl.Minimum = 0;
  127. vscrollbar_ctrl.SmallChange = 1;
  128. vscrollbar_ctrl.LargeChange = 1;
  129. vscrollbar_ctrl.Maximum = 0;
  130. vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
  131. vscrollbar_ctrl.Visible = false;
  132. /* Horizontal scrollbar */
  133. hscrollbar_ctrl = new HScrollBar ();
  134. hscrollbar_ctrl.Minimum = 0;
  135. hscrollbar_ctrl.SmallChange = 1;
  136. hscrollbar_ctrl.LargeChange = 1;
  137. hscrollbar_ctrl.Maximum = 0;
  138. hscrollbar_ctrl.Visible = false;
  139. hscrollbar_ctrl.ValueChanged += new EventHandler (HorizontalScrollEvent);
  140. /* Events */
  141. MouseDown += new MouseEventHandler (OnMouseDownLB);
  142. UpdateFormatString ();
  143. }
  144. #region Events
  145. public new event EventHandler BackgroundImageChanged;
  146. public new event EventHandler Click;
  147. public event DrawItemEventHandler DrawItem;
  148. public event MeasureItemEventHandler MeasureItem;
  149. public new event PaintEventHandler Paint;
  150. public event EventHandler SelectedIndexChanged;
  151. public new event EventHandler TextChanged;
  152. #endregion // Events
  153. #region Public Properties
  154. public override Color BackColor {
  155. get { return base.BackColor; }
  156. set {
  157. if (base.BackColor == value)
  158. return;
  159. base.BackColor = value;
  160. Refresh ();
  161. }
  162. }
  163. public override Image BackgroundImage {
  164. get { return base.BackgroundImage; }
  165. set {
  166. if (base.BackgroundImage == value)
  167. return;
  168. base.BackgroundImage = value;
  169. if (BackgroundImageChanged != null)
  170. BackgroundImageChanged (this, EventArgs.Empty);
  171. Refresh ();
  172. }
  173. }
  174. public BorderStyle BorderStyle {
  175. get { return border_style; }
  176. set {
  177. if (!Enum.IsDefined (typeof (BorderStyle), value))
  178. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for BorderStyle", value));
  179. if (border_style == value)
  180. return;
  181. border_style = value;
  182. Refresh ();
  183. }
  184. }
  185. public int ColumnWidth {
  186. get { return column_width; }
  187. set {
  188. if (column_width < 0)
  189. throw new ArgumentException ("A value less than zero is assigned to the property.");
  190. column_width = value;
  191. if (value == 0)
  192. ColumnWidthInternal = 120;
  193. else
  194. ColumnWidthInternal = value;
  195. Refresh ();
  196. }
  197. }
  198. protected override CreateParams CreateParams {
  199. get { return base.CreateParams;}
  200. }
  201. protected override Size DefaultSize {
  202. get { return new Size (120, 96); }
  203. }
  204. public virtual DrawMode DrawMode {
  205. get { return draw_mode; }
  206. set {
  207. if (!Enum.IsDefined (typeof (DrawMode), value))
  208. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
  209. if (draw_mode == value)
  210. return;
  211. draw_mode = value;
  212. Refresh ();
  213. }
  214. }
  215. public override Color ForeColor {
  216. get { return base.ForeColor; }
  217. set {
  218. if (base.ForeColor == value)
  219. return;
  220. base.ForeColor = value;
  221. Refresh ();
  222. }
  223. }
  224. public int HorizontalExtent {
  225. get { return horizontal_extent; }
  226. set {
  227. if (horizontal_extent == value)
  228. return;
  229. horizontal_extent = value;
  230. Refresh ();
  231. }
  232. }
  233. public bool HorizontalScrollbar {
  234. get { return horizontal_scrollbar; }
  235. set {
  236. if (horizontal_scrollbar == value)
  237. return;
  238. horizontal_scrollbar = value;
  239. UpdateShowHorizontalScrollBar ();
  240. Refresh ();
  241. }
  242. }
  243. public bool IntegralHeight {
  244. get { return integral_height; }
  245. set {
  246. if (integral_height == value)
  247. return;
  248. integral_height = value;
  249. CalcClientArea ();
  250. }
  251. }
  252. public virtual int ItemHeight {
  253. get { return listbox_info.item_height; }
  254. set {
  255. if (value > 255)
  256. throw new ArgumentOutOfRangeException ("The ItemHeight property was set beyond 255 pixels");
  257. listbox_info.item_height = value;
  258. CalcClientArea ();
  259. }
  260. }
  261. public ObjectCollection Items {
  262. get { return items; }
  263. }
  264. public bool MultiColumn {
  265. get { return multicolumn; }
  266. set {
  267. if (multicolumn == value)
  268. return;
  269. if (value == true && DrawMode == DrawMode.OwnerDrawVariable)
  270. throw new ArgumentException ("A multicolumn ListBox cannot have a variable-sized height.");
  271. multicolumn = value;
  272. UpdateShowVerticalScrollBar (); /* the needs for scrollbars may change */
  273. UpdateShowHorizontalScrollBar ();
  274. Refresh ();
  275. }
  276. }
  277. public int PreferredHeight {
  278. get { return preferred_height;}
  279. }
  280. public override RightToLeft RightToLeft {
  281. get { return base.RightToLeft; }
  282. set {
  283. if (base.RightToLeft == value)
  284. return;
  285. base.RightToLeft = value;
  286. UpdateFormatString ();
  287. Refresh ();
  288. }
  289. }
  290. // Only afects the Vertical ScrollBar
  291. public bool ScrollAlwaysVisible {
  292. get { return scroll_always_visible; }
  293. set {
  294. if (scroll_always_visible == value)
  295. return;
  296. scroll_always_visible = value;
  297. UpdateShowVerticalScrollBar ();
  298. UpdateShowHorizontalScrollBar ();
  299. }
  300. }
  301. public override int SelectedIndex {
  302. get { return selected_index;}
  303. set {
  304. if (value < -1 || value >= Items.Count)
  305. throw new ArgumentOutOfRangeException ("Index of out range");
  306. if (SelectionMode == SelectionMode.None)
  307. throw new ArgumentException ("cannot call this method if SelectionMode is SelectionMode.None");
  308. if (selected_index == value)
  309. return;
  310. if (SelectionMode == SelectionMode.One)
  311. UnSelectItem (selected_index, true);
  312. SelectItem (value);
  313. selected_index = value;
  314. OnSelectedIndexChanged (new EventArgs ());
  315. }
  316. }
  317. public SelectedIndexCollection SelectedIndices {
  318. get { return selected_indices; }
  319. }
  320. public object SelectedItem {
  321. get {
  322. if (SelectedItems.Count > 0)
  323. return SelectedItems[0];
  324. else
  325. return null;
  326. }
  327. set {
  328. if (selected_item == value)
  329. return;
  330. int index = Items.IndexOf (value);
  331. if (index == -1)
  332. return;
  333. SelectedIndex = index;
  334. }
  335. }
  336. public SelectedObjectCollection SelectedItems {
  337. get {return selected_items;}
  338. }
  339. public virtual SelectionMode SelectionMode {
  340. get { return selection_mode; }
  341. set {
  342. if (!Enum.IsDefined (typeof (SelectionMode), value))
  343. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for SelectionMode", value));
  344. if (selection_mode == value)
  345. return;
  346. selection_mode = value;
  347. Refresh ();
  348. }
  349. }
  350. public bool Sorted {
  351. get { return sorted; }
  352. set {
  353. if (sorted == value)
  354. return;
  355. sorted = value;
  356. Sort ();
  357. }
  358. }
  359. public override string Text {
  360. get {
  361. if (SelectionMode != SelectionMode.None && SelectedIndex != -1)
  362. return Items[SelectedIndex].ToString ();
  363. return base.Text;
  364. }
  365. set {
  366. base.Text = value;
  367. if (SelectionMode == SelectionMode.None)
  368. return;
  369. int index;
  370. index = FindStringExact (value);
  371. if (index == -1)
  372. return;
  373. SelectedIndex = index;
  374. }
  375. }
  376. public int TopIndex {
  377. get { return top_index;}
  378. set {
  379. if (value == top_index)
  380. return;
  381. if (value < 0 || value >= Items.Count)
  382. return;
  383. value = top_index;
  384. Refresh ();
  385. }
  386. }
  387. public bool UseTabStops {
  388. get { return use_tabstops; }
  389. set {
  390. if (use_tabstops == value)
  391. return;
  392. use_tabstops = value;
  393. UpdateFormatString ();
  394. Refresh ();
  395. }
  396. }
  397. #endregion Public Properties
  398. #region Private Properties
  399. internal ListBoxInfo LBoxInfo {
  400. get { return listbox_info; }
  401. }
  402. private int ColumnWidthInternal {
  403. get { return column_width_internal; }
  404. set { column_width_internal = value; }
  405. }
  406. #endregion Private Properties
  407. #region Public Methods
  408. protected virtual void AddItemsCore (object[] value)
  409. {
  410. Items.AddRange (value);
  411. }
  412. public void BeginUpdate ()
  413. {
  414. suspend_ctrlupdate = true;
  415. }
  416. public void ClearSelected ()
  417. {
  418. foreach (int i in selected_indices) {
  419. UnSelectItem (i, false);
  420. }
  421. selected_indices.ClearIndices ();
  422. selected_items.ClearObjects ();
  423. }
  424. protected virtual ObjectCollection CreateItemCollection ()
  425. {
  426. return new ObjectCollection (this);
  427. }
  428. public void EndUpdate ()
  429. {
  430. suspend_ctrlupdate = false;
  431. UpdateItemInfo (false, -1, -1);
  432. Refresh ();
  433. }
  434. public int FindString (String s)
  435. {
  436. return FindString (s, 0);
  437. }
  438. public int FindString (string s, int startIndex)
  439. {
  440. for (int i = startIndex; i < Items.Count; i++) {
  441. if ((Items[i].ToString ()).StartsWith (s))
  442. return i;
  443. }
  444. return -1;
  445. }
  446. public int FindStringExact (string s)
  447. {
  448. return FindStringExact (s, 0);
  449. }
  450. public int FindStringExact (string s, int startIndex)
  451. {
  452. for (int i = startIndex; i < Items.Count; i++) {
  453. if ((Items[i].ToString ()).Equals (s))
  454. return i;
  455. }
  456. return -1;
  457. }
  458. public int GetItemHeight (int index)
  459. {
  460. if (index < 0 || index >= Items.Count)
  461. throw new ArgumentOutOfRangeException ("Index of out range");
  462. return ItemHeight;
  463. }
  464. public Rectangle GetItemRectangle (int index)
  465. {
  466. if (index < 0 || index >= Items.Count)
  467. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  468. Rectangle rect = new Rectangle ();
  469. if (MultiColumn == false) {
  470. rect.X = 0;
  471. rect.Y = ItemHeight * index;
  472. rect.Height = ItemHeight;
  473. rect.Width = listbox_info.textdrawing_rect.Width;
  474. }
  475. else {
  476. int which_page;
  477. if (listbox_info.page_size == 0) {
  478. listbox_info.page_size = 1;
  479. }
  480. which_page = index / listbox_info.page_size;
  481. rect.Y = (index % listbox_info.page_size) * ItemHeight;
  482. rect.X = which_page * ColumnWidthInternal;
  483. rect.Height = ItemHeight;
  484. rect.Width = ColumnWidthInternal;
  485. }
  486. return rect;
  487. }
  488. public bool GetSelected (int index)
  489. {
  490. if (index < 0 || index >= Items.Count)
  491. throw new ArgumentOutOfRangeException ("Index of out range");
  492. return (Items.GetListBoxItem (index)).Selected;
  493. }
  494. public int IndexFromPoint (Point p)
  495. {
  496. return IndexFromPoint (p.X, p.Y);
  497. }
  498. // Only returns visible points
  499. public int IndexFromPoint (int x, int y)
  500. {
  501. for (int i = LBoxInfo.top_item; i < LBoxInfo.last_item; i++) {
  502. if (GetItemRectangle (i).Contains (x,y) == true)
  503. return i;
  504. }
  505. return -1;
  506. }
  507. protected override void OnChangeUICues (UICuesEventArgs e)
  508. {
  509. base.OnChangeUICues (e);
  510. }
  511. protected override void OnDataSourceChanged (EventArgs e)
  512. {
  513. base.OnDataSourceChanged (e);
  514. }
  515. protected override void OnDisplayMemberChanged (EventArgs e)
  516. {
  517. base.OnDisplayMemberChanged (e);
  518. }
  519. protected virtual void OnDrawItem (DrawItemEventArgs e)
  520. {
  521. if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable))
  522. DrawItem (this, e);
  523. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
  524. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  525. (ThemeEngine.Current.ColorHilight), e.Bounds);
  526. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  527. ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHilightText),
  528. e.Bounds, string_format);
  529. }
  530. else {
  531. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  532. (e.BackColor), e.Bounds);
  533. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  534. ThemeEngine.Current.ResPool.GetSolidBrush (e.ForeColor),
  535. e.Bounds, string_format);
  536. }
  537. }
  538. protected override void OnFontChanged (EventArgs e)
  539. {
  540. base.OnFontChanged (e);
  541. UpdateShowHorizontalScrollBar ();
  542. UpdateShowVerticalScrollBar ();
  543. RellocateScrollBars ();
  544. CalcClientArea ();
  545. UpdateItemInfo (false, -1, -1);
  546. }
  547. protected override void OnHandleCreated (EventArgs e)
  548. {
  549. base.OnHandleCreated (e);
  550. UpdateInternalClientRect (ClientRectangle);
  551. Controls.Add (vscrollbar_ctrl);
  552. Controls.Add (hscrollbar_ctrl);
  553. if (Sorted)
  554. Sort ();
  555. }
  556. protected override void OnHandleDestroyed (EventArgs e)
  557. {
  558. base.OnHandleDestroyed (e);
  559. }
  560. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  561. {
  562. }
  563. protected override void OnParentChanged (EventArgs e)
  564. {
  565. base.OnParentChanged (e);
  566. }
  567. protected override void OnResize (EventArgs e)
  568. {
  569. base.OnResize (e);
  570. UpdateInternalClientRect (ClientRectangle);
  571. }
  572. protected override void OnSelectedIndexChanged (EventArgs e)
  573. {
  574. base.OnSelectedIndexChanged (e);
  575. if (SelectedIndexChanged != null)
  576. SelectedIndexChanged (this, e);
  577. }
  578. protected override void OnSelectedValueChanged (EventArgs e)
  579. {
  580. base.OnSelectedValueChanged (e);
  581. }
  582. public override void Refresh ()
  583. {
  584. base.Refresh ();
  585. }
  586. protected override void RefreshItem (int index)
  587. {
  588. }
  589. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  590. {
  591. base.SetBoundsCore (x, y, width, height, specified);
  592. }
  593. protected override void SetItemCore (int index, object value)
  594. {
  595. if (index < 0 || index >= Items.Count)
  596. return;
  597. Items[index] = value;
  598. }
  599. protected override void SetItemsCore (IList value)
  600. {
  601. }
  602. public void SetSelected (int index, bool value)
  603. {
  604. if (index < 0 || index >= Items.Count)
  605. throw new ArgumentOutOfRangeException ("Index of out range");
  606. if (SelectionMode == SelectionMode.None)
  607. throw new InvalidOperationException ();
  608. if (value)
  609. SelectItem (index);
  610. else
  611. UnSelectItem (index, true);
  612. }
  613. protected virtual void Sort ()
  614. {
  615. if (Items.Count == 0)
  616. return;
  617. Items.Sort ();
  618. Refresh ();
  619. }
  620. public override string ToString ()
  621. {
  622. return base.ToString () + ", Items Count: " + Items.Count;
  623. }
  624. protected virtual void WmReflectCommand (ref Message m)
  625. {
  626. }
  627. protected override void WndProc (ref Message m)
  628. {
  629. switch ((Msg) m.Msg) {
  630. case Msg.WM_PAINT: {
  631. PaintEventArgs paint_event;
  632. paint_event = XplatUI.PaintEventStart (Handle);
  633. OnPaintLB (paint_event);
  634. XplatUI.PaintEventEnd (Handle);
  635. return;
  636. }
  637. case Msg.WM_ERASEBKGND:
  638. m.Result = (IntPtr) 1;
  639. return;
  640. default:
  641. break;
  642. }
  643. base.WndProc (ref m);
  644. }
  645. #endregion Public Methods
  646. #region Private Methods
  647. internal void CalcClientArea ()
  648. {
  649. listbox_info.textdrawing_rect = listbox_info.client_rect;
  650. listbox_info.textdrawing_rect.Y += ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle);
  651. listbox_info.textdrawing_rect.X += ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle);
  652. listbox_info.textdrawing_rect.Height -= ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle);
  653. listbox_info.textdrawing_rect.Width -= ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle);
  654. if (listbox_info.show_verticalsb)
  655. listbox_info.textdrawing_rect.Width -= vscrollbar_ctrl.Width;
  656. if (listbox_info.show_horizontalsb)
  657. listbox_info.textdrawing_rect.Height -= hscrollbar_ctrl.Height;
  658. //listbox_info.page_size = listbox_info.client_rect.Height / listbox_info.item_height;
  659. listbox_info.page_size = listbox_info.textdrawing_rect.Height / listbox_info.item_height;
  660. /* Adjust size to visible the maxim number of displayable items */
  661. if (IntegralHeight == true) {
  662. // From MS Docs: The integral height is based on the height of the ListBox, rather than
  663. // the client area height. As a result, when the IntegralHeight property is set true,
  664. // items can still be partially shown if scroll bars are displayed.
  665. int remaining = (listbox_info.client_rect.Height -
  666. ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle) -
  667. ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle)) %
  668. listbox_info.item_height;
  669. if (remaining > 0) {
  670. listbox_info.client_rect.Height -= remaining;
  671. CalcClientArea ();
  672. RellocateScrollBars ();
  673. Refresh ();
  674. }
  675. }
  676. LBoxInfo.last_item = LastVisibleItem ();
  677. }
  678. internal void Draw (Rectangle clip)
  679. {
  680. if (LBoxInfo.textdrawing_rect.Contains (clip) == false) {
  681. // IntegralHeight has effect, we also have to paint the unused area
  682. if (ClientRectangle.Height > listbox_info.client_rect.Height) {
  683. Region area = new Region (ClientRectangle);
  684. area.Exclude (listbox_info.client_rect);
  685. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (Parent.BackColor),
  686. area.GetBounds (DeviceContext));
  687. }
  688. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), LBoxInfo.textdrawing_rect);
  689. }
  690. if (Items.Count > 0) {
  691. Rectangle item_rect;
  692. DrawItemState state = DrawItemState.None;
  693. for (int i = LBoxInfo.top_item; i < LBoxInfo.last_item; i++) {
  694. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  695. if (clip.IntersectsWith (item_rect) == false)
  696. continue;
  697. /* Draw item */
  698. state = DrawItemState.None;
  699. if ((Items.GetListBoxItem (i)).Selected) {
  700. state |= DrawItemState.Selected;
  701. }
  702. OnDrawItem (new DrawItemEventArgs (DeviceContext, Font, item_rect,
  703. i, state, ForeColor, BackColor));
  704. }
  705. }
  706. ThemeEngine.Current.DrawListBoxDecorations (DeviceContext, this);
  707. }
  708. // Converts a GetItemRectangle to a one that we can display
  709. internal Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  710. {
  711. Rectangle item_rect;
  712. Rectangle first_item_rect = GetItemRectangle (first_displayble);
  713. item_rect = GetItemRectangle (index);
  714. item_rect.X -= first_item_rect.X;
  715. item_rect.Y -= first_item_rect.Y;
  716. item_rect.Y += ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle);
  717. item_rect.X += ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle);
  718. item_rect.Width -= ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle);
  719. return item_rect;
  720. }
  721. // Value Changed
  722. private void HorizontalScrollEvent (object sender, EventArgs e)
  723. {
  724. LBoxInfo.top_item = listbox_info.page_size * hscrollbar_ctrl.Value;
  725. LBoxInfo.last_item = LastVisibleItem ();
  726. Refresh ();
  727. }
  728. // Only returns visible points. The diference of with IndexFromPoint is that the rectangle
  729. // has screen coordinates
  730. internal int IndexFromPointDisplayRectangle (int x, int y)
  731. {
  732. for (int i = LBoxInfo.top_item; i < LBoxInfo.last_item; i++) {
  733. if (GetItemDisplayRectangle (i, LBoxInfo.top_item).Contains (x, y) == true)
  734. return i;
  735. }
  736. return -1;
  737. }
  738. private int LastVisibleItem ()
  739. {
  740. Rectangle item_rect;
  741. int top_y = LBoxInfo.textdrawing_rect.Y + LBoxInfo.textdrawing_rect.Height;
  742. int i = 0;
  743. for (i = LBoxInfo.top_item; i < Items.Count; i++) {
  744. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  745. if (MultiColumn) {
  746. if (item_rect.X > LBoxInfo.textdrawing_rect.Width)
  747. return i - 1;
  748. }
  749. else {
  750. if (IntegralHeight) {
  751. if (item_rect.Y + item_rect.Height > top_y) {
  752. return i;
  753. }
  754. }
  755. else {
  756. if (item_rect.Y > top_y)
  757. return i;
  758. }
  759. }
  760. }
  761. return i;
  762. }
  763. internal virtual void OnMouseDownLB (object sender, MouseEventArgs e)
  764. {
  765. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  766. if (index == -1) return;
  767. switch (SelectionMode) {
  768. case SelectionMode.None: // Do nothing
  769. break;
  770. case SelectionMode.One: {
  771. SelectedIndex = index;
  772. break;
  773. }
  774. case SelectionMode.MultiSimple: {
  775. if (selected_index == -1) {
  776. SelectedIndex = index;
  777. } else {
  778. if ((Items.GetListBoxItem (index)).Selected)
  779. UnSelectItem (index, true);
  780. else {
  781. SelectItem (index);
  782. OnSelectedIndexChanged (new EventArgs ());
  783. }
  784. }
  785. break;
  786. }
  787. default:
  788. break;
  789. }
  790. }
  791. private void OnPaintLB (PaintEventArgs pevent)
  792. {
  793. if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
  794. return;
  795. /* Copies memory drawing buffer to screen*/
  796. Draw (pevent.ClipRectangle);
  797. pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  798. if (Paint != null)
  799. Paint (this, pevent);
  800. }
  801. internal void RellocateScrollBars ()
  802. {
  803. if (listbox_info.show_verticalsb) {
  804. vscrollbar_ctrl.Size = new Size (vscrollbar_ctrl.Width,
  805. listbox_info.client_rect.Height - ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle) -
  806. ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle));
  807. vscrollbar_ctrl.Location = new Point (listbox_info.client_rect.Width - vscrollbar_ctrl.Width
  808. - ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle),
  809. ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle));
  810. }
  811. if (listbox_info.show_horizontalsb) {
  812. int width;
  813. width = listbox_info.client_rect.Width - (ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle) + ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle));
  814. if (listbox_info.show_verticalsb)
  815. width -= vscrollbar_ctrl.Width;
  816. hscrollbar_ctrl.Size = new Size (width, hscrollbar_ctrl.Height);
  817. hscrollbar_ctrl.Location = new Point (ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle),
  818. listbox_info.client_rect.Height - hscrollbar_ctrl.Height
  819. - ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle));
  820. }
  821. CalcClientArea ();
  822. }
  823. // Add an item in the Selection array and marks it visually as selected
  824. private void SelectItem (int index)
  825. {
  826. if (index == -1)
  827. return;
  828. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  829. (Items.GetListBoxItem (index)).Selected = true;
  830. selected_indices.AddIndex (index);
  831. selected_items.AddObject (Items[index]);
  832. if (ClientRectangle.Contains (invalidate))
  833. Invalidate (invalidate);
  834. }
  835. // Removes an item in the Selection array and marks it visually as unselected
  836. private void UnSelectItem (int index, bool remove)
  837. {
  838. if (index == -1)
  839. return;
  840. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  841. (Items.GetListBoxItem (index)).Selected = false;
  842. if (remove) {
  843. selected_indices.RemoveIndex (index);
  844. selected_items.RemoveObject (Items[index]);
  845. }
  846. if (ClientRectangle.Contains (invalidate))
  847. Invalidate (invalidate);
  848. }
  849. private void UpdateFormatString ()
  850. {
  851. if (RightToLeft == RightToLeft.No)
  852. string_format.Alignment = StringAlignment.Near;
  853. else
  854. string_format.Alignment = StringAlignment.Far;
  855. if (UseTabStops)
  856. string_format.SetTabStops (0, new float [] {(float)(Font.Height * 3.7)});
  857. }
  858. // Updates the scrollbar's position with the new items and inside area
  859. internal virtual void UpdateItemInfo (bool adding, int first, int last)
  860. {
  861. if (!IsHandleCreated || suspend_ctrlupdate == true)
  862. return;
  863. UpdateShowVerticalScrollBar ();
  864. if (listbox_info.show_verticalsb && Items.Count > listbox_info.page_size)
  865. if (vscrollbar_ctrl.Enabled)
  866. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  867. if (listbox_info.show_horizontalsb) {
  868. if (MultiColumn) {
  869. int fullpage = (listbox_info.page_size * (listbox_info.client_rect.Width / ColumnWidthInternal));
  870. if (hscrollbar_ctrl.Enabled && listbox_info.page_size > 0)
  871. hscrollbar_ctrl.Maximum = 1 + ((Items.Count - fullpage) / listbox_info.page_size);
  872. }
  873. }
  874. if (MultiColumn == false) {
  875. /* Calc the longest items for non multicolumn listboxes */
  876. if ((first == -1 && last == -1) || (adding == false)) {
  877. SizeF size;
  878. for (int i = 0; i < Items.Count; i++) {
  879. size = DeviceContext.MeasureString (Items[i].ToString(), Font);
  880. if ((int) size.Width > listbox_info.max_itemwidth)
  881. listbox_info.max_itemwidth = (int) size.Width;
  882. }
  883. }
  884. else {
  885. if (adding) {
  886. SizeF size;
  887. for (int i = first; i < last + 1; i++) {
  888. size = DeviceContext.MeasureString (Items[i].ToString(), Font);
  889. if ((int) size.Width > listbox_info.max_itemwidth)
  890. listbox_info.max_itemwidth = (int) size.Width;
  891. }
  892. }
  893. }
  894. }
  895. if (sorted)
  896. Sort ();
  897. SelectedItems.ReCreate ();
  898. SelectedIndices.ReCreate ();
  899. UpdateShowHorizontalScrollBar ();
  900. Refresh ();
  901. }
  902. private void UpdateInternalClientRect (Rectangle client_rectangle)
  903. {
  904. listbox_info.client_rect = client_rectangle;
  905. UpdateShowHorizontalScrollBar ();
  906. UpdateShowVerticalScrollBar ();
  907. RellocateScrollBars ();
  908. UpdateItemInfo (false, -1, -1);
  909. }
  910. /* Determines if the horizontal scrollbar has to be displyed */
  911. private void UpdateShowHorizontalScrollBar ()
  912. {
  913. bool show = false;
  914. bool enabled = true;
  915. if (MultiColumn) { /* Horizontal scrollbar is always shown in Multicolum mode */
  916. /* Is it really need it */
  917. int page_size = listbox_info.client_rect.Height / listbox_info.item_height;
  918. int fullpage = (page_size * (listbox_info.textdrawing_rect.Height / ColumnWidthInternal));
  919. if (Items.Count > fullpage) {
  920. if (IntegralHeight == false)
  921. show = true;
  922. }
  923. else { /* Acording to MS Documentation ScrollAlwaysVisible only affects Horizontal scrollbars but
  924. this is not true for MultiColumn listboxes */
  925. if (ScrollAlwaysVisible == true) {
  926. enabled = false;
  927. show = true;
  928. }
  929. }
  930. } else { /* If large item*/
  931. if (listbox_info.max_itemwidth > listbox_info.client_rect.Width && HorizontalScrollbar) {
  932. show = true;
  933. hscrollbar_ctrl.Maximum = listbox_info.max_itemwidth;
  934. }
  935. }
  936. if (hscrollbar_ctrl.Enabled != enabled)
  937. hscrollbar_ctrl.Enabled = enabled;
  938. if (listbox_info.show_horizontalsb == show)
  939. return;
  940. listbox_info.show_horizontalsb = show;
  941. hscrollbar_ctrl.Visible = show;
  942. if (show == true) {
  943. RellocateScrollBars ();
  944. }
  945. CalcClientArea ();
  946. }
  947. /* Determines if the vertical scrollbar has to be displyed */
  948. private void UpdateShowVerticalScrollBar ()
  949. {
  950. bool show = false;
  951. bool enabled = true;
  952. if (!MultiColumn) { /* Vertical scrollbar is never shown in Multicolum mode */
  953. if (Items.Count > listbox_info.page_size) {
  954. show = true;
  955. }
  956. else
  957. if (ScrollAlwaysVisible) {
  958. show = true;
  959. enabled = false;
  960. }
  961. }
  962. if (vscrollbar_ctrl.Enabled != enabled)
  963. vscrollbar_ctrl.Enabled = enabled;
  964. if (listbox_info.show_verticalsb == show)
  965. return;
  966. listbox_info.show_verticalsb = show;
  967. vscrollbar_ctrl.Visible = show;
  968. if (show == true) {
  969. if (vscrollbar_ctrl.Enabled)
  970. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  971. RellocateScrollBars ();
  972. }
  973. CalcClientArea ();
  974. }
  975. // Value Changed
  976. private void VerticalScrollEvent (object sender, EventArgs e)
  977. {
  978. LBoxInfo.top_item = /*listbox_info.page_size + */ vscrollbar_ctrl.Value;
  979. LBoxInfo.last_item = LastVisibleItem ();
  980. Refresh ();
  981. }
  982. #endregion Private Methods
  983. /*
  984. ListBox.ObjectCollection
  985. */
  986. public class ObjectCollection : IList, ICollection, IEnumerable
  987. {
  988. // Compare objects
  989. internal class ListObjectComparer : IComparer
  990. {
  991. private ListBox owner;
  992. public ListObjectComparer (ListBox owner)
  993. {
  994. this.owner = owner;
  995. }
  996. public int Compare (object a, object b)
  997. {
  998. string str1 = a.ToString ();
  999. string str2 = b.ToString ();
  1000. return str1.CompareTo (str2);
  1001. }
  1002. }
  1003. // Compare ListItem
  1004. internal class ListItemComparer : IComparer
  1005. {
  1006. private ListBox owner;
  1007. public ListItemComparer (ListBox owner)
  1008. {
  1009. this.owner = owner;
  1010. }
  1011. public int Compare (object a, object b)
  1012. {
  1013. int index1 = ((ListBox.ListBoxItem) (a)).Index;
  1014. int index2 = ((ListBox.ListBoxItem) (b)).Index;
  1015. string str1 = owner.Items[index1].ToString ();
  1016. string str2 = owner.Items[index2].ToString ();
  1017. return str1.CompareTo (str2);
  1018. }
  1019. }
  1020. private ListBox owner;
  1021. internal ArrayList object_items = new ArrayList ();
  1022. internal ArrayList listbox_items = new ArrayList ();
  1023. public ObjectCollection (ListBox owner)
  1024. {
  1025. this.owner = owner;
  1026. }
  1027. public ObjectCollection (ListBox owner, object[] obj)
  1028. {
  1029. this.owner = owner;
  1030. AddRange (obj);
  1031. }
  1032. public ObjectCollection (ListBox owner, ObjectCollection obj)
  1033. {
  1034. this.owner = owner;
  1035. AddRange (obj);
  1036. }
  1037. #region Public Properties
  1038. public virtual int Count {
  1039. get { return object_items.Count; }
  1040. }
  1041. public virtual bool IsReadOnly {
  1042. get { return false; }
  1043. }
  1044. public virtual object this [int index] {
  1045. get {
  1046. if (index < 0 || index >= Count)
  1047. throw new ArgumentOutOfRangeException ("Index of out range");
  1048. return object_items[index];
  1049. }
  1050. set {
  1051. if (index < 0 || index >= Count)
  1052. throw new ArgumentOutOfRangeException ("Index of out range");
  1053. object_items[index] = value;
  1054. }
  1055. }
  1056. bool ICollection.IsSynchronized {
  1057. get { return false; }
  1058. }
  1059. object ICollection.SyncRoot {
  1060. get { return this; }
  1061. }
  1062. bool IList.IsFixedSize {
  1063. get { return false; }
  1064. }
  1065. #endregion Public Properties
  1066. #region Public Methods
  1067. public int Add (object item)
  1068. {
  1069. int idx;
  1070. idx = AddItem (item);
  1071. owner.UpdateItemInfo (true, idx, idx);
  1072. return idx;
  1073. }
  1074. public void AddRange (object[] items)
  1075. {
  1076. int cnt = Count;
  1077. foreach (object mi in items)
  1078. AddItem (mi);
  1079. owner.UpdateItemInfo (true, cnt, Count);
  1080. }
  1081. public void AddRange (ObjectCollection col)
  1082. {
  1083. int cnt = Count;
  1084. foreach (object mi in col)
  1085. AddItem (mi);
  1086. owner.UpdateItemInfo (true, cnt, Count);
  1087. }
  1088. public virtual void Clear ()
  1089. {
  1090. object_items.Clear ();
  1091. listbox_items.Clear ();
  1092. owner.UpdateItemInfo (false, -1, -1);
  1093. }
  1094. public virtual bool Contains (object obj)
  1095. {
  1096. return object_items.Contains (obj);
  1097. }
  1098. public void CopyTo (object[] dest, int arrayIndex)
  1099. {
  1100. object_items.CopyTo (dest, arrayIndex);
  1101. }
  1102. void ICollection.CopyTo (Array dest, int index)
  1103. {
  1104. object_items.CopyTo (dest, index);
  1105. }
  1106. public virtual IEnumerator GetEnumerator ()
  1107. {
  1108. return object_items.GetEnumerator ();
  1109. }
  1110. int IList.Add (object item)
  1111. {
  1112. return Add (item);
  1113. }
  1114. public virtual int IndexOf (object value)
  1115. {
  1116. return object_items.IndexOf (value);
  1117. }
  1118. public virtual void Insert (int index, object item)
  1119. {
  1120. throw new NotImplementedException ();
  1121. }
  1122. public virtual void Remove (object value)
  1123. {
  1124. RemoveAt (IndexOf (value));
  1125. owner.UpdateItemInfo (false, -1, -1);
  1126. }
  1127. public virtual void RemoveAt (int index)
  1128. {
  1129. if (index < 0 || index >= Count)
  1130. throw new ArgumentOutOfRangeException ("Index of out range");
  1131. object_items.RemoveAt (index);
  1132. listbox_items.RemoveAt (index);
  1133. owner.UpdateItemInfo (false, -1, -1);
  1134. }
  1135. #endregion Public Methods
  1136. #region Private Methods
  1137. private int AddItem (object item)
  1138. {
  1139. int cnt = object_items.Count;
  1140. object_items.Add (item);
  1141. listbox_items.Add (new ListBox.ListBoxItem (cnt));
  1142. return cnt;
  1143. }
  1144. internal ListBox.ListBoxItem GetListBoxItem (int index)
  1145. {
  1146. if (index < 0 || index >= Count)
  1147. throw new ArgumentOutOfRangeException ("Index of out range");
  1148. return (ListBox.ListBoxItem) listbox_items[index];
  1149. }
  1150. internal void SetListBoxItem (ListBox.ListBoxItem item, int index)
  1151. {
  1152. if (index < 0 || index >= Count)
  1153. throw new ArgumentOutOfRangeException ("Index of out range");
  1154. listbox_items[index] = item;
  1155. }
  1156. internal void Sort ()
  1157. {
  1158. /* Keep this order */
  1159. listbox_items.Sort (new ListItemComparer (owner));
  1160. object_items.Sort (new ListObjectComparer (owner));
  1161. for (int i = 0; i < listbox_items.Count; i++) {
  1162. ListBox.ListBoxItem item = GetListBoxItem (i);
  1163. item.Index = i;
  1164. }
  1165. }
  1166. #endregion Private Methods
  1167. }
  1168. /*
  1169. ListBox.SelectedIndexCollection
  1170. */
  1171. public class SelectedIndexCollection : IList, ICollection, IEnumerable
  1172. {
  1173. private ListBox owner;
  1174. private ArrayList indices = new ArrayList ();
  1175. public SelectedIndexCollection (ListBox owner)
  1176. {
  1177. this.owner = owner;
  1178. }
  1179. #region Public Properties
  1180. public virtual int Count {
  1181. get { return indices.Count; }
  1182. }
  1183. public virtual bool IsReadOnly {
  1184. get { return true; }
  1185. }
  1186. public int this [int index] {
  1187. get {
  1188. if (index < 0 || index >= Count)
  1189. throw new ArgumentOutOfRangeException ("Index of out range");
  1190. return (int) indices[index];
  1191. }
  1192. }
  1193. bool ICollection.IsSynchronized {
  1194. get { return true; }
  1195. }
  1196. bool IList.IsFixedSize{
  1197. get { return true; }
  1198. }
  1199. object ICollection.SyncRoot {
  1200. get { return this; }
  1201. }
  1202. #endregion Public Properties
  1203. #region Public Methods
  1204. public bool Contains (int selectedIndex)
  1205. {
  1206. return indices.Contains (selectedIndex);
  1207. }
  1208. public virtual void CopyTo (Array dest, int index)
  1209. {
  1210. indices.CopyTo (dest, index);
  1211. }
  1212. public virtual IEnumerator GetEnumerator ()
  1213. {
  1214. return indices.GetEnumerator ();
  1215. }
  1216. int IList.Add (object obj)
  1217. {
  1218. throw new NotSupportedException ();
  1219. }
  1220. void IList.Clear ()
  1221. {
  1222. throw new NotSupportedException ();
  1223. }
  1224. bool IList.Contains (object selectedIndex)
  1225. {
  1226. return Contains ((int)selectedIndex);
  1227. }
  1228. int IList.IndexOf (object selectedIndex)
  1229. {
  1230. return IndexOf ((int) selectedIndex);
  1231. }
  1232. void IList.Insert (int index, object value)
  1233. {
  1234. throw new NotSupportedException ();
  1235. }
  1236. void IList.Remove (object value)
  1237. {
  1238. throw new NotSupportedException ();
  1239. }
  1240. void IList.RemoveAt (int index)
  1241. {
  1242. throw new NotSupportedException ();
  1243. }
  1244. object IList.this[int index]{
  1245. get {return indices[index]; }
  1246. set {throw new NotImplementedException (); }
  1247. }
  1248. public int IndexOf (int selectedIndex)
  1249. {
  1250. return indices.IndexOf (selectedIndex);
  1251. }
  1252. #endregion Public Methods
  1253. #region Private Methods
  1254. internal void AddIndex (int index)
  1255. {
  1256. indices.Add (index);
  1257. }
  1258. internal void ClearIndices ()
  1259. {
  1260. indices.Clear ();
  1261. }
  1262. internal void RemoveIndex (int index)
  1263. {
  1264. indices.Remove (index);
  1265. }
  1266. internal void ReCreate ()
  1267. {
  1268. indices.Clear ();
  1269. for (int i = 0; i < owner.Items.Count; i++) {
  1270. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1271. if (item.Selected)
  1272. indices.Add (item.Index);
  1273. }
  1274. }
  1275. #endregion Private Methods
  1276. }
  1277. /*
  1278. SelectedObjectCollection
  1279. */
  1280. public class SelectedObjectCollection : IList, ICollection, IEnumerable
  1281. {
  1282. private ListBox owner;
  1283. private ArrayList object_items = new ArrayList ();
  1284. public SelectedObjectCollection (ListBox owner)
  1285. {
  1286. this.owner = owner;
  1287. }
  1288. #region Public Properties
  1289. public virtual int Count {
  1290. get { return object_items.Count; }
  1291. }
  1292. public virtual bool IsReadOnly {
  1293. get { return true; }
  1294. }
  1295. public virtual object this [int index] {
  1296. get {
  1297. if (index < 0 || index >= Count)
  1298. throw new ArgumentOutOfRangeException ("Index of out range");
  1299. return object_items[index];
  1300. }
  1301. set {throw new NotSupportedException ();}
  1302. }
  1303. bool ICollection.IsSynchronized {
  1304. get { return true; }
  1305. }
  1306. object ICollection.SyncRoot {
  1307. get { return this; }
  1308. }
  1309. bool IList.IsFixedSize {
  1310. get { return true; }
  1311. }
  1312. object IList.this[int index] {
  1313. get { return object_items[index]; }
  1314. set { throw new NotSupportedException (); }
  1315. }
  1316. #endregion Public Properties
  1317. #region Public Methods
  1318. public virtual bool Contains (object selectedObject)
  1319. {
  1320. return object_items.Contains (selectedObject);
  1321. }
  1322. public virtual void CopyTo (Array dest, int index)
  1323. {
  1324. object_items.CopyTo (dest, index);
  1325. }
  1326. int IList.Add (object value)
  1327. {
  1328. throw new NotSupportedException ();
  1329. }
  1330. void IList.Clear ()
  1331. {
  1332. throw new NotSupportedException ();
  1333. }
  1334. bool IList.Contains (object selectedIndex)
  1335. {
  1336. throw new NotImplementedException ();
  1337. }
  1338. int IList.IndexOf (object selectedIndex)
  1339. {
  1340. return IndexOf ((int) selectedIndex);
  1341. }
  1342. void IList.Insert (int index, object value)
  1343. {
  1344. throw new NotSupportedException ();
  1345. }
  1346. void IList.Remove (object value)
  1347. {
  1348. throw new NotSupportedException ();
  1349. }
  1350. void IList.RemoveAt (int index)
  1351. {
  1352. throw new NotSupportedException ();
  1353. }
  1354. public int IndexOf (int selectedIndex)
  1355. {
  1356. return object_items.IndexOf (selectedIndex);
  1357. }
  1358. public virtual IEnumerator GetEnumerator ()
  1359. {
  1360. return object_items.GetEnumerator ();
  1361. }
  1362. #endregion Public Methods
  1363. #region Private Methods
  1364. internal void AddObject (object obj)
  1365. {
  1366. object_items.Add (obj);
  1367. }
  1368. internal void ClearObjects ()
  1369. {
  1370. object_items.Clear ();
  1371. }
  1372. internal void ReCreate ()
  1373. {
  1374. object_items.Clear ();
  1375. for (int i = 0; i < owner.Items.Count; i++) {
  1376. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1377. if (item.Selected)
  1378. object_items.Add (owner.Items[item.Index]);
  1379. }
  1380. }
  1381. internal void RemoveObject (object obj)
  1382. {
  1383. object_items.Remove (obj);
  1384. }
  1385. #endregion Private Methods
  1386. }
  1387. }
  1388. }