ListBox.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  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. return;
  524. }
  525. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
  526. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  527. (ThemeEngine.Current.ColorHilight), e.Bounds);
  528. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  529. ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHilightText),
  530. e.Bounds, string_format);
  531. }
  532. else {
  533. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  534. (e.BackColor), e.Bounds);
  535. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  536. ThemeEngine.Current.ResPool.GetSolidBrush (e.ForeColor),
  537. e.Bounds, string_format);
  538. }
  539. }
  540. protected override void OnFontChanged (EventArgs e)
  541. {
  542. base.OnFontChanged (e);
  543. UpdateShowHorizontalScrollBar ();
  544. UpdateShowVerticalScrollBar ();
  545. RellocateScrollBars ();
  546. CalcClientArea ();
  547. UpdateItemInfo (false, -1, -1);
  548. }
  549. protected override void OnHandleCreated (EventArgs e)
  550. {
  551. base.OnHandleCreated (e);
  552. UpdateInternalClientRect (ClientRectangle);
  553. Controls.Add (vscrollbar_ctrl);
  554. Controls.Add (hscrollbar_ctrl);
  555. if (Sorted)
  556. Sort ();
  557. }
  558. protected override void OnHandleDestroyed (EventArgs e)
  559. {
  560. base.OnHandleDestroyed (e);
  561. }
  562. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  563. {
  564. }
  565. protected override void OnParentChanged (EventArgs e)
  566. {
  567. base.OnParentChanged (e);
  568. }
  569. protected override void OnResize (EventArgs e)
  570. {
  571. base.OnResize (e);
  572. UpdateInternalClientRect (ClientRectangle);
  573. }
  574. protected override void OnSelectedIndexChanged (EventArgs e)
  575. {
  576. base.OnSelectedIndexChanged (e);
  577. if (SelectedIndexChanged != null)
  578. SelectedIndexChanged (this, e);
  579. }
  580. protected override void OnSelectedValueChanged (EventArgs e)
  581. {
  582. base.OnSelectedValueChanged (e);
  583. }
  584. public override void Refresh ()
  585. {
  586. base.Refresh ();
  587. }
  588. protected override void RefreshItem (int index)
  589. {
  590. }
  591. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  592. {
  593. base.SetBoundsCore (x, y, width, height, specified);
  594. }
  595. protected override void SetItemCore (int index, object value)
  596. {
  597. if (index < 0 || index >= Items.Count)
  598. return;
  599. Items[index] = value;
  600. }
  601. protected override void SetItemsCore (IList value)
  602. {
  603. }
  604. public void SetSelected (int index, bool value)
  605. {
  606. if (index < 0 || index >= Items.Count)
  607. throw new ArgumentOutOfRangeException ("Index of out range");
  608. if (SelectionMode == SelectionMode.None)
  609. throw new InvalidOperationException ();
  610. if (value)
  611. SelectItem (index);
  612. else
  613. UnSelectItem (index, true);
  614. }
  615. protected virtual void Sort ()
  616. {
  617. if (Items.Count == 0)
  618. return;
  619. Items.Sort ();
  620. Refresh ();
  621. }
  622. public override string ToString ()
  623. {
  624. return base.ToString () + ", Items Count: " + Items.Count;
  625. }
  626. protected virtual void WmReflectCommand (ref Message m)
  627. {
  628. }
  629. protected override void WndProc (ref Message m)
  630. {
  631. switch ((Msg) m.Msg) {
  632. case Msg.WM_PAINT: {
  633. PaintEventArgs paint_event;
  634. paint_event = XplatUI.PaintEventStart (Handle);
  635. OnPaintLB (paint_event);
  636. XplatUI.PaintEventEnd (Handle);
  637. return;
  638. }
  639. case Msg.WM_ERASEBKGND:
  640. m.Result = (IntPtr) 1;
  641. return;
  642. default:
  643. break;
  644. }
  645. base.WndProc (ref m);
  646. }
  647. #endregion Public Methods
  648. #region Private Methods
  649. internal void CalcClientArea ()
  650. {
  651. listbox_info.textdrawing_rect = listbox_info.client_rect;
  652. listbox_info.textdrawing_rect.Y += ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle);
  653. listbox_info.textdrawing_rect.X += ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle);
  654. //BUG: Top and Left decorations
  655. listbox_info.textdrawing_rect.Height -= ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle);
  656. listbox_info.textdrawing_rect.Width -= ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle);
  657. if (listbox_info.show_verticalsb)
  658. listbox_info.textdrawing_rect.Width -= vscrollbar_ctrl.Width;
  659. if (listbox_info.show_horizontalsb)
  660. listbox_info.textdrawing_rect.Height -= hscrollbar_ctrl.Height;
  661. //listbox_info.page_size = listbox_info.client_rect.Height / listbox_info.item_height;
  662. listbox_info.page_size = listbox_info.textdrawing_rect.Height / listbox_info.item_height;
  663. /* Adjust size to visible the maxim number of displayable items */
  664. if (IntegralHeight == true) {
  665. // From MS Docs: The integral height is based on the height of the ListBox, rather than
  666. // the client area height. As a result, when the IntegralHeight property is set true,
  667. // items can still be partially shown if scroll bars are displayed.
  668. int remaining = (listbox_info.client_rect.Height -
  669. ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle) -
  670. ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle)) %
  671. listbox_info.item_height;
  672. if (remaining > 0) {
  673. listbox_info.client_rect.Height -= remaining;
  674. CalcClientArea ();
  675. RellocateScrollBars ();
  676. Refresh ();
  677. }
  678. }
  679. LBoxInfo.last_item = LastVisibleItem ();
  680. }
  681. internal void Draw (Rectangle clip)
  682. {
  683. if (LBoxInfo.textdrawing_rect.Contains (clip) == false) {
  684. // IntegralHeight has effect, we also have to paint the unused area
  685. if (ClientRectangle.Height > listbox_info.client_rect.Height) {
  686. Region area = new Region (ClientRectangle);
  687. area.Exclude (listbox_info.client_rect);
  688. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (Parent.BackColor),
  689. area.GetBounds (DeviceContext));
  690. }
  691. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), LBoxInfo.textdrawing_rect);
  692. }
  693. if (Items.Count > 0) {
  694. Rectangle item_rect;
  695. DrawItemState state = DrawItemState.None;
  696. for (int i = LBoxInfo.top_item; i < LBoxInfo.last_item; i++) {
  697. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  698. if (clip.IntersectsWith (item_rect) == false)
  699. continue;
  700. /* Draw item */
  701. state = DrawItemState.None;
  702. if ((Items.GetListBoxItem (i)).Selected) {
  703. state |= DrawItemState.Selected;
  704. }
  705. OnDrawItem (new DrawItemEventArgs (DeviceContext, Font, item_rect,
  706. i, state, ForeColor, BackColor));
  707. }
  708. }
  709. ThemeEngine.Current.DrawListBoxDecorations (DeviceContext, this);
  710. }
  711. // Converts a GetItemRectangle to a one that we can display
  712. internal Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  713. {
  714. Rectangle item_rect;
  715. Rectangle first_item_rect = GetItemRectangle (first_displayble);
  716. item_rect = GetItemRectangle (index);
  717. item_rect.X -= first_item_rect.X;
  718. item_rect.Y -= first_item_rect.Y;
  719. item_rect.Y += ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle);
  720. item_rect.X += ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle);
  721. item_rect.Width -= ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle);
  722. return item_rect;
  723. }
  724. // Value Changed
  725. private void HorizontalScrollEvent (object sender, EventArgs e)
  726. {
  727. LBoxInfo.top_item = listbox_info.page_size * hscrollbar_ctrl.Value;
  728. LBoxInfo.last_item = LastVisibleItem ();
  729. Refresh ();
  730. }
  731. // Only returns visible points. The diference of with IndexFromPoint is that the rectangle
  732. // has screen coordinates
  733. internal int IndexFromPointDisplayRectangle (int x, int y)
  734. {
  735. for (int i = LBoxInfo.top_item; i < LBoxInfo.last_item; i++) {
  736. if (GetItemDisplayRectangle (i, LBoxInfo.top_item).Contains (x, y) == true)
  737. return i;
  738. }
  739. return -1;
  740. }
  741. private int LastVisibleItem ()
  742. {
  743. Rectangle item_rect;
  744. int top_y = LBoxInfo.textdrawing_rect.Y + LBoxInfo.textdrawing_rect.Height;
  745. int i = 0;
  746. for (i = LBoxInfo.top_item; i < Items.Count; i++) {
  747. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  748. if (MultiColumn) {
  749. if (item_rect.X > LBoxInfo.textdrawing_rect.Width)
  750. return i - 1;
  751. }
  752. else {
  753. if (IntegralHeight) {
  754. if (item_rect.Y + item_rect.Height > top_y) {
  755. return i;
  756. }
  757. }
  758. else {
  759. if (item_rect.Y > top_y)
  760. return i;
  761. }
  762. }
  763. }
  764. return i;
  765. }
  766. internal virtual void OnMouseDownLB (object sender, MouseEventArgs e)
  767. {
  768. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  769. if (index == -1) return;
  770. switch (SelectionMode) {
  771. case SelectionMode.None: // Do nothing
  772. break;
  773. case SelectionMode.One: {
  774. SelectedIndex = index;
  775. break;
  776. }
  777. case SelectionMode.MultiSimple: {
  778. if (selected_index == -1) {
  779. SelectedIndex = index;
  780. } else {
  781. if ((Items.GetListBoxItem (index)).Selected)
  782. UnSelectItem (index, true);
  783. else {
  784. SelectItem (index);
  785. OnSelectedIndexChanged (new EventArgs ());
  786. }
  787. }
  788. break;
  789. }
  790. default:
  791. break;
  792. }
  793. }
  794. private void OnPaintLB (PaintEventArgs pevent)
  795. {
  796. if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
  797. return;
  798. /* Copies memory drawing buffer to screen*/
  799. Draw (pevent.ClipRectangle);
  800. pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  801. if (Paint != null)
  802. Paint (this, pevent);
  803. }
  804. internal void RellocateScrollBars ()
  805. {
  806. if (listbox_info.show_verticalsb) {
  807. vscrollbar_ctrl.Size = new Size (vscrollbar_ctrl.Width,
  808. listbox_info.client_rect.Height - ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle) -
  809. ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle));
  810. vscrollbar_ctrl.Location = new Point (listbox_info.client_rect.Width - vscrollbar_ctrl.Width
  811. - ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle),
  812. ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle));
  813. }
  814. if (listbox_info.show_horizontalsb) {
  815. int width;
  816. width = listbox_info.client_rect.Width - (ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle) + ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle));
  817. if (listbox_info.show_verticalsb)
  818. width -= vscrollbar_ctrl.Width;
  819. hscrollbar_ctrl.Size = new Size (width, hscrollbar_ctrl.Height);
  820. hscrollbar_ctrl.Location = new Point (ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle),
  821. listbox_info.client_rect.Height - hscrollbar_ctrl.Height
  822. - ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle));
  823. }
  824. CalcClientArea ();
  825. }
  826. // Add an item in the Selection array and marks it visually as selected
  827. private void SelectItem (int index)
  828. {
  829. if (index == -1)
  830. return;
  831. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  832. (Items.GetListBoxItem (index)).Selected = true;
  833. selected_indices.AddIndex (index);
  834. selected_items.AddObject (Items[index]);
  835. if (ClientRectangle.Contains (invalidate))
  836. Invalidate (invalidate);
  837. }
  838. // Removes an item in the Selection array and marks it visually as unselected
  839. private void UnSelectItem (int index, bool remove)
  840. {
  841. if (index == -1)
  842. return;
  843. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  844. (Items.GetListBoxItem (index)).Selected = false;
  845. if (remove) {
  846. selected_indices.RemoveIndex (index);
  847. selected_items.RemoveObject (Items[index]);
  848. }
  849. if (ClientRectangle.Contains (invalidate))
  850. Invalidate (invalidate);
  851. }
  852. private void UpdateFormatString ()
  853. {
  854. if (RightToLeft == RightToLeft.No)
  855. string_format.Alignment = StringAlignment.Near;
  856. else
  857. string_format.Alignment = StringAlignment.Far;
  858. if (UseTabStops)
  859. string_format.SetTabStops (0, new float [] {(float)(Font.Height * 3.7)});
  860. }
  861. // Updates the scrollbar's position with the new items and inside area
  862. internal virtual void UpdateItemInfo (bool adding, int first, int last)
  863. {
  864. if (!IsHandleCreated || suspend_ctrlupdate == true)
  865. return;
  866. UpdateShowVerticalScrollBar ();
  867. if (listbox_info.show_verticalsb && Items.Count > listbox_info.page_size)
  868. if (vscrollbar_ctrl.Enabled)
  869. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  870. if (listbox_info.show_horizontalsb) {
  871. if (MultiColumn) {
  872. int fullpage = (listbox_info.page_size * (listbox_info.client_rect.Width / ColumnWidthInternal));
  873. if (hscrollbar_ctrl.Enabled && listbox_info.page_size > 0)
  874. hscrollbar_ctrl.Maximum = Math.Max (0, 1 + ((Items.Count - fullpage) / listbox_info.page_size));
  875. }
  876. }
  877. if (MultiColumn == false) {
  878. /* Calc the longest items for non multicolumn listboxes */
  879. if ((first == -1 && last == -1) || (adding == false)) {
  880. SizeF size;
  881. for (int i = 0; i < Items.Count; i++) {
  882. size = DeviceContext.MeasureString (Items[i].ToString(), Font);
  883. if ((int) size.Width > listbox_info.max_itemwidth)
  884. listbox_info.max_itemwidth = (int) size.Width;
  885. }
  886. }
  887. else {
  888. if (adding) {
  889. SizeF size;
  890. for (int i = first; i < last + 1; i++) {
  891. size = DeviceContext.MeasureString (Items[i].ToString(), Font);
  892. if ((int) size.Width > listbox_info.max_itemwidth)
  893. listbox_info.max_itemwidth = (int) size.Width;
  894. }
  895. }
  896. }
  897. }
  898. if (sorted)
  899. Sort ();
  900. SelectedItems.ReCreate ();
  901. SelectedIndices.ReCreate ();
  902. UpdateShowHorizontalScrollBar ();
  903. Refresh ();
  904. }
  905. private void UpdateInternalClientRect (Rectangle client_rectangle)
  906. {
  907. listbox_info.client_rect = client_rectangle;
  908. UpdateShowHorizontalScrollBar ();
  909. UpdateShowVerticalScrollBar ();
  910. RellocateScrollBars ();
  911. UpdateItemInfo (false, -1, -1);
  912. }
  913. /* Determines if the horizontal scrollbar has to be displyed */
  914. private void UpdateShowHorizontalScrollBar ()
  915. {
  916. bool show = false;
  917. bool enabled = true;
  918. if (MultiColumn) { /* Horizontal scrollbar is always shown in Multicolum mode */
  919. /* Is it really need it */
  920. int page_size = listbox_info.client_rect.Height / listbox_info.item_height;
  921. int fullpage = (page_size * (listbox_info.textdrawing_rect.Height / ColumnWidthInternal));
  922. if (Items.Count > fullpage) {
  923. if (IntegralHeight == false)
  924. show = true;
  925. }
  926. else { /* Acording to MS Documentation ScrollAlwaysVisible only affects Horizontal scrollbars but
  927. this is not true for MultiColumn listboxes */
  928. if (ScrollAlwaysVisible == true) {
  929. enabled = false;
  930. show = true;
  931. }
  932. }
  933. } else { /* If large item*/
  934. if (listbox_info.max_itemwidth > listbox_info.client_rect.Width && HorizontalScrollbar) {
  935. show = true;
  936. hscrollbar_ctrl.Maximum = listbox_info.max_itemwidth;
  937. }
  938. }
  939. if (hscrollbar_ctrl.Enabled != enabled)
  940. hscrollbar_ctrl.Enabled = enabled;
  941. if (listbox_info.show_horizontalsb == show)
  942. return;
  943. listbox_info.show_horizontalsb = show;
  944. hscrollbar_ctrl.Visible = show;
  945. if (show == true) {
  946. RellocateScrollBars ();
  947. }
  948. CalcClientArea ();
  949. }
  950. /* Determines if the vertical scrollbar has to be displyed */
  951. private void UpdateShowVerticalScrollBar ()
  952. {
  953. bool show = false;
  954. bool enabled = true;
  955. if (!MultiColumn) { /* Vertical scrollbar is never shown in Multicolum mode */
  956. if (Items.Count > listbox_info.page_size) {
  957. show = true;
  958. }
  959. else
  960. if (ScrollAlwaysVisible) {
  961. show = true;
  962. enabled = false;
  963. }
  964. }
  965. if (vscrollbar_ctrl.Enabled != enabled)
  966. vscrollbar_ctrl.Enabled = enabled;
  967. if (listbox_info.show_verticalsb == show)
  968. return;
  969. listbox_info.show_verticalsb = show;
  970. vscrollbar_ctrl.Visible = show;
  971. if (show == true) {
  972. if (vscrollbar_ctrl.Enabled)
  973. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  974. RellocateScrollBars ();
  975. }
  976. CalcClientArea ();
  977. }
  978. // Value Changed
  979. private void VerticalScrollEvent (object sender, EventArgs e)
  980. {
  981. LBoxInfo.top_item = /*listbox_info.page_size + */ vscrollbar_ctrl.Value;
  982. LBoxInfo.last_item = LastVisibleItem ();
  983. Refresh ();
  984. }
  985. #endregion Private Methods
  986. /*
  987. ListBox.ObjectCollection
  988. */
  989. public class ObjectCollection : IList, ICollection, IEnumerable
  990. {
  991. // Compare objects
  992. internal class ListObjectComparer : IComparer
  993. {
  994. private ListBox owner;
  995. public ListObjectComparer (ListBox owner)
  996. {
  997. this.owner = owner;
  998. }
  999. public int Compare (object a, object b)
  1000. {
  1001. string str1 = a.ToString ();
  1002. string str2 = b.ToString ();
  1003. return str1.CompareTo (str2);
  1004. }
  1005. }
  1006. // Compare ListItem
  1007. internal class ListItemComparer : IComparer
  1008. {
  1009. private ListBox owner;
  1010. public ListItemComparer (ListBox owner)
  1011. {
  1012. this.owner = owner;
  1013. }
  1014. public int Compare (object a, object b)
  1015. {
  1016. int index1 = ((ListBox.ListBoxItem) (a)).Index;
  1017. int index2 = ((ListBox.ListBoxItem) (b)).Index;
  1018. string str1 = owner.Items[index1].ToString ();
  1019. string str2 = owner.Items[index2].ToString ();
  1020. return str1.CompareTo (str2);
  1021. }
  1022. }
  1023. private ListBox owner;
  1024. internal ArrayList object_items = new ArrayList ();
  1025. internal ArrayList listbox_items = new ArrayList ();
  1026. public ObjectCollection (ListBox owner)
  1027. {
  1028. this.owner = owner;
  1029. }
  1030. public ObjectCollection (ListBox owner, object[] obj)
  1031. {
  1032. this.owner = owner;
  1033. AddRange (obj);
  1034. }
  1035. public ObjectCollection (ListBox owner, ObjectCollection obj)
  1036. {
  1037. this.owner = owner;
  1038. AddRange (obj);
  1039. }
  1040. #region Public Properties
  1041. public virtual int Count {
  1042. get { return object_items.Count; }
  1043. }
  1044. public virtual bool IsReadOnly {
  1045. get { return false; }
  1046. }
  1047. public virtual object this [int index] {
  1048. get {
  1049. if (index < 0 || index >= Count)
  1050. throw new ArgumentOutOfRangeException ("Index of out range");
  1051. return object_items[index];
  1052. }
  1053. set {
  1054. if (index < 0 || index >= Count)
  1055. throw new ArgumentOutOfRangeException ("Index of out range");
  1056. object_items[index] = value;
  1057. }
  1058. }
  1059. bool ICollection.IsSynchronized {
  1060. get { return false; }
  1061. }
  1062. object ICollection.SyncRoot {
  1063. get { return this; }
  1064. }
  1065. bool IList.IsFixedSize {
  1066. get { return false; }
  1067. }
  1068. #endregion Public Properties
  1069. #region Public Methods
  1070. public int Add (object item)
  1071. {
  1072. int idx;
  1073. idx = AddItem (item);
  1074. owner.UpdateItemInfo (true, idx, idx);
  1075. return idx;
  1076. }
  1077. public void AddRange (object[] items)
  1078. {
  1079. int cnt = Count;
  1080. foreach (object mi in items)
  1081. AddItem (mi);
  1082. owner.UpdateItemInfo (true, cnt, Count);
  1083. }
  1084. public void AddRange (ObjectCollection col)
  1085. {
  1086. int cnt = Count;
  1087. foreach (object mi in col)
  1088. AddItem (mi);
  1089. owner.UpdateItemInfo (true, cnt, Count);
  1090. }
  1091. public virtual void Clear ()
  1092. {
  1093. object_items.Clear ();
  1094. listbox_items.Clear ();
  1095. owner.UpdateItemInfo (false, -1, -1);
  1096. }
  1097. public virtual bool Contains (object obj)
  1098. {
  1099. return object_items.Contains (obj);
  1100. }
  1101. public void CopyTo (object[] dest, int arrayIndex)
  1102. {
  1103. object_items.CopyTo (dest, arrayIndex);
  1104. }
  1105. void ICollection.CopyTo (Array dest, int index)
  1106. {
  1107. object_items.CopyTo (dest, index);
  1108. }
  1109. public virtual IEnumerator GetEnumerator ()
  1110. {
  1111. return object_items.GetEnumerator ();
  1112. }
  1113. int IList.Add (object item)
  1114. {
  1115. return Add (item);
  1116. }
  1117. public virtual int IndexOf (object value)
  1118. {
  1119. return object_items.IndexOf (value);
  1120. }
  1121. public virtual void Insert (int index, object item)
  1122. {
  1123. throw new NotImplementedException ();
  1124. }
  1125. public virtual void Remove (object value)
  1126. {
  1127. RemoveAt (IndexOf (value));
  1128. owner.UpdateItemInfo (false, -1, -1);
  1129. }
  1130. public virtual void RemoveAt (int index)
  1131. {
  1132. if (index < 0 || index >= Count)
  1133. throw new ArgumentOutOfRangeException ("Index of out range");
  1134. object_items.RemoveAt (index);
  1135. listbox_items.RemoveAt (index);
  1136. owner.UpdateItemInfo (false, -1, -1);
  1137. }
  1138. #endregion Public Methods
  1139. #region Private Methods
  1140. private int AddItem (object item)
  1141. {
  1142. int cnt = object_items.Count;
  1143. object_items.Add (item);
  1144. listbox_items.Add (new ListBox.ListBoxItem (cnt));
  1145. return cnt;
  1146. }
  1147. internal ListBox.ListBoxItem GetListBoxItem (int index)
  1148. {
  1149. if (index < 0 || index >= Count)
  1150. throw new ArgumentOutOfRangeException ("Index of out range");
  1151. return (ListBox.ListBoxItem) listbox_items[index];
  1152. }
  1153. internal void SetListBoxItem (ListBox.ListBoxItem item, int index)
  1154. {
  1155. if (index < 0 || index >= Count)
  1156. throw new ArgumentOutOfRangeException ("Index of out range");
  1157. listbox_items[index] = item;
  1158. }
  1159. internal void Sort ()
  1160. {
  1161. /* Keep this order */
  1162. listbox_items.Sort (new ListItemComparer (owner));
  1163. object_items.Sort (new ListObjectComparer (owner));
  1164. for (int i = 0; i < listbox_items.Count; i++) {
  1165. ListBox.ListBoxItem item = GetListBoxItem (i);
  1166. item.Index = i;
  1167. }
  1168. }
  1169. #endregion Private Methods
  1170. }
  1171. /*
  1172. ListBox.SelectedIndexCollection
  1173. */
  1174. public class SelectedIndexCollection : IList, ICollection, IEnumerable
  1175. {
  1176. private ListBox owner;
  1177. private ArrayList indices = new ArrayList ();
  1178. public SelectedIndexCollection (ListBox owner)
  1179. {
  1180. this.owner = owner;
  1181. }
  1182. #region Public Properties
  1183. public virtual int Count {
  1184. get { return indices.Count; }
  1185. }
  1186. public virtual bool IsReadOnly {
  1187. get { return true; }
  1188. }
  1189. public int this [int index] {
  1190. get {
  1191. if (index < 0 || index >= Count)
  1192. throw new ArgumentOutOfRangeException ("Index of out range");
  1193. return (int) indices[index];
  1194. }
  1195. }
  1196. bool ICollection.IsSynchronized {
  1197. get { return true; }
  1198. }
  1199. bool IList.IsFixedSize{
  1200. get { return true; }
  1201. }
  1202. object ICollection.SyncRoot {
  1203. get { return this; }
  1204. }
  1205. #endregion Public Properties
  1206. #region Public Methods
  1207. public bool Contains (int selectedIndex)
  1208. {
  1209. return indices.Contains (selectedIndex);
  1210. }
  1211. public virtual void CopyTo (Array dest, int index)
  1212. {
  1213. indices.CopyTo (dest, index);
  1214. }
  1215. public virtual IEnumerator GetEnumerator ()
  1216. {
  1217. return indices.GetEnumerator ();
  1218. }
  1219. int IList.Add (object obj)
  1220. {
  1221. throw new NotSupportedException ();
  1222. }
  1223. void IList.Clear ()
  1224. {
  1225. throw new NotSupportedException ();
  1226. }
  1227. bool IList.Contains (object selectedIndex)
  1228. {
  1229. return Contains ((int)selectedIndex);
  1230. }
  1231. int IList.IndexOf (object selectedIndex)
  1232. {
  1233. return IndexOf ((int) selectedIndex);
  1234. }
  1235. void IList.Insert (int index, object value)
  1236. {
  1237. throw new NotSupportedException ();
  1238. }
  1239. void IList.Remove (object value)
  1240. {
  1241. throw new NotSupportedException ();
  1242. }
  1243. void IList.RemoveAt (int index)
  1244. {
  1245. throw new NotSupportedException ();
  1246. }
  1247. object IList.this[int index]{
  1248. get {return indices[index]; }
  1249. set {throw new NotImplementedException (); }
  1250. }
  1251. public int IndexOf (int selectedIndex)
  1252. {
  1253. return indices.IndexOf (selectedIndex);
  1254. }
  1255. #endregion Public Methods
  1256. #region Private Methods
  1257. internal void AddIndex (int index)
  1258. {
  1259. indices.Add (index);
  1260. }
  1261. internal void ClearIndices ()
  1262. {
  1263. indices.Clear ();
  1264. }
  1265. internal void RemoveIndex (int index)
  1266. {
  1267. indices.Remove (index);
  1268. }
  1269. internal void ReCreate ()
  1270. {
  1271. indices.Clear ();
  1272. for (int i = 0; i < owner.Items.Count; i++) {
  1273. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1274. if (item.Selected)
  1275. indices.Add (item.Index);
  1276. }
  1277. }
  1278. #endregion Private Methods
  1279. }
  1280. /*
  1281. SelectedObjectCollection
  1282. */
  1283. public class SelectedObjectCollection : IList, ICollection, IEnumerable
  1284. {
  1285. private ListBox owner;
  1286. private ArrayList object_items = new ArrayList ();
  1287. public SelectedObjectCollection (ListBox owner)
  1288. {
  1289. this.owner = owner;
  1290. }
  1291. #region Public Properties
  1292. public virtual int Count {
  1293. get { return object_items.Count; }
  1294. }
  1295. public virtual bool IsReadOnly {
  1296. get { return true; }
  1297. }
  1298. public virtual object this [int index] {
  1299. get {
  1300. if (index < 0 || index >= Count)
  1301. throw new ArgumentOutOfRangeException ("Index of out range");
  1302. return object_items[index];
  1303. }
  1304. set {throw new NotSupportedException ();}
  1305. }
  1306. bool ICollection.IsSynchronized {
  1307. get { return true; }
  1308. }
  1309. object ICollection.SyncRoot {
  1310. get { return this; }
  1311. }
  1312. bool IList.IsFixedSize {
  1313. get { return true; }
  1314. }
  1315. object IList.this[int index] {
  1316. get { return object_items[index]; }
  1317. set { throw new NotSupportedException (); }
  1318. }
  1319. #endregion Public Properties
  1320. #region Public Methods
  1321. public virtual bool Contains (object selectedObject)
  1322. {
  1323. return object_items.Contains (selectedObject);
  1324. }
  1325. public virtual void CopyTo (Array dest, int index)
  1326. {
  1327. object_items.CopyTo (dest, index);
  1328. }
  1329. int IList.Add (object value)
  1330. {
  1331. throw new NotSupportedException ();
  1332. }
  1333. void IList.Clear ()
  1334. {
  1335. throw new NotSupportedException ();
  1336. }
  1337. bool IList.Contains (object selectedIndex)
  1338. {
  1339. throw new NotImplementedException ();
  1340. }
  1341. int IList.IndexOf (object selectedIndex)
  1342. {
  1343. return IndexOf ((int) selectedIndex);
  1344. }
  1345. void IList.Insert (int index, object value)
  1346. {
  1347. throw new NotSupportedException ();
  1348. }
  1349. void IList.Remove (object value)
  1350. {
  1351. throw new NotSupportedException ();
  1352. }
  1353. void IList.RemoveAt (int index)
  1354. {
  1355. throw new NotSupportedException ();
  1356. }
  1357. public int IndexOf (int selectedIndex)
  1358. {
  1359. return object_items.IndexOf (selectedIndex);
  1360. }
  1361. public virtual IEnumerator GetEnumerator ()
  1362. {
  1363. return object_items.GetEnumerator ();
  1364. }
  1365. #endregion Public Methods
  1366. #region Private Methods
  1367. internal void AddObject (object obj)
  1368. {
  1369. object_items.Add (obj);
  1370. }
  1371. internal void ClearObjects ()
  1372. {
  1373. object_items.Clear ();
  1374. }
  1375. internal void ReCreate ()
  1376. {
  1377. object_items.Clear ();
  1378. for (int i = 0; i < owner.Items.Count; i++) {
  1379. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1380. if (item.Selected)
  1381. object_items.Add (owner.Items[item.Index]);
  1382. }
  1383. }
  1384. internal void RemoveObject (object obj)
  1385. {
  1386. object_items.Remove (obj);
  1387. }
  1388. #endregion Private Methods
  1389. }
  1390. }
  1391. }