ListBox.cs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390
  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-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. // COMPLETE
  26. using System;
  27. using System.Drawing;
  28. using System.Collections;
  29. using System.ComponentModel;
  30. using System.ComponentModel.Design;
  31. using System.ComponentModel.Design.Serialization;
  32. using System.Reflection;
  33. using System.Runtime.InteropServices;
  34. namespace System.Windows.Forms
  35. {
  36. [DefaultProperty("Items")]
  37. [DefaultEvent("SelectedIndexChanged")]
  38. [Designer ("System.Windows.Forms.Design.ListBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. public class ListBox : ListControl
  40. {
  41. public const int DefaultItemHeight = 13;
  42. public const int NoMatches = -1;
  43. internal class ListBoxInfo
  44. {
  45. internal int item_height; /* Item's height */
  46. internal int top_item; /* First item that we show the in the current page */
  47. internal int last_item; /* Last visible item */
  48. internal int page_size; /* Number of listbox items per page. In MultiColumn listbox indicates items per column */
  49. internal Rectangle textdrawing_rect; /* Displayable Client Rectangle minus the scrollbars and with IntegralHeight calculated*/
  50. internal bool show_verticalsb; /* Is Vertical scrollbar show it? */
  51. internal bool show_horizontalsb; /* Is Horizontal scrollbar show it? */
  52. internal Rectangle client_rect; /* Client Rectangle. Usually = ClientRectangle except when IntegralHeight has been applied*/
  53. internal int max_itemwidth; /* Maxium item width within the listbox */
  54. public ListBoxInfo ()
  55. {
  56. last_item = 0;
  57. item_height = 0;
  58. top_item = 0;
  59. page_size = 0;
  60. max_itemwidth = 0;
  61. show_verticalsb = false;
  62. show_horizontalsb = false;
  63. }
  64. }
  65. internal class ListBoxItem
  66. {
  67. internal int Index;
  68. internal bool Selected;
  69. internal int ItemHeight; /* Only used for OwnerDrawVariable */
  70. internal CheckState State;
  71. public ListBoxItem (int index)
  72. {
  73. Index = index;
  74. Selected = false;
  75. ItemHeight = -1;
  76. State = CheckState.Unchecked;
  77. }
  78. public void CopyState (ListBoxItem src)
  79. {
  80. Selected = src.Selected;
  81. ItemHeight = src.ItemHeight;
  82. State = src.State;
  83. }
  84. }
  85. internal enum ItemNavigation
  86. {
  87. First,
  88. Last,
  89. Next,
  90. Previous,
  91. NextPage,
  92. PreviousPage,
  93. PreviousColumn,
  94. NextColumn
  95. }
  96. internal enum UpdateOperation
  97. {
  98. AddItems,
  99. DeleteItems,
  100. AllItems
  101. }
  102. private int column_width;
  103. private DrawMode draw_mode;
  104. private int horizontal_extent;
  105. private bool horizontal_scrollbar;
  106. private bool integral_height;
  107. private bool multicolumn;
  108. private bool scroll_always_visible;
  109. private int selected_index;
  110. private SelectedIndexCollection selected_indices;
  111. private SelectedObjectCollection selected_items;
  112. private SelectionMode selection_mode;
  113. private bool sorted;
  114. private bool use_tabstops;
  115. private int column_width_internal;
  116. private ImplicitVScrollBar vscrollbar_ctrl;
  117. private ImplicitHScrollBar hscrollbar_ctrl;
  118. private bool suspend_ctrlupdate;
  119. private bool ctrl_pressed;
  120. private bool shift_pressed;
  121. private bool has_focus;
  122. private bool use_item_height;
  123. internal int focused_item;
  124. internal ListBoxInfo listbox_info;
  125. internal ObjectCollection items;
  126. public ListBox ()
  127. {
  128. border_style = BorderStyle.Fixed3D;
  129. draw_mode = DrawMode.Normal;
  130. horizontal_extent = 0;
  131. horizontal_scrollbar = false;
  132. integral_height = true;
  133. multicolumn = false;
  134. scroll_always_visible = false;
  135. selected_index = -1;
  136. focused_item = -1;
  137. selection_mode = SelectionMode.One;
  138. sorted = false;
  139. use_tabstops = true;
  140. BackColor = ThemeEngine.Current.ColorWindow;
  141. ColumnWidth = 0;
  142. suspend_ctrlupdate = false;
  143. ctrl_pressed = false;
  144. shift_pressed = false;
  145. has_focus = false;
  146. use_item_height = false;
  147. items = new ObjectCollection (this);
  148. selected_indices = new SelectedIndexCollection (this);
  149. selected_items = new SelectedObjectCollection (this);
  150. listbox_info = new ListBoxInfo ();
  151. listbox_info.item_height = FontHeight;
  152. /* Vertical scrollbar */
  153. vscrollbar_ctrl = new ImplicitVScrollBar ();
  154. vscrollbar_ctrl.Minimum = 0;
  155. vscrollbar_ctrl.SmallChange = 1;
  156. vscrollbar_ctrl.LargeChange = 1;
  157. vscrollbar_ctrl.Maximum = 0;
  158. vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
  159. vscrollbar_ctrl.Visible = false;
  160. /* Horizontal scrollbar */
  161. hscrollbar_ctrl = new ImplicitHScrollBar ();
  162. hscrollbar_ctrl.Minimum = 0;
  163. hscrollbar_ctrl.SmallChange = 1;
  164. hscrollbar_ctrl.LargeChange = 1;
  165. hscrollbar_ctrl.Maximum = 0;
  166. hscrollbar_ctrl.Visible = false;
  167. hscrollbar_ctrl.ValueChanged += new EventHandler (HorizontalScrollEvent);
  168. /* Events */
  169. MouseDown += new MouseEventHandler (OnMouseDownLB);
  170. KeyDown += new KeyEventHandler (OnKeyDownLB);
  171. KeyUp += new KeyEventHandler (OnKeyUpLB);
  172. GotFocus += new EventHandler (OnGotFocus);
  173. LostFocus += new EventHandler (OnLostFocus);
  174. SetStyle (ControlStyles.UserPaint, false);
  175. }
  176. #region Events
  177. [Browsable (false)]
  178. [EditorBrowsable (EditorBrowsableState.Never)]
  179. public new event EventHandler BackgroundImageChanged;
  180. [Browsable (false)]
  181. [EditorBrowsable (EditorBrowsableState.Advanced)]
  182. public new event EventHandler Click;
  183. public event DrawItemEventHandler DrawItem;
  184. public event MeasureItemEventHandler MeasureItem;
  185. [Browsable (false)]
  186. [EditorBrowsable (EditorBrowsableState.Never)]
  187. public new event PaintEventHandler Paint;
  188. public event EventHandler SelectedIndexChanged;
  189. [Browsable (false)]
  190. [EditorBrowsable (EditorBrowsableState.Advanced)]
  191. public new event EventHandler TextChanged;
  192. #endregion // Events
  193. #region Public Properties
  194. public override Color BackColor {
  195. get { return base.BackColor; }
  196. set {
  197. if (base.BackColor == value)
  198. return;
  199. base.BackColor = value;
  200. base.Refresh (); // Careful. Calling the base method is not the same that calling
  201. } // the overriden one that refresh also all the items
  202. }
  203. [Browsable (false)]
  204. [EditorBrowsable (EditorBrowsableState.Never)]
  205. public override Image BackgroundImage {
  206. get { return base.BackgroundImage; }
  207. set {
  208. if (base.BackgroundImage == value)
  209. return;
  210. base.BackgroundImage = value;
  211. if (BackgroundImageChanged != null)
  212. BackgroundImageChanged (this, EventArgs.Empty);
  213. base.Refresh ();
  214. }
  215. }
  216. [DefaultValue (BorderStyle.Fixed3D)]
  217. [DispId(-504)]
  218. public BorderStyle BorderStyle {
  219. get { return InternalBorderStyle; }
  220. set { InternalBorderStyle = value; }
  221. }
  222. [DefaultValue (0)]
  223. [Localizable (true)]
  224. public int ColumnWidth {
  225. get { return column_width; }
  226. set {
  227. if (value < 0)
  228. throw new ArgumentException ("A value less than zero is assigned to the property.");
  229. column_width = value;
  230. if (value == 0)
  231. ColumnWidthInternal = 120;
  232. else
  233. ColumnWidthInternal = value;
  234. base.Refresh ();
  235. }
  236. }
  237. protected override CreateParams CreateParams {
  238. get { return base.CreateParams;}
  239. }
  240. protected override Size DefaultSize {
  241. get { return new Size (120, 96); }
  242. }
  243. [RefreshProperties(RefreshProperties.Repaint)]
  244. [DefaultValue (DrawMode.Normal)]
  245. public virtual DrawMode DrawMode {
  246. get { return draw_mode; }
  247. set {
  248. if (!Enum.IsDefined (typeof (DrawMode), value))
  249. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
  250. if (value == DrawMode.OwnerDrawVariable && multicolumn == true)
  251. throw new ArgumentException ("Cannot have variable height and multicolumn");
  252. if (draw_mode == value)
  253. return;
  254. draw_mode = value;
  255. base.Refresh ();
  256. }
  257. }
  258. public override Color ForeColor {
  259. get { return base.ForeColor; }
  260. set {
  261. if (base.ForeColor == value)
  262. return;
  263. base.ForeColor = value;
  264. base.Refresh ();
  265. }
  266. }
  267. [DefaultValue (0)]
  268. [Localizable (true)]
  269. public int HorizontalExtent {
  270. get { return horizontal_extent; }
  271. set {
  272. if (horizontal_extent == value)
  273. return;
  274. horizontal_extent = value;
  275. base.Refresh ();
  276. }
  277. }
  278. [DefaultValue (false)]
  279. [Localizable (true)]
  280. public bool HorizontalScrollbar {
  281. get { return horizontal_scrollbar; }
  282. set {
  283. if (horizontal_scrollbar == value)
  284. return;
  285. horizontal_scrollbar = value;
  286. UpdateShowHorizontalScrollBar ();
  287. base.Refresh ();
  288. }
  289. }
  290. [DefaultValue (true)]
  291. [Localizable (true)]
  292. [RefreshProperties(RefreshProperties.Repaint)]
  293. public bool IntegralHeight {
  294. get { return integral_height; }
  295. set {
  296. if (integral_height == value)
  297. return;
  298. integral_height = value;
  299. CalcClientArea ();
  300. }
  301. }
  302. [DefaultValue (13)]
  303. [Localizable (true)]
  304. [RefreshProperties(RefreshProperties.Repaint)]
  305. public virtual int ItemHeight {
  306. get {
  307. if (draw_mode == DrawMode.Normal)
  308. return FontHeight;
  309. return listbox_info.item_height;
  310. }
  311. set {
  312. if (value > 255)
  313. throw new ArgumentOutOfRangeException ("The ItemHeight property was set beyond 255 pixels");
  314. if (listbox_info.item_height == value)
  315. return;
  316. listbox_info.item_height = value;
  317. use_item_height = true;
  318. CalcClientArea ();
  319. }
  320. }
  321. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  322. [Localizable (true)]
  323. [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  324. public ObjectCollection Items {
  325. get { return items; }
  326. }
  327. [DefaultValue (false)]
  328. public bool MultiColumn {
  329. get { return multicolumn; }
  330. set {
  331. if (multicolumn == value)
  332. return;
  333. if (value == true && DrawMode == DrawMode.OwnerDrawVariable)
  334. throw new ArgumentException ("A multicolumn ListBox cannot have a variable-sized height.");
  335. multicolumn = value;
  336. if (IsHandleCreated) {
  337. RellocateScrollBars ();
  338. CalcClientArea ();
  339. UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  340. }
  341. }
  342. }
  343. [Browsable (false)]
  344. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  345. [EditorBrowsable (EditorBrowsableState.Advanced)]
  346. public int PreferredHeight {
  347. get {
  348. int itemsHeight = 0;
  349. if (draw_mode == DrawMode.Normal)
  350. itemsHeight = FontHeight * items.Count;
  351. else if (draw_mode == DrawMode.OwnerDrawFixed)
  352. itemsHeight = ItemHeight * items.Count;
  353. else if (draw_mode == DrawMode.OwnerDrawVariable) {
  354. for (int i = 0; i < items.Count; i++)
  355. itemsHeight += items.GetListBoxItem (i).ItemHeight;
  356. }
  357. return itemsHeight;
  358. }
  359. }
  360. public override RightToLeft RightToLeft {
  361. get { return base.RightToLeft; }
  362. set {
  363. base.RightToLeft = value;
  364. base.Refresh ();
  365. }
  366. }
  367. // Only affects the Vertical ScrollBar
  368. [DefaultValue (false)]
  369. [Localizable (true)]
  370. public bool ScrollAlwaysVisible {
  371. get { return scroll_always_visible; }
  372. set {
  373. if (scroll_always_visible == value)
  374. return;
  375. scroll_always_visible = value;
  376. UpdateShowVerticalScrollBar ();
  377. UpdateShowHorizontalScrollBar ();
  378. }
  379. }
  380. [Bindable(true)]
  381. [Browsable (false)]
  382. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  383. public override int SelectedIndex {
  384. get { return selected_index;}
  385. set {
  386. if (value < -1 || value >= Items.Count)
  387. throw new ArgumentOutOfRangeException ("Index of out range");
  388. if (SelectionMode == SelectionMode.None)
  389. throw new ArgumentException ("cannot call this method if SelectionMode is SelectionMode.None");
  390. if (selected_index == value)
  391. return;
  392. if (SelectionMode == SelectionMode.One)
  393. UnSelectItem (selected_index, true);
  394. SelectItem (value);
  395. selected_index = value;
  396. focused_item = value;
  397. OnSelectedIndexChanged (new EventArgs ());
  398. OnSelectedValueChanged (new EventArgs ());
  399. }
  400. }
  401. [Browsable (false)]
  402. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  403. public SelectedIndexCollection SelectedIndices {
  404. get { return selected_indices; }
  405. }
  406. [Bindable(true)]
  407. [Browsable (false)]
  408. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  409. public object SelectedItem {
  410. get {
  411. if (SelectedItems.Count > 0)
  412. return SelectedItems[0];
  413. else
  414. return null;
  415. }
  416. set {
  417. int index = Items.IndexOf (value);
  418. if (index == -1)
  419. return;
  420. if (index != SelectedIndex) {
  421. SelectedIndex = index;
  422. }
  423. }
  424. }
  425. [Browsable (false)]
  426. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  427. public SelectedObjectCollection SelectedItems {
  428. get {return selected_items;}
  429. }
  430. [DefaultValue (SelectionMode.One)]
  431. public virtual SelectionMode SelectionMode {
  432. get { return selection_mode; }
  433. set {
  434. if (!Enum.IsDefined (typeof (SelectionMode), value))
  435. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for SelectionMode", value));
  436. if (selection_mode == value)
  437. return;
  438. selection_mode = value;
  439. if (SelectedItems.Count > 0) {
  440. switch (selection_mode) {
  441. case SelectionMode.None:
  442. ClearSelected ();
  443. break;
  444. case SelectionMode.One: {
  445. if (SelectedItems.Count > 1) { // All except one
  446. int cnt = selected_indices.Count - 1;
  447. for (int i = 0; i < cnt; i++) {
  448. UnSelectItem (i, true);
  449. }
  450. }
  451. }
  452. break;
  453. default:
  454. break;
  455. }
  456. }
  457. }
  458. }
  459. [DefaultValue (false)]
  460. public bool Sorted {
  461. get { return sorted; }
  462. set {
  463. if (sorted == value)
  464. return;
  465. sorted = value;
  466. Sort ();
  467. }
  468. }
  469. [Bindable (false)]
  470. [Browsable (false)]
  471. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  472. [EditorBrowsable (EditorBrowsableState.Advanced)]
  473. public override string Text {
  474. get {
  475. if (SelectionMode != SelectionMode.None && SelectedIndex != -1)
  476. return GetItemText (SelectedItem);
  477. return base.Text;
  478. }
  479. set {
  480. base.Text = value;
  481. if (SelectionMode == SelectionMode.None)
  482. return;
  483. int index;
  484. index = FindStringExact (value);
  485. if (index == -1)
  486. return;
  487. SelectedIndex = index;
  488. }
  489. }
  490. [Browsable (false)]
  491. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  492. public int TopIndex {
  493. get { return LBoxInfo.top_item; }
  494. set {
  495. if (value == LBoxInfo.top_item)
  496. return;
  497. if (value < 0 || value >= Items.Count)
  498. return;
  499. LBoxInfo.top_item = value;
  500. UpdatedTopItem ();
  501. base.Refresh ();
  502. }
  503. }
  504. [DefaultValue (true)]
  505. public bool UseTabStops {
  506. get { return use_tabstops; }
  507. set {
  508. if (use_tabstops == value)
  509. return;
  510. use_tabstops = value;
  511. base.Refresh ();
  512. }
  513. }
  514. #endregion Public Properties
  515. #region Private Properties
  516. internal ListBoxInfo LBoxInfo {
  517. get { return listbox_info; }
  518. }
  519. private int ColumnWidthInternal {
  520. get { return column_width_internal; }
  521. set { column_width_internal = value; }
  522. }
  523. #endregion Private Properties
  524. #region Public Methods
  525. protected virtual void AddItemsCore (object[] value)
  526. {
  527. Items.AddRange (value);
  528. }
  529. public void BeginUpdate ()
  530. {
  531. suspend_ctrlupdate = true;
  532. }
  533. public void ClearSelected ()
  534. {
  535. foreach (int i in selected_indices) {
  536. UnSelectItem (i, false);
  537. }
  538. selected_indices.ClearIndices ();
  539. selected_items.ClearObjects ();
  540. }
  541. protected virtual ObjectCollection CreateItemCollection ()
  542. {
  543. return new ObjectCollection (this);
  544. }
  545. public void EndUpdate ()
  546. {
  547. suspend_ctrlupdate = false;
  548. UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  549. base.Refresh ();
  550. }
  551. public int FindString (String s)
  552. {
  553. return FindString (s, -1);
  554. }
  555. public int FindString (string s, int startIndex)
  556. {
  557. if (Items.Count == 0)
  558. return -1; // No exception throwing if empty
  559. if (startIndex < -1 || startIndex >= Items.Count - 1)
  560. throw new ArgumentOutOfRangeException ("Index of out range");
  561. startIndex++;
  562. for (int i = startIndex; i < Items.Count; i++) {
  563. if ((GetItemText (Items[i])).StartsWith (s))
  564. return i;
  565. }
  566. return NoMatches;
  567. }
  568. public int FindStringExact (string s)
  569. {
  570. return FindStringExact (s, -1);
  571. }
  572. public int FindStringExact (string s, int startIndex)
  573. {
  574. if (Items.Count == 0)
  575. return -1; // No exception throwing if empty
  576. if (startIndex < -1 || startIndex >= Items.Count - 1)
  577. throw new ArgumentOutOfRangeException ("Index of out range");
  578. startIndex++;
  579. for (int i = startIndex; i < Items.Count; i++) {
  580. if ((GetItemText (Items[i])).Equals (s))
  581. return i;
  582. }
  583. return NoMatches;
  584. }
  585. public int GetItemHeight (int index)
  586. {
  587. if (index < 0 || index >= Items.Count)
  588. throw new ArgumentOutOfRangeException ("Index of out range");
  589. if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated == true) {
  590. if ((Items.GetListBoxItem (index)).ItemHeight != -1) {
  591. return (Items.GetListBoxItem (index)).ItemHeight;
  592. }
  593. MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
  594. OnMeasureItem (args);
  595. (Items.GetListBoxItem (index)).ItemHeight = args.ItemHeight;
  596. return args.ItemHeight;
  597. }
  598. return ItemHeight;
  599. }
  600. public Rectangle GetItemRectangle (int index)
  601. {
  602. if (index < 0 || index >= Items.Count)
  603. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  604. Rectangle rect = new Rectangle ();
  605. if (MultiColumn == false) {
  606. rect.X = 0;
  607. rect.Height = GetItemHeight (index);
  608. rect.Width = listbox_info.textdrawing_rect.Width;
  609. if (DrawMode == DrawMode.OwnerDrawVariable) {
  610. rect.Y = 0;
  611. if (index >= listbox_info.top_item) {
  612. for (int i = listbox_info.top_item; i < index; i++) {
  613. rect.Y += GetItemHeight (i);
  614. }
  615. } else {
  616. for (int i = index; i < listbox_info.top_item; i++) {
  617. rect.Y -= GetItemHeight (i);
  618. }
  619. }
  620. } else {
  621. rect.Y = ItemHeight * (index - listbox_info.top_item);
  622. }
  623. }
  624. else {
  625. int which_page;
  626. which_page = index / listbox_info.page_size;
  627. rect.Y = ((index - listbox_info.top_item) % listbox_info.page_size) * ItemHeight;
  628. rect.X = which_page * ColumnWidthInternal;
  629. rect.Height = ItemHeight;
  630. rect.Width = ColumnWidthInternal;
  631. }
  632. return rect;
  633. }
  634. public bool GetSelected (int index)
  635. {
  636. if (index < 0 || index >= Items.Count)
  637. throw new ArgumentOutOfRangeException ("Index of out range");
  638. return (Items.GetListBoxItem (index)).Selected;
  639. }
  640. public int IndexFromPoint (Point p)
  641. {
  642. return IndexFromPoint (p.X, p.Y);
  643. }
  644. // Only returns visible points
  645. public int IndexFromPoint (int x, int y)
  646. {
  647. if (Items.Count == 0) {
  648. return -1;
  649. }
  650. for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
  651. if (GetItemRectangle (i).Contains (x,y) == true)
  652. return i;
  653. }
  654. return -1;
  655. }
  656. protected override void OnChangeUICues (UICuesEventArgs e)
  657. {
  658. base.OnChangeUICues (e);
  659. }
  660. protected override void OnDataSourceChanged (EventArgs e)
  661. {
  662. base.OnDataSourceChanged (e);
  663. BindDataItems (items);
  664. if (DataSource == null || DataManager == null) {
  665. SelectedIndex = -1;
  666. }
  667. else {
  668. SelectedIndex = DataManager.Position;
  669. }
  670. }
  671. protected override void OnDisplayMemberChanged (EventArgs e)
  672. {
  673. base.OnDisplayMemberChanged (e);
  674. if (DataManager == null || !IsHandleCreated)
  675. return;
  676. BindDataItems (items);
  677. base.Refresh ();
  678. }
  679. protected virtual void OnDrawItem (DrawItemEventArgs e)
  680. {
  681. if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable)) {
  682. DrawItem (this, e);
  683. return;
  684. }
  685. ThemeEngine.Current.DrawListBoxItem (this, e);
  686. }
  687. protected override void OnFontChanged (EventArgs e)
  688. {
  689. base.OnFontChanged (e);
  690. if (!use_item_height) {
  691. listbox_info.item_height = FontHeight;
  692. RellocateScrollBars ();
  693. CalcClientArea ();
  694. UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  695. } else {
  696. base.Refresh ();
  697. }
  698. }
  699. protected override void OnHandleCreated (EventArgs e)
  700. {
  701. base.OnHandleCreated (e);
  702. UpdateInternalClientRect (ClientRectangle);
  703. SuspendLayout ();
  704. Controls.AddImplicit (vscrollbar_ctrl);
  705. Controls.AddImplicit (hscrollbar_ctrl);
  706. ResumeLayout ();
  707. UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  708. }
  709. protected override void OnHandleDestroyed (EventArgs e)
  710. {
  711. base.OnHandleDestroyed (e);
  712. }
  713. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  714. {
  715. if (draw_mode != DrawMode.OwnerDrawVariable)
  716. return;
  717. if (MeasureItem != null)
  718. MeasureItem (this, e);
  719. }
  720. protected override void OnParentChanged (EventArgs e)
  721. {
  722. base.OnParentChanged (e);
  723. }
  724. protected override void OnResize (EventArgs e)
  725. {
  726. base.OnResize (e);
  727. UpdateInternalClientRect (ClientRectangle);
  728. }
  729. protected override void OnSelectedIndexChanged (EventArgs e)
  730. {
  731. base.OnSelectedIndexChanged (e);
  732. if (SelectedIndexChanged != null)
  733. SelectedIndexChanged (this, e);
  734. }
  735. protected override void OnSelectedValueChanged (EventArgs e)
  736. {
  737. base.OnSelectedValueChanged (e);
  738. }
  739. public override void Refresh ()
  740. {
  741. if (draw_mode == DrawMode.OwnerDrawVariable) {
  742. for (int i = 0; i < Items.Count; i++) {
  743. (Items.GetListBoxItem (i)).ItemHeight = -1;
  744. }
  745. }
  746. base.Refresh ();
  747. }
  748. protected override void RefreshItem (int index)
  749. {
  750. if (index < 0 || index >= Items.Count)
  751. throw new ArgumentOutOfRangeException ("Index of out range");
  752. if (draw_mode == DrawMode.OwnerDrawVariable) {
  753. (Items.GetListBoxItem (index)).ItemHeight = -1;
  754. }
  755. }
  756. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  757. {
  758. base.SetBoundsCore (x, y, width, height, specified);
  759. }
  760. protected override void SetItemCore (int index, object value)
  761. {
  762. if (index < 0 || index >= Items.Count)
  763. return;
  764. Items[index] = value;
  765. }
  766. protected override void SetItemsCore (IList value)
  767. {
  768. BeginUpdate ();
  769. try {
  770. Items.Clear ();
  771. Items.AddRange (value);
  772. } finally {
  773. EndUpdate ();
  774. }
  775. }
  776. public void SetSelected (int index, bool value)
  777. {
  778. if (index < 0 || index >= Items.Count)
  779. throw new ArgumentOutOfRangeException ("Index of out range");
  780. if (SelectionMode == SelectionMode.None)
  781. throw new InvalidOperationException ();
  782. if (value)
  783. SelectItem (index);
  784. else
  785. UnSelectItem (index, true);
  786. }
  787. protected virtual void Sort ()
  788. {
  789. if (Items.Count == 0)
  790. return;
  791. Items.Sort ();
  792. base.Refresh ();
  793. }
  794. public override string ToString ()
  795. {
  796. return base.ToString () + ", Items Count: " + Items.Count;
  797. }
  798. protected virtual void WmReflectCommand (ref Message m)
  799. {
  800. }
  801. protected override void WndProc (ref Message m)
  802. {
  803. switch ((Msg) m.Msg) {
  804. case Msg.WM_PAINT: {
  805. PaintEventArgs paint_event;
  806. paint_event = XplatUI.PaintEventStart (Handle, true);
  807. OnPaintLB (paint_event);
  808. XplatUI.PaintEventEnd (Handle, true);
  809. return;
  810. }
  811. case Msg.WM_ERASEBKGND:
  812. m.Result = (IntPtr) 1;
  813. return;
  814. default:
  815. break;
  816. }
  817. base.WndProc (ref m);
  818. }
  819. #endregion Public Methods
  820. #region Private Methods
  821. internal void CalcClientArea ()
  822. {
  823. listbox_info.textdrawing_rect = listbox_info.client_rect;
  824. if (listbox_info.show_verticalsb)
  825. listbox_info.textdrawing_rect.Width -= vscrollbar_ctrl.Width;
  826. if (listbox_info.show_horizontalsb)
  827. listbox_info.textdrawing_rect.Height -= hscrollbar_ctrl.Height;
  828. if (DrawMode == DrawMode.OwnerDrawVariable) {
  829. int height = 0;
  830. listbox_info.page_size = 0;
  831. for (int i = 0; i < Items.Count; i++) {
  832. height += GetItemHeight (i);
  833. if (height > listbox_info.textdrawing_rect.Height)
  834. break;
  835. listbox_info.page_size++;
  836. }
  837. } else {
  838. listbox_info.page_size = listbox_info.textdrawing_rect.Height / ItemHeight;
  839. }
  840. if (listbox_info.page_size == 0) {
  841. listbox_info.page_size = 1;
  842. }
  843. /* Adjust size to visible the maximum number of displayable items */
  844. if (IntegralHeight == true) {
  845. // From MS Docs: The integral height is based on the height of the ListBox, rather than
  846. // the client area height. As a result, when the IntegralHeight property is set true,
  847. // items can still be partially shown if scroll bars are displayed.
  848. int remaining = listbox_info.client_rect.Height % listbox_info.item_height;
  849. if (remaining > 0) {
  850. listbox_info.client_rect.Height -= remaining;
  851. CalcClientArea ();
  852. RellocateScrollBars ();
  853. base.Refresh ();
  854. }
  855. }
  856. }
  857. internal void Draw (Rectangle clip, Graphics dc)
  858. {
  859. // IntegralHeight has effect, we also have to paint the unused area
  860. if (IntegralHeight) {
  861. if (ClientRectangle.Height > listbox_info.client_rect.Height) {
  862. Region area = new Region (ClientRectangle);
  863. area.Exclude (LBoxInfo.textdrawing_rect);
  864. if (listbox_info.show_horizontalsb) {
  865. area.Exclude (new Rectangle (hscrollbar_ctrl.Location.X, hscrollbar_ctrl.Location.Y,
  866. hscrollbar_ctrl.Width, hscrollbar_ctrl.Height));
  867. }
  868. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
  869. area.GetBounds (dc));
  870. area.Dispose ();
  871. }
  872. }
  873. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
  874. LBoxInfo.textdrawing_rect);
  875. if (Items.Count > 0) {
  876. Rectangle item_rect;
  877. DrawItemState state = DrawItemState.None;
  878. for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
  879. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  880. if (clip.IntersectsWith (item_rect) == false)
  881. continue;
  882. /* Draw item */
  883. state = DrawItemState.None;
  884. if ((Items.GetListBoxItem (i)).Selected) {
  885. state |= DrawItemState.Selected;
  886. }
  887. if (has_focus == true && focused_item == i)
  888. state |= DrawItemState.Focus;
  889. if (MultiColumn == false && hscrollbar_ctrl != null && hscrollbar_ctrl.Visible) {
  890. item_rect.X -= hscrollbar_ctrl.Value;
  891. item_rect.Width += hscrollbar_ctrl.Value;
  892. }
  893. OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect,
  894. i, state, ForeColor, BackColor));
  895. }
  896. }
  897. }
  898. // Converts a GetItemRectangle to a one that we can display
  899. internal Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  900. {
  901. Rectangle item_rect;
  902. Rectangle first_item_rect = GetItemRectangle (first_displayble);
  903. item_rect = GetItemRectangle (index);
  904. item_rect.X -= first_item_rect.X;
  905. item_rect.Y -= first_item_rect.Y;
  906. return item_rect;
  907. }
  908. // Value Changed
  909. private void HorizontalScrollEvent (object sender, EventArgs e)
  910. {
  911. if (!multicolumn) {
  912. base.Refresh ();
  913. return;
  914. }
  915. int top_item = LBoxInfo.top_item;
  916. int last_item = LBoxInfo.last_item;
  917. LBoxInfo.top_item = listbox_info.page_size * hscrollbar_ctrl.Value;
  918. LBoxInfo.last_item = LastVisibleItem ();
  919. if (top_item != LBoxInfo.top_item || last_item != LBoxInfo.last_item)
  920. base.Refresh ();
  921. }
  922. // Only returns visible points. The diference of with IndexFromPoint is that the rectangle
  923. // has screen coordinates
  924. internal int IndexFromPointDisplayRectangle (int x, int y)
  925. {
  926. if (Items.Count == 0)
  927. return -1;
  928. for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
  929. if (GetItemDisplayRectangle (i, LBoxInfo.top_item).Contains (x, y) == true)
  930. return i;
  931. }
  932. return -1;
  933. }
  934. private int LastVisibleItem ()
  935. {
  936. Rectangle item_rect;
  937. int top_y = LBoxInfo.textdrawing_rect.Y + LBoxInfo.textdrawing_rect.Height;
  938. int i = 0;
  939. if (LBoxInfo.top_item >= Items.Count)
  940. return LBoxInfo.top_item;
  941. for (i = LBoxInfo.top_item; i < Items.Count; i++) {
  942. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  943. if (MultiColumn) {
  944. if (item_rect.X > LBoxInfo.textdrawing_rect.Width)
  945. return i - 1;
  946. }
  947. else {
  948. if (item_rect.Y + item_rect.Height > top_y) {
  949. return i;
  950. }
  951. }
  952. }
  953. return i - 1;
  954. }
  955. private void UpdatedTopItem ()
  956. {
  957. if (multicolumn) {
  958. int col = LBoxInfo.top_item / LBoxInfo.page_size;
  959. if (col > hscrollbar_ctrl.Maximum)
  960. hscrollbar_ctrl.Value = hscrollbar_ctrl.Maximum;
  961. else
  962. hscrollbar_ctrl.Value = col;
  963. }
  964. else {
  965. if (LBoxInfo.top_item > vscrollbar_ctrl.Maximum)
  966. vscrollbar_ctrl.Value = vscrollbar_ctrl.Maximum;
  967. else
  968. vscrollbar_ctrl.Value = LBoxInfo.top_item;
  969. }
  970. }
  971. // Navigates to the indicated item and returns the new item
  972. private int NavigateItemVisually (ItemNavigation navigation)
  973. {
  974. int page_size, columns, selected_index = -1;
  975. if (multicolumn) {
  976. columns = LBoxInfo.textdrawing_rect.Width / ColumnWidthInternal;
  977. page_size = columns * LBoxInfo.page_size;
  978. if (page_size == 0) {
  979. page_size = LBoxInfo.page_size;
  980. }
  981. } else {
  982. page_size = LBoxInfo.page_size;
  983. }
  984. switch (navigation) {
  985. case ItemNavigation.PreviousColumn: {
  986. if (focused_item - LBoxInfo.page_size < 0) {
  987. return -1;
  988. }
  989. if (focused_item - LBoxInfo.page_size < LBoxInfo.top_item) {
  990. LBoxInfo.top_item = focused_item - LBoxInfo.page_size;
  991. UpdatedTopItem ();
  992. }
  993. selected_index = focused_item - LBoxInfo.page_size;
  994. break;
  995. }
  996. case ItemNavigation.NextColumn: {
  997. if (focused_item + LBoxInfo.page_size >= Items.Count) {
  998. break;
  999. }
  1000. if (focused_item + LBoxInfo.page_size > LBoxInfo.last_item) {
  1001. LBoxInfo.top_item = focused_item;
  1002. UpdatedTopItem ();
  1003. }
  1004. selected_index = focused_item + LBoxInfo.page_size;
  1005. break;
  1006. }
  1007. case ItemNavigation.First: {
  1008. LBoxInfo.top_item = 0;
  1009. selected_index = 0;
  1010. UpdatedTopItem ();
  1011. break;
  1012. }
  1013. case ItemNavigation.Last: {
  1014. if (Items.Count < LBoxInfo.page_size) {
  1015. LBoxInfo.top_item = 0;
  1016. selected_index = Items.Count - 1;
  1017. UpdatedTopItem ();
  1018. } else {
  1019. LBoxInfo.top_item = Items.Count - LBoxInfo.page_size;
  1020. selected_index = Items.Count - 1;
  1021. UpdatedTopItem ();
  1022. }
  1023. break;
  1024. }
  1025. case ItemNavigation.Next: {
  1026. if (focused_item + 1 < Items.Count) {
  1027. int actualHeight = 0;
  1028. if (draw_mode == DrawMode.OwnerDrawVariable) {
  1029. for (int i = LBoxInfo.top_item; i <= focused_item + 1; i++)
  1030. actualHeight += GetItemHeight (i);
  1031. } else {
  1032. actualHeight = ((focused_item + 1) - LBoxInfo.top_item + 1) * ItemHeight;
  1033. }
  1034. if (actualHeight >= LBoxInfo.textdrawing_rect.Height) {
  1035. int bal = IntegralHeight ? 0 : (listbox_info.textdrawing_rect.Height == actualHeight ? 0 : 1);
  1036. if (focused_item + bal >= LBoxInfo.last_item) {
  1037. LBoxInfo.top_item++;
  1038. UpdatedTopItem ();
  1039. }
  1040. }
  1041. selected_index = focused_item + 1;
  1042. }
  1043. break;
  1044. }
  1045. case ItemNavigation.Previous: {
  1046. if (focused_item > 0) {
  1047. if (focused_item - 1 < LBoxInfo.top_item) {
  1048. LBoxInfo.top_item--;
  1049. UpdatedTopItem ();
  1050. }
  1051. selected_index = focused_item - 1;
  1052. }
  1053. break;
  1054. }
  1055. case ItemNavigation.NextPage: {
  1056. if (Items.Count < page_size) {
  1057. NavigateItemVisually (ItemNavigation.Last);
  1058. break;
  1059. }
  1060. if (focused_item + page_size - 1 >= Items.Count) {
  1061. LBoxInfo.top_item = Items.Count - page_size;
  1062. UpdatedTopItem ();
  1063. selected_index = Items.Count - 1;
  1064. }
  1065. else {
  1066. if (focused_item + page_size - 1 > LBoxInfo.last_item) {
  1067. LBoxInfo.top_item = focused_item;
  1068. UpdatedTopItem ();
  1069. }
  1070. selected_index = focused_item + page_size - 1;
  1071. }
  1072. break;
  1073. }
  1074. case ItemNavigation.PreviousPage: {
  1075. if (focused_item - (LBoxInfo.page_size - 1) <= 0) {
  1076. LBoxInfo.top_item = 0;
  1077. UpdatedTopItem ();
  1078. SelectedIndex = 0;
  1079. }
  1080. else {
  1081. if (focused_item - (LBoxInfo.page_size - 1) < LBoxInfo.top_item) {
  1082. LBoxInfo.top_item = focused_item - (LBoxInfo.page_size - 1);
  1083. UpdatedTopItem ();
  1084. }
  1085. selected_index = focused_item - (LBoxInfo.page_size - 1);
  1086. }
  1087. break;
  1088. }
  1089. default:
  1090. break;
  1091. }
  1092. return selected_index;
  1093. }
  1094. private void OnGotFocus (object sender, EventArgs e)
  1095. {
  1096. has_focus = true;
  1097. if (focused_item != -1) {
  1098. Rectangle invalidate = GetItemDisplayRectangle (focused_item, LBoxInfo.top_item);
  1099. Invalidate (invalidate);
  1100. }
  1101. }
  1102. private void OnLostFocus (object sender, EventArgs e)
  1103. {
  1104. has_focus = false;
  1105. if (focused_item != -1) {
  1106. Rectangle invalidate = GetItemDisplayRectangle (focused_item, LBoxInfo.top_item);
  1107. Invalidate (invalidate);
  1108. }
  1109. }
  1110. private void OnKeyDownLB (object sender, KeyEventArgs e)
  1111. {
  1112. int new_item = -1;
  1113. if (Items.Count == 0)
  1114. return;
  1115. switch (e.KeyCode) {
  1116. case Keys.ControlKey:
  1117. ctrl_pressed = true;
  1118. break;
  1119. case Keys.ShiftKey:
  1120. shift_pressed = true;
  1121. break;
  1122. case Keys.Home:
  1123. new_item = NavigateItemVisually (ItemNavigation.First);
  1124. break;
  1125. case Keys.End:
  1126. new_item = NavigateItemVisually (ItemNavigation.Last);
  1127. break;
  1128. case Keys.Up:
  1129. new_item = NavigateItemVisually (ItemNavigation.Previous);
  1130. break;
  1131. case Keys.Down:
  1132. new_item = NavigateItemVisually (ItemNavigation.Next);
  1133. break;
  1134. case Keys.PageUp:
  1135. new_item = NavigateItemVisually (ItemNavigation.PreviousPage);
  1136. break;
  1137. case Keys.PageDown:
  1138. new_item = NavigateItemVisually (ItemNavigation.NextPage);
  1139. break;
  1140. case Keys.Right:
  1141. if (multicolumn == true) {
  1142. new_item = NavigateItemVisually (ItemNavigation.NextColumn);
  1143. }
  1144. break;
  1145. case Keys.Left:
  1146. if (multicolumn == true) {
  1147. new_item = NavigateItemVisually (ItemNavigation.PreviousColumn);
  1148. }
  1149. break;
  1150. case Keys.Space:
  1151. if (selection_mode == SelectionMode.MultiSimple) {
  1152. SelectedItemFromNavigation (focused_item);
  1153. }
  1154. break;
  1155. default:
  1156. break;
  1157. }
  1158. if (new_item != -1) {
  1159. SetFocusedItem (new_item);
  1160. }
  1161. if (new_item != -1) {
  1162. if (selection_mode != SelectionMode.MultiSimple && selection_mode != SelectionMode.None) {
  1163. SelectedItemFromNavigation (new_item);
  1164. }
  1165. }
  1166. }
  1167. private void OnKeyUpLB (object sender, KeyEventArgs e)
  1168. {
  1169. switch (e.KeyCode) {
  1170. case Keys.ControlKey:
  1171. ctrl_pressed = false;
  1172. break;
  1173. case Keys.ShiftKey:
  1174. shift_pressed = false;
  1175. break;
  1176. default:
  1177. break;
  1178. }
  1179. }
  1180. internal virtual void OnMouseDownLB (object sender, MouseEventArgs e)
  1181. {
  1182. if (Click != null) {
  1183. if (e.Button == MouseButtons.Left) {
  1184. Click (this, e);
  1185. }
  1186. }
  1187. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  1188. if (index != -1) {
  1189. SelectedItemFromNavigation (index);
  1190. SetFocusedItem (index);
  1191. }
  1192. }
  1193. private void OnPaintLB (PaintEventArgs pevent)
  1194. {
  1195. if (Paint != null)
  1196. Paint (this, pevent);
  1197. if (suspend_ctrlupdate == true)
  1198. return;
  1199. Draw (pevent.ClipRectangle, pevent.Graphics);
  1200. }
  1201. internal void RellocateScrollBars ()
  1202. {
  1203. if (listbox_info.show_verticalsb) {
  1204. vscrollbar_ctrl.Size = new Size (vscrollbar_ctrl.Width, ClientRectangle.Height);
  1205. vscrollbar_ctrl.Location = new Point (ClientRectangle.Width - vscrollbar_ctrl.Width, 0);
  1206. }
  1207. if (listbox_info.show_horizontalsb) {
  1208. int width = listbox_info.client_rect.Width;
  1209. if (listbox_info.show_verticalsb)
  1210. width -= vscrollbar_ctrl.Width;
  1211. hscrollbar_ctrl.Size = new Size (width, hscrollbar_ctrl.Height);
  1212. hscrollbar_ctrl.Location = new Point (0, ClientRectangle.Height - hscrollbar_ctrl.Height);
  1213. }
  1214. CalcClientArea ();
  1215. }
  1216. // Add an item in the Selection array and marks it visually as selected
  1217. private void SelectItem (int index)
  1218. {
  1219. if (index == -1)
  1220. return;
  1221. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  1222. (Items.GetListBoxItem (index)).Selected = true;
  1223. selected_indices.AddIndex (index);
  1224. selected_items.AddObject (Items[index]);
  1225. if (ClientRectangle.IntersectsWith (invalidate))
  1226. Invalidate (invalidate);
  1227. }
  1228. // An item navigation operation (mouse or keyboard) has caused to select a new item
  1229. internal void SelectedItemFromNavigation (int index)
  1230. {
  1231. switch (SelectionMode) {
  1232. case SelectionMode.None: // Do nothing
  1233. break;
  1234. case SelectionMode.One: {
  1235. SelectedIndex = index;
  1236. break;
  1237. }
  1238. case SelectionMode.MultiSimple: {
  1239. if (selected_index == -1) {
  1240. SelectedIndex = index;
  1241. } else {
  1242. if ((Items.GetListBoxItem (index)).Selected) // BUG: index or selected_index?
  1243. UnSelectItem (index, true);
  1244. else {
  1245. SelectItem (index);
  1246. OnSelectedIndexChanged (new EventArgs ());
  1247. OnSelectedValueChanged (new EventArgs ());
  1248. }
  1249. }
  1250. break;
  1251. }
  1252. case SelectionMode.MultiExtended: {
  1253. if (selected_index == -1) {
  1254. SelectedIndex = index;
  1255. } else {
  1256. if (ctrl_pressed == false && shift_pressed == false) {
  1257. ClearSelected ();
  1258. }
  1259. if (shift_pressed == true) {
  1260. ShiftSelection (index);
  1261. } else { // ctrl_pressed or single item
  1262. SelectItem (index);
  1263. }
  1264. OnSelectedIndexChanged (new EventArgs ());
  1265. OnSelectedValueChanged (new EventArgs ());
  1266. }
  1267. break;
  1268. }
  1269. default:
  1270. break;
  1271. }
  1272. }
  1273. private void ShiftSelection (int index)
  1274. {
  1275. int shorter_item = -1, dist = Items.Count + 1, cur_dist;
  1276. foreach (int idx in selected_indices) {
  1277. if (idx > index) {
  1278. cur_dist = idx - index;
  1279. }
  1280. else {
  1281. cur_dist = index - idx;
  1282. }
  1283. if (cur_dist < dist) {
  1284. dist = cur_dist;
  1285. shorter_item = idx;
  1286. }
  1287. }
  1288. if (shorter_item != -1) {
  1289. int start, end;
  1290. if (shorter_item > index) {
  1291. start = index;
  1292. end = shorter_item;
  1293. } else {
  1294. start = shorter_item;
  1295. end = index;
  1296. }
  1297. ClearSelected ();
  1298. for (int idx = start; idx <= end; idx++) {
  1299. SelectItem (idx);
  1300. }
  1301. }
  1302. }
  1303. internal void SetFocusedItem (int index)
  1304. {
  1305. Rectangle invalidate;
  1306. int prev = focused_item;
  1307. focused_item = index;
  1308. if (has_focus == false)
  1309. return;
  1310. if (prev != -1) { // Invalidates previous item
  1311. invalidate = GetItemDisplayRectangle (prev, LBoxInfo.top_item);
  1312. Invalidate (invalidate);
  1313. }
  1314. if (index != -1) {
  1315. invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  1316. Invalidate (invalidate);
  1317. }
  1318. }
  1319. // Removes an item in the Selection array and marks it visually as unselected
  1320. private void UnSelectItem (int index, bool remove)
  1321. {
  1322. if (index == -1)
  1323. return;
  1324. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  1325. (Items.GetListBoxItem (index)).Selected = false;
  1326. if (remove) {
  1327. selected_indices.RemoveIndex (index);
  1328. selected_items.RemoveObject (Items[index]);
  1329. }
  1330. if (ClientRectangle.IntersectsWith (invalidate))
  1331. Invalidate (invalidate);
  1332. }
  1333. internal StringFormat GetFormatString ()
  1334. {
  1335. StringFormat string_format = new StringFormat ();
  1336. if (RightToLeft == RightToLeft.Yes)
  1337. string_format.Alignment = StringAlignment.Far;
  1338. else
  1339. string_format.Alignment = StringAlignment.Near;
  1340. if (UseTabStops)
  1341. string_format.SetTabStops (0, new float [] {(float)(Font.Height * 3.7)});
  1342. return string_format;
  1343. }
  1344. // Updates the scrollbar's position with the new items and inside area
  1345. internal virtual void UpdateItemInfo (UpdateOperation operation, int first, int last)
  1346. {
  1347. if (!IsHandleCreated || suspend_ctrlupdate == true)
  1348. return;
  1349. UpdateShowVerticalScrollBar ();
  1350. if (listbox_info.show_verticalsb && Items.Count > listbox_info.page_size)
  1351. if (vscrollbar_ctrl.Enabled)
  1352. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  1353. if (listbox_info.show_horizontalsb) {
  1354. if (MultiColumn) {
  1355. int fullpage = (listbox_info.page_size * (listbox_info.client_rect.Width / ColumnWidthInternal));
  1356. if (hscrollbar_ctrl.Enabled && listbox_info.page_size > 0)
  1357. hscrollbar_ctrl.Maximum = Math.Max (0, 1 + ((Items.Count - fullpage) / listbox_info.page_size));
  1358. }
  1359. }
  1360. if (MultiColumn == false) {
  1361. /* Calc the longest items for non multicolumn listboxes */
  1362. if (operation == UpdateOperation.AllItems || operation == UpdateOperation.DeleteItems) {
  1363. SizeF size;
  1364. for (int i = 0; i < Items.Count; i++) {
  1365. size = DeviceContext.MeasureString (GetItemText (Items[i]), Font);
  1366. if ((int) size.Width > listbox_info.max_itemwidth)
  1367. listbox_info.max_itemwidth = (int) size.Width;
  1368. }
  1369. }
  1370. else {
  1371. if (operation == UpdateOperation.AddItems) {
  1372. SizeF size;
  1373. for (int i = first; i < last + 1; i++) {
  1374. size = DeviceContext.MeasureString (GetItemText (Items[i]), Font);
  1375. if ((int) size.Width > listbox_info.max_itemwidth)
  1376. listbox_info.max_itemwidth = (int) size.Width;
  1377. }
  1378. }
  1379. }
  1380. }
  1381. if (sorted)
  1382. Sort ();
  1383. if (Items.Count == 0) {
  1384. selected_index = -1;
  1385. focused_item = -1;
  1386. }
  1387. SelectedItems.ReCreate ();
  1388. SelectedIndices.ReCreate ();
  1389. UpdateShowHorizontalScrollBar ();
  1390. LBoxInfo.last_item = LastVisibleItem ();
  1391. base.Refresh ();
  1392. }
  1393. private void UpdateInternalClientRect (Rectangle client_rectangle)
  1394. {
  1395. listbox_info.client_rect = client_rectangle;
  1396. UpdateShowHorizontalScrollBar ();
  1397. UpdateShowVerticalScrollBar ();
  1398. RellocateScrollBars ();
  1399. UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  1400. }
  1401. /* Determines if the horizontal scrollbar has to be displyed */
  1402. private void UpdateShowHorizontalScrollBar ()
  1403. {
  1404. bool show = false;
  1405. bool enabled = true;
  1406. if (MultiColumn) { /* Horizontal scrollbar is always shown in Multicolum mode */
  1407. /* Is it really need it */
  1408. int page_size = listbox_info.client_rect.Height / listbox_info.item_height;
  1409. int fullpage = (page_size * (listbox_info.textdrawing_rect.Height / ColumnWidthInternal));
  1410. if (Items.Count > fullpage) {
  1411. show = true;
  1412. }
  1413. else { /* Acording to MS Documentation ScrollAlwaysVisible only affects Horizontal scrollbars but
  1414. this is not true for MultiColumn listboxes */
  1415. if (ScrollAlwaysVisible == true) {
  1416. enabled = false;
  1417. show = true;
  1418. }
  1419. }
  1420. } else { /* If large item*/
  1421. if (listbox_info.max_itemwidth > listbox_info.client_rect.Width && HorizontalScrollbar) {
  1422. show = true;
  1423. hscrollbar_ctrl.Maximum = listbox_info.max_itemwidth;
  1424. hscrollbar_ctrl.LargeChange = listbox_info.textdrawing_rect.Width;
  1425. }
  1426. }
  1427. if (hscrollbar_ctrl.Enabled != enabled)
  1428. hscrollbar_ctrl.Enabled = enabled;
  1429. if (listbox_info.show_horizontalsb == show)
  1430. return;
  1431. listbox_info.show_horizontalsb = show;
  1432. hscrollbar_ctrl.Visible = show;
  1433. if (show == true) {
  1434. RellocateScrollBars ();
  1435. }
  1436. CalcClientArea ();
  1437. }
  1438. /* Determines if the vertical scrollbar has to be displyed */
  1439. private void UpdateShowVerticalScrollBar ()
  1440. {
  1441. bool show = false;
  1442. bool enabled = true;
  1443. if (!MultiColumn) { /* Vertical scrollbar is never shown in Multicolum mode */
  1444. if (Items.Count > listbox_info.page_size) {
  1445. show = true;
  1446. }
  1447. else
  1448. if (ScrollAlwaysVisible) {
  1449. show = true;
  1450. enabled = false;
  1451. }
  1452. }
  1453. if (vscrollbar_ctrl.Enabled != enabled)
  1454. vscrollbar_ctrl.Enabled = enabled;
  1455. if (listbox_info.show_verticalsb == show)
  1456. return;
  1457. listbox_info.show_verticalsb = show;
  1458. vscrollbar_ctrl.Visible = show;
  1459. if (show == true) {
  1460. if (vscrollbar_ctrl.Enabled)
  1461. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  1462. RellocateScrollBars ();
  1463. } else if (vscrollbar_ctrl.Maximum > 0) {
  1464. vscrollbar_ctrl.Maximum = 0;
  1465. }
  1466. CalcClientArea ();
  1467. }
  1468. // Value Changed
  1469. private void VerticalScrollEvent (object sender, EventArgs e)
  1470. {
  1471. int top_item = LBoxInfo.top_item;
  1472. int last_item = LBoxInfo.last_item;
  1473. LBoxInfo.top_item = /*listbox_info.page_size + */ vscrollbar_ctrl.Value;
  1474. LBoxInfo.last_item = LastVisibleItem ();
  1475. if (top_item != LBoxInfo.top_item || last_item != LBoxInfo.last_item)
  1476. base.Refresh ();
  1477. }
  1478. #endregion Private Methods
  1479. /*
  1480. ListBox.ObjectCollection
  1481. */
  1482. [ListBindable (false)]
  1483. public class ObjectCollection : IList, ICollection, IEnumerable
  1484. {
  1485. // Compare objects
  1486. internal class ListObjectComparer : IComparer
  1487. {
  1488. private ListBox owner;
  1489. public ListObjectComparer (ListBox owner)
  1490. {
  1491. this.owner = owner;
  1492. }
  1493. public int Compare (object a, object b)
  1494. {
  1495. string str1 = a.ToString ();
  1496. string str2 = b.ToString ();
  1497. return str1.CompareTo (str2);
  1498. }
  1499. }
  1500. // Compare ListItem
  1501. internal class ListItemComparer : IComparer
  1502. {
  1503. private ListBox owner;
  1504. public ListItemComparer (ListBox owner)
  1505. {
  1506. this.owner = owner;
  1507. }
  1508. public int Compare (object a, object b)
  1509. {
  1510. int index1 = ((ListBox.ListBoxItem) (a)).Index;
  1511. int index2 = ((ListBox.ListBoxItem) (b)).Index;
  1512. string str1 = owner.GetItemText (owner.Items[index1]);
  1513. string str2 = owner.GetItemText (owner.Items[index2]);
  1514. return str1.CompareTo (str2);
  1515. }
  1516. }
  1517. private ListBox owner;
  1518. internal ArrayList object_items = new ArrayList ();
  1519. internal ArrayList listbox_items = new ArrayList ();
  1520. public ObjectCollection (ListBox owner)
  1521. {
  1522. this.owner = owner;
  1523. }
  1524. public ObjectCollection (ListBox owner, object[] obj)
  1525. {
  1526. this.owner = owner;
  1527. AddRange (obj);
  1528. }
  1529. public ObjectCollection (ListBox owner, ObjectCollection obj)
  1530. {
  1531. this.owner = owner;
  1532. AddRange (obj);
  1533. }
  1534. #region Public Properties
  1535. public int Count {
  1536. get { return object_items.Count; }
  1537. }
  1538. public bool IsReadOnly {
  1539. get { return false; }
  1540. }
  1541. [Browsable(false)]
  1542. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1543. public virtual object this [int index] {
  1544. get {
  1545. if (index < 0 || index >= Count)
  1546. throw new ArgumentOutOfRangeException ("Index of out range");
  1547. return object_items[index];
  1548. }
  1549. set {
  1550. if (index < 0 || index >= Count)
  1551. throw new ArgumentOutOfRangeException ("Index of out range");
  1552. object_items[index] = value;
  1553. }
  1554. }
  1555. bool ICollection.IsSynchronized {
  1556. get { return false; }
  1557. }
  1558. object ICollection.SyncRoot {
  1559. get { return this; }
  1560. }
  1561. bool IList.IsFixedSize {
  1562. get { return false; }
  1563. }
  1564. #endregion Public Properties
  1565. #region Private Properties
  1566. internal ArrayList ObjectItems {
  1567. get { return object_items;}
  1568. set {
  1569. object_items = value;
  1570. }
  1571. }
  1572. internal ArrayList ListBoxItems {
  1573. get { return listbox_items;}
  1574. set {
  1575. listbox_items = value;
  1576. }
  1577. }
  1578. #endregion Private Properties
  1579. #region Public Methods
  1580. public int Add (object item)
  1581. {
  1582. int idx;
  1583. idx = AddItem (item);
  1584. owner.UpdateItemInfo (UpdateOperation.AddItems, idx, idx);
  1585. return idx;
  1586. }
  1587. public void AddRange (object[] items)
  1588. {
  1589. int cnt = Count;
  1590. foreach (object mi in items)
  1591. AddItem (mi);
  1592. owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, Count - 1);
  1593. }
  1594. public void AddRange (ObjectCollection col)
  1595. {
  1596. int cnt = Count;
  1597. foreach (object mi in col)
  1598. AddItem (mi);
  1599. owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, Count - 1);
  1600. }
  1601. internal void AddRange (IList list)
  1602. {
  1603. int cnt = Count;
  1604. foreach (object mi in list)
  1605. AddItem (mi);
  1606. owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, Count - 1);
  1607. }
  1608. public virtual void Clear ()
  1609. {
  1610. owner.selected_index = -1;
  1611. owner.focused_item = -1;
  1612. object_items.Clear ();
  1613. listbox_items.Clear ();
  1614. owner.UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  1615. }
  1616. public bool Contains (object obj)
  1617. {
  1618. return object_items.Contains (obj);
  1619. }
  1620. public void CopyTo (object[] dest, int arrayIndex)
  1621. {
  1622. object_items.CopyTo (dest, arrayIndex);
  1623. }
  1624. void ICollection.CopyTo (Array dest, int index)
  1625. {
  1626. object_items.CopyTo (dest, index);
  1627. }
  1628. public IEnumerator GetEnumerator ()
  1629. {
  1630. return object_items.GetEnumerator ();
  1631. }
  1632. int IList.Add (object item)
  1633. {
  1634. return Add (item);
  1635. }
  1636. public int IndexOf (object value)
  1637. {
  1638. return object_items.IndexOf (value);
  1639. }
  1640. public void Insert (int index, object item)
  1641. {
  1642. if (index < 0 || index > Count)
  1643. throw new ArgumentOutOfRangeException ("Index of out range");
  1644. int idx;
  1645. ObjectCollection new_items = new ObjectCollection (owner);
  1646. owner.BeginUpdate ();
  1647. for (int i = 0; i < index; i++) {
  1648. idx = new_items.AddItem (ObjectItems[i]);
  1649. (new_items.GetListBoxItem (idx)).CopyState (GetListBoxItem (i));
  1650. }
  1651. new_items.AddItem (item);
  1652. for (int i = index; i < Count; i++){
  1653. idx = new_items.AddItem (ObjectItems[i]);
  1654. (new_items.GetListBoxItem (idx)).CopyState (GetListBoxItem (i));
  1655. }
  1656. ObjectItems = new_items.ObjectItems;
  1657. ListBoxItems = new_items.ListBoxItems;
  1658. owner.EndUpdate (); // Calls UpdateItemInfo
  1659. }
  1660. public void Remove (object value)
  1661. {
  1662. RemoveAt (IndexOf (value));
  1663. }
  1664. public void RemoveAt (int index)
  1665. {
  1666. if (index < 0 || index >= Count)
  1667. throw new ArgumentOutOfRangeException ("Index of out range");
  1668. object_items.RemoveAt (index);
  1669. listbox_items.RemoveAt (index);
  1670. owner.UpdateItemInfo (UpdateOperation.DeleteItems, index, index);
  1671. }
  1672. #endregion Public Methods
  1673. #region Private Methods
  1674. internal int AddItem (object item)
  1675. {
  1676. int cnt = object_items.Count;
  1677. object_items.Add (item);
  1678. listbox_items.Add (new ListBox.ListBoxItem (cnt));
  1679. return cnt;
  1680. }
  1681. internal ListBox.ListBoxItem GetListBoxItem (int index)
  1682. {
  1683. if (index < 0 || index >= Count)
  1684. throw new ArgumentOutOfRangeException ("Index of out range");
  1685. return (ListBox.ListBoxItem) listbox_items[index];
  1686. }
  1687. internal void SetListBoxItem (ListBox.ListBoxItem item, int index)
  1688. {
  1689. if (index < 0 || index >= Count)
  1690. throw new ArgumentOutOfRangeException ("Index of out range");
  1691. listbox_items[index] = item;
  1692. }
  1693. internal void Sort ()
  1694. {
  1695. /* Keep this order */
  1696. listbox_items.Sort (new ListItemComparer (owner));
  1697. object_items.Sort (new ListObjectComparer (owner));
  1698. for (int i = 0; i < listbox_items.Count; i++) {
  1699. ListBox.ListBoxItem item = GetListBoxItem (i);
  1700. item.Index = i;
  1701. }
  1702. }
  1703. #endregion Private Methods
  1704. }
  1705. /*
  1706. ListBox.SelectedIndexCollection
  1707. */
  1708. public class SelectedIndexCollection : IList, ICollection, IEnumerable
  1709. {
  1710. private ListBox owner;
  1711. private ArrayList indices = new ArrayList ();
  1712. public SelectedIndexCollection (ListBox owner)
  1713. {
  1714. this.owner = owner;
  1715. }
  1716. #region Public Properties
  1717. [Browsable (false)]
  1718. public int Count {
  1719. get { return indices.Count; }
  1720. }
  1721. public bool IsReadOnly {
  1722. get { return true; }
  1723. }
  1724. public int this [int index] {
  1725. get {
  1726. if (index < 0 || index >= Count)
  1727. throw new ArgumentOutOfRangeException ("Index of out range");
  1728. return (int) indices[index];
  1729. }
  1730. }
  1731. bool ICollection.IsSynchronized {
  1732. get { return true; }
  1733. }
  1734. bool IList.IsFixedSize{
  1735. get { return true; }
  1736. }
  1737. object ICollection.SyncRoot {
  1738. get { return this; }
  1739. }
  1740. #endregion Public Properties
  1741. #region Public Methods
  1742. public bool Contains (int selectedIndex)
  1743. {
  1744. return indices.Contains (selectedIndex);
  1745. }
  1746. public void CopyTo (Array dest, int index)
  1747. {
  1748. indices.CopyTo (dest, index);
  1749. }
  1750. public IEnumerator GetEnumerator ()
  1751. {
  1752. return indices.GetEnumerator ();
  1753. }
  1754. int IList.Add (object obj)
  1755. {
  1756. throw new NotSupportedException ();
  1757. }
  1758. void IList.Clear ()
  1759. {
  1760. throw new NotSupportedException ();
  1761. }
  1762. bool IList.Contains (object selectedIndex)
  1763. {
  1764. return Contains ((int)selectedIndex);
  1765. }
  1766. int IList.IndexOf (object selectedIndex)
  1767. {
  1768. return IndexOf ((int) selectedIndex);
  1769. }
  1770. void IList.Insert (int index, object value)
  1771. {
  1772. throw new NotSupportedException ();
  1773. }
  1774. void IList.Remove (object value)
  1775. {
  1776. throw new NotSupportedException ();
  1777. }
  1778. void IList.RemoveAt (int index)
  1779. {
  1780. throw new NotSupportedException ();
  1781. }
  1782. object IList.this[int index]{
  1783. get {return indices[index]; }
  1784. set {throw new NotImplementedException (); }
  1785. }
  1786. public int IndexOf (int selectedIndex)
  1787. {
  1788. return indices.IndexOf (selectedIndex);
  1789. }
  1790. #endregion Public Methods
  1791. #region Private Methods
  1792. internal void AddIndex (int index)
  1793. {
  1794. indices.Add (index);
  1795. }
  1796. internal void ClearIndices ()
  1797. {
  1798. indices.Clear ();
  1799. }
  1800. internal void RemoveIndex (int index)
  1801. {
  1802. indices.Remove (index);
  1803. }
  1804. internal void ReCreate ()
  1805. {
  1806. indices.Clear ();
  1807. for (int i = 0; i < owner.Items.Count; i++) {
  1808. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1809. if (item.Selected)
  1810. indices.Add (item.Index);
  1811. }
  1812. }
  1813. #endregion Private Methods
  1814. }
  1815. /*
  1816. SelectedObjectCollection
  1817. */
  1818. public class SelectedObjectCollection : IList, ICollection, IEnumerable
  1819. {
  1820. private ListBox owner;
  1821. private ArrayList object_items = new ArrayList ();
  1822. public SelectedObjectCollection (ListBox owner)
  1823. {
  1824. this.owner = owner;
  1825. }
  1826. #region Public Properties
  1827. public int Count {
  1828. get { return object_items.Count; }
  1829. }
  1830. public bool IsReadOnly {
  1831. get { return true; }
  1832. }
  1833. [Browsable(false)]
  1834. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1835. public object this [int index] {
  1836. get {
  1837. if (index < 0 || index >= Count)
  1838. throw new ArgumentOutOfRangeException ("Index of out range");
  1839. return object_items[index];
  1840. }
  1841. set {throw new NotSupportedException ();}
  1842. }
  1843. bool ICollection.IsSynchronized {
  1844. get { return true; }
  1845. }
  1846. object ICollection.SyncRoot {
  1847. get { return this; }
  1848. }
  1849. bool IList.IsFixedSize {
  1850. get { return true; }
  1851. }
  1852. object IList.this[int index] {
  1853. get { return object_items[index]; }
  1854. set { throw new NotSupportedException (); }
  1855. }
  1856. #endregion Public Properties
  1857. #region Public Methods
  1858. public bool Contains (object selectedObject)
  1859. {
  1860. return object_items.Contains (selectedObject);
  1861. }
  1862. public void CopyTo (Array dest, int index)
  1863. {
  1864. object_items.CopyTo (dest, index);
  1865. }
  1866. int IList.Add (object value)
  1867. {
  1868. throw new NotSupportedException ();
  1869. }
  1870. void IList.Clear ()
  1871. {
  1872. throw new NotSupportedException ();
  1873. }
  1874. bool IList.Contains (object selectedIndex)
  1875. {
  1876. throw new NotImplementedException ();
  1877. }
  1878. void IList.Insert (int index, object value)
  1879. {
  1880. throw new NotSupportedException ();
  1881. }
  1882. void IList.Remove (object value)
  1883. {
  1884. throw new NotSupportedException ();
  1885. }
  1886. void IList.RemoveAt (int index)
  1887. {
  1888. throw new NotSupportedException ();
  1889. }
  1890. public int IndexOf (object item)
  1891. {
  1892. return object_items.IndexOf (item);
  1893. }
  1894. public IEnumerator GetEnumerator ()
  1895. {
  1896. return object_items.GetEnumerator ();
  1897. }
  1898. #endregion Public Methods
  1899. #region Private Methods
  1900. internal void AddObject (object obj)
  1901. {
  1902. object_items.Add (obj);
  1903. }
  1904. internal void ClearObjects ()
  1905. {
  1906. object_items.Clear ();
  1907. }
  1908. internal void ReCreate ()
  1909. {
  1910. object_items.Clear ();
  1911. for (int i = 0; i < owner.Items.Count; i++) {
  1912. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1913. if (item.Selected)
  1914. object_items.Add (owner.Items[item.Index]);
  1915. }
  1916. }
  1917. internal void RemoveObject (object obj)
  1918. {
  1919. object_items.Remove (obj);
  1920. }
  1921. #endregion Private Methods
  1922. }
  1923. }
  1924. }