ListBox.cs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  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 VScrollBar vscrollbar_ctrl;
  117. private HScrollBar 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 VScrollBar ();
  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 HScrollBar ();
  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. for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
  648. if (GetItemRectangle (i).Contains (x,y) == true)
  649. return i;
  650. }
  651. return -1;
  652. }
  653. protected override void OnChangeUICues (UICuesEventArgs e)
  654. {
  655. base.OnChangeUICues (e);
  656. }
  657. protected override void OnDataSourceChanged (EventArgs e)
  658. {
  659. base.OnDataSourceChanged (e);
  660. BindDataItems (items);
  661. if (DataSource == null || DataManager == null) {
  662. SelectedIndex = -1;
  663. }
  664. else {
  665. SelectedIndex = DataManager.Position;
  666. }
  667. }
  668. protected override void OnDisplayMemberChanged (EventArgs e)
  669. {
  670. base.OnDisplayMemberChanged (e);
  671. if (DataManager == null || !IsHandleCreated)
  672. return;
  673. BindDataItems (items);
  674. base.Refresh ();
  675. }
  676. protected virtual void OnDrawItem (DrawItemEventArgs e)
  677. {
  678. if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable)) {
  679. DrawItem (this, e);
  680. return;
  681. }
  682. ThemeEngine.Current.DrawListBoxItem (this, e);
  683. }
  684. protected override void OnFontChanged (EventArgs e)
  685. {
  686. base.OnFontChanged (e);
  687. if (!use_item_height) {
  688. listbox_info.item_height = FontHeight;
  689. RellocateScrollBars ();
  690. CalcClientArea ();
  691. UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  692. } else {
  693. base.Refresh ();
  694. }
  695. }
  696. protected override void OnHandleCreated (EventArgs e)
  697. {
  698. base.OnHandleCreated (e);
  699. UpdateInternalClientRect (ClientRectangle);
  700. SuspendLayout ();
  701. Controls.AddImplicit (vscrollbar_ctrl);
  702. Controls.AddImplicit (hscrollbar_ctrl);
  703. ResumeLayout ();
  704. UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  705. }
  706. protected override void OnHandleDestroyed (EventArgs e)
  707. {
  708. base.OnHandleDestroyed (e);
  709. }
  710. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  711. {
  712. if (draw_mode != DrawMode.OwnerDrawVariable)
  713. return;
  714. if (MeasureItem != null)
  715. MeasureItem (this, e);
  716. }
  717. protected override void OnParentChanged (EventArgs e)
  718. {
  719. base.OnParentChanged (e);
  720. }
  721. protected override void OnResize (EventArgs e)
  722. {
  723. base.OnResize (e);
  724. UpdateInternalClientRect (ClientRectangle);
  725. }
  726. protected override void OnSelectedIndexChanged (EventArgs e)
  727. {
  728. base.OnSelectedIndexChanged (e);
  729. if (SelectedIndexChanged != null)
  730. SelectedIndexChanged (this, e);
  731. }
  732. protected override void OnSelectedValueChanged (EventArgs e)
  733. {
  734. base.OnSelectedValueChanged (e);
  735. }
  736. public override void Refresh ()
  737. {
  738. if (draw_mode == DrawMode.OwnerDrawVariable) {
  739. for (int i = 0; i < Items.Count; i++) {
  740. (Items.GetListBoxItem (i)).ItemHeight = -1;
  741. }
  742. }
  743. base.Refresh ();
  744. }
  745. protected override void RefreshItem (int index)
  746. {
  747. if (index < 0 || index >= Items.Count)
  748. throw new ArgumentOutOfRangeException ("Index of out range");
  749. if (draw_mode == DrawMode.OwnerDrawVariable) {
  750. (Items.GetListBoxItem (index)).ItemHeight = -1;
  751. }
  752. }
  753. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  754. {
  755. base.SetBoundsCore (x, y, width, height, specified);
  756. }
  757. protected override void SetItemCore (int index, object value)
  758. {
  759. if (index < 0 || index >= Items.Count)
  760. return;
  761. Items[index] = value;
  762. }
  763. protected override void SetItemsCore (IList value)
  764. {
  765. BeginUpdate ();
  766. try {
  767. Items.Clear ();
  768. Items.AddRange (value);
  769. } finally {
  770. EndUpdate ();
  771. }
  772. }
  773. public void SetSelected (int index, bool value)
  774. {
  775. if (index < 0 || index >= Items.Count)
  776. throw new ArgumentOutOfRangeException ("Index of out range");
  777. if (SelectionMode == SelectionMode.None)
  778. throw new InvalidOperationException ();
  779. if (value)
  780. SelectItem (index);
  781. else
  782. UnSelectItem (index, true);
  783. }
  784. protected virtual void Sort ()
  785. {
  786. if (Items.Count == 0)
  787. return;
  788. Items.Sort ();
  789. base.Refresh ();
  790. }
  791. public override string ToString ()
  792. {
  793. return base.ToString () + ", Items Count: " + Items.Count;
  794. }
  795. protected virtual void WmReflectCommand (ref Message m)
  796. {
  797. }
  798. protected override void WndProc (ref Message m)
  799. {
  800. switch ((Msg) m.Msg) {
  801. case Msg.WM_PAINT: {
  802. PaintEventArgs paint_event;
  803. paint_event = XplatUI.PaintEventStart (Handle, true);
  804. OnPaintLB (paint_event);
  805. XplatUI.PaintEventEnd (Handle, true);
  806. return;
  807. }
  808. case Msg.WM_ERASEBKGND:
  809. m.Result = (IntPtr) 1;
  810. return;
  811. default:
  812. break;
  813. }
  814. base.WndProc (ref m);
  815. }
  816. #endregion Public Methods
  817. #region Private Methods
  818. internal void CalcClientArea ()
  819. {
  820. listbox_info.textdrawing_rect = listbox_info.client_rect;
  821. if (listbox_info.show_verticalsb)
  822. listbox_info.textdrawing_rect.Width -= vscrollbar_ctrl.Width;
  823. if (listbox_info.show_horizontalsb)
  824. listbox_info.textdrawing_rect.Height -= hscrollbar_ctrl.Height;
  825. if (DrawMode == DrawMode.OwnerDrawVariable) {
  826. int height = 0;
  827. listbox_info.page_size = 0;
  828. for (int i = 0; i < Items.Count; i++) {
  829. height += GetItemHeight (i);
  830. if (height > listbox_info.textdrawing_rect.Height)
  831. break;
  832. listbox_info.page_size++;
  833. }
  834. } else {
  835. listbox_info.page_size = listbox_info.textdrawing_rect.Height / ItemHeight;
  836. }
  837. if (listbox_info.page_size == 0) {
  838. listbox_info.page_size = 1;
  839. }
  840. /* Adjust size to visible the maximum number of displayable items */
  841. if (IntegralHeight == true) {
  842. // From MS Docs: The integral height is based on the height of the ListBox, rather than
  843. // the client area height. As a result, when the IntegralHeight property is set true,
  844. // items can still be partially shown if scroll bars are displayed.
  845. int remaining = listbox_info.client_rect.Height % listbox_info.item_height;
  846. if (remaining > 0) {
  847. listbox_info.client_rect.Height -= remaining;
  848. CalcClientArea ();
  849. RellocateScrollBars ();
  850. base.Refresh ();
  851. }
  852. }
  853. }
  854. internal void Draw (Rectangle clip, Graphics dc)
  855. {
  856. // IntegralHeight has effect, we also have to paint the unused area
  857. if (IntegralHeight) {
  858. if (ClientRectangle.Height > listbox_info.client_rect.Height) {
  859. Region area = new Region (ClientRectangle);
  860. area.Exclude (LBoxInfo.textdrawing_rect);
  861. if (listbox_info.show_horizontalsb) {
  862. area.Exclude (new Rectangle (hscrollbar_ctrl.Location.X, hscrollbar_ctrl.Location.Y,
  863. hscrollbar_ctrl.Width, hscrollbar_ctrl.Height));
  864. }
  865. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
  866. area.GetBounds (dc));
  867. area.Dispose ();
  868. }
  869. }
  870. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
  871. LBoxInfo.textdrawing_rect);
  872. if (Items.Count > 0) {
  873. Rectangle item_rect;
  874. DrawItemState state = DrawItemState.None;
  875. for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
  876. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  877. if (clip.IntersectsWith (item_rect) == false)
  878. continue;
  879. /* Draw item */
  880. state = DrawItemState.None;
  881. if ((Items.GetListBoxItem (i)).Selected) {
  882. state |= DrawItemState.Selected;
  883. }
  884. if (has_focus == true && focused_item == i)
  885. state |= DrawItemState.Focus;
  886. if (MultiColumn == false && hscrollbar_ctrl != null && hscrollbar_ctrl.Visible) {
  887. item_rect.X -= hscrollbar_ctrl.Value;
  888. item_rect.Width += hscrollbar_ctrl.Value;
  889. }
  890. OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect,
  891. i, state, ForeColor, BackColor));
  892. }
  893. }
  894. }
  895. // Converts a GetItemRectangle to a one that we can display
  896. internal Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  897. {
  898. Rectangle item_rect;
  899. Rectangle first_item_rect = GetItemRectangle (first_displayble);
  900. item_rect = GetItemRectangle (index);
  901. item_rect.X -= first_item_rect.X;
  902. item_rect.Y -= first_item_rect.Y;
  903. return item_rect;
  904. }
  905. // Value Changed
  906. private void HorizontalScrollEvent (object sender, EventArgs e)
  907. {
  908. if (!multicolumn) {
  909. base.Refresh ();
  910. return;
  911. }
  912. int top_item = LBoxInfo.top_item;
  913. int last_item = LBoxInfo.last_item;
  914. LBoxInfo.top_item = listbox_info.page_size * hscrollbar_ctrl.Value;
  915. LBoxInfo.last_item = LastVisibleItem ();
  916. if (top_item != LBoxInfo.top_item || last_item != LBoxInfo.last_item)
  917. base.Refresh ();
  918. }
  919. // Only returns visible points. The diference of with IndexFromPoint is that the rectangle
  920. // has screen coordinates
  921. internal int IndexFromPointDisplayRectangle (int x, int y)
  922. {
  923. if (Items.Count == 0)
  924. return -1;
  925. for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
  926. if (GetItemDisplayRectangle (i, LBoxInfo.top_item).Contains (x, y) == true)
  927. return i;
  928. }
  929. return -1;
  930. }
  931. private int LastVisibleItem ()
  932. {
  933. Rectangle item_rect;
  934. int top_y = LBoxInfo.textdrawing_rect.Y + LBoxInfo.textdrawing_rect.Height;
  935. int i = 0;
  936. if (LBoxInfo.top_item >= Items.Count)
  937. return LBoxInfo.top_item;
  938. for (i = LBoxInfo.top_item; i < Items.Count; i++) {
  939. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  940. if (MultiColumn) {
  941. if (item_rect.X > LBoxInfo.textdrawing_rect.Width)
  942. return i - 1;
  943. }
  944. else {
  945. if (item_rect.Y + item_rect.Height > top_y) {
  946. return i;
  947. }
  948. }
  949. }
  950. return i - 1;
  951. }
  952. private void UpdatedTopItem ()
  953. {
  954. if (multicolumn) {
  955. int col = LBoxInfo.top_item / LBoxInfo.page_size;
  956. if (col > hscrollbar_ctrl.Maximum)
  957. hscrollbar_ctrl.Value = hscrollbar_ctrl.Maximum;
  958. else
  959. hscrollbar_ctrl.Value = col;
  960. }
  961. else {
  962. if (LBoxInfo.top_item > vscrollbar_ctrl.Maximum)
  963. vscrollbar_ctrl.Value = vscrollbar_ctrl.Maximum;
  964. else
  965. vscrollbar_ctrl.Value = LBoxInfo.top_item;
  966. }
  967. }
  968. // Navigates to the indicated item and returns the new item
  969. private int NavigateItemVisually (ItemNavigation navigation)
  970. {
  971. int page_size, columns, selected_index = -1;
  972. if (multicolumn) {
  973. columns = LBoxInfo.textdrawing_rect.Width / ColumnWidthInternal;
  974. page_size = columns * LBoxInfo.page_size;
  975. if (page_size == 0) {
  976. page_size = LBoxInfo.page_size;
  977. }
  978. } else {
  979. page_size = LBoxInfo.page_size;
  980. }
  981. switch (navigation) {
  982. case ItemNavigation.PreviousColumn: {
  983. if (focused_item - LBoxInfo.page_size < 0) {
  984. return -1;
  985. }
  986. if (focused_item - LBoxInfo.page_size < LBoxInfo.top_item) {
  987. LBoxInfo.top_item = focused_item - LBoxInfo.page_size;
  988. UpdatedTopItem ();
  989. }
  990. selected_index = focused_item - LBoxInfo.page_size;
  991. break;
  992. }
  993. case ItemNavigation.NextColumn: {
  994. if (focused_item + LBoxInfo.page_size >= Items.Count) {
  995. break;
  996. }
  997. if (focused_item + LBoxInfo.page_size > LBoxInfo.last_item) {
  998. LBoxInfo.top_item = focused_item;
  999. UpdatedTopItem ();
  1000. }
  1001. selected_index = focused_item + LBoxInfo.page_size;
  1002. break;
  1003. }
  1004. case ItemNavigation.First: {
  1005. LBoxInfo.top_item = 0;
  1006. selected_index = 0;
  1007. UpdatedTopItem ();
  1008. break;
  1009. }
  1010. case ItemNavigation.Last: {
  1011. if (Items.Count < LBoxInfo.page_size) {
  1012. LBoxInfo.top_item = 0;
  1013. selected_index = Items.Count - 1;
  1014. UpdatedTopItem ();
  1015. } else {
  1016. LBoxInfo.top_item = Items.Count - LBoxInfo.page_size;
  1017. selected_index = Items.Count - 1;
  1018. UpdatedTopItem ();
  1019. }
  1020. break;
  1021. }
  1022. case ItemNavigation.Next: {
  1023. if (focused_item + 1 < Items.Count) {
  1024. int actualHeight = 0;
  1025. if (draw_mode == DrawMode.OwnerDrawVariable) {
  1026. for (int i = LBoxInfo.top_item; i <= focused_item + 1; i++)
  1027. actualHeight += GetItemHeight (i);
  1028. } else {
  1029. actualHeight = ((focused_item + 1) - LBoxInfo.top_item + 1) * ItemHeight;
  1030. }
  1031. if (actualHeight >= LBoxInfo.textdrawing_rect.Height) {
  1032. int bal = IntegralHeight ? 0 : (listbox_info.textdrawing_rect.Height == actualHeight ? 0 : 1);
  1033. if (focused_item + bal >= LBoxInfo.last_item) {
  1034. LBoxInfo.top_item++;
  1035. UpdatedTopItem ();
  1036. }
  1037. }
  1038. selected_index = focused_item + 1;
  1039. }
  1040. break;
  1041. }
  1042. case ItemNavigation.Previous: {
  1043. if (focused_item > 0) {
  1044. if (focused_item - 1 < LBoxInfo.top_item) {
  1045. LBoxInfo.top_item--;
  1046. UpdatedTopItem ();
  1047. }
  1048. selected_index = focused_item - 1;
  1049. }
  1050. break;
  1051. }
  1052. case ItemNavigation.NextPage: {
  1053. if (Items.Count < page_size) {
  1054. NavigateItemVisually (ItemNavigation.Last);
  1055. break;
  1056. }
  1057. if (focused_item + page_size - 1 >= Items.Count) {
  1058. LBoxInfo.top_item = Items.Count - page_size;
  1059. UpdatedTopItem ();
  1060. selected_index = Items.Count - 1;
  1061. }
  1062. else {
  1063. if (focused_item + page_size - 1 > LBoxInfo.last_item) {
  1064. LBoxInfo.top_item = focused_item;
  1065. UpdatedTopItem ();
  1066. }
  1067. selected_index = focused_item + page_size - 1;
  1068. }
  1069. break;
  1070. }
  1071. case ItemNavigation.PreviousPage: {
  1072. if (focused_item - (LBoxInfo.page_size - 1) <= 0) {
  1073. LBoxInfo.top_item = 0;
  1074. UpdatedTopItem ();
  1075. SelectedIndex = 0;
  1076. }
  1077. else {
  1078. if (focused_item - (LBoxInfo.page_size - 1) < LBoxInfo.top_item) {
  1079. LBoxInfo.top_item = focused_item - (LBoxInfo.page_size - 1);
  1080. UpdatedTopItem ();
  1081. }
  1082. selected_index = focused_item - (LBoxInfo.page_size - 1);
  1083. }
  1084. break;
  1085. }
  1086. default:
  1087. break;
  1088. }
  1089. return selected_index;
  1090. }
  1091. private void OnGotFocus (object sender, EventArgs e)
  1092. {
  1093. has_focus = true;
  1094. if (focused_item != -1) {
  1095. Rectangle invalidate = GetItemDisplayRectangle (focused_item, LBoxInfo.top_item);
  1096. Invalidate (invalidate);
  1097. }
  1098. }
  1099. private void OnLostFocus (object sender, EventArgs e)
  1100. {
  1101. has_focus = false;
  1102. if (focused_item != -1) {
  1103. Rectangle invalidate = GetItemDisplayRectangle (focused_item, LBoxInfo.top_item);
  1104. Invalidate (invalidate);
  1105. }
  1106. }
  1107. private void OnKeyDownLB (object sender, KeyEventArgs e)
  1108. {
  1109. int new_item = -1;
  1110. if (Items.Count == 0)
  1111. return;
  1112. switch (e.KeyCode) {
  1113. case Keys.ControlKey:
  1114. ctrl_pressed = true;
  1115. break;
  1116. case Keys.ShiftKey:
  1117. shift_pressed = true;
  1118. break;
  1119. case Keys.Home:
  1120. new_item = NavigateItemVisually (ItemNavigation.First);
  1121. break;
  1122. case Keys.End:
  1123. new_item = NavigateItemVisually (ItemNavigation.Last);
  1124. break;
  1125. case Keys.Up:
  1126. new_item = NavigateItemVisually (ItemNavigation.Previous);
  1127. break;
  1128. case Keys.Down:
  1129. new_item = NavigateItemVisually (ItemNavigation.Next);
  1130. break;
  1131. case Keys.PageUp:
  1132. new_item = NavigateItemVisually (ItemNavigation.PreviousPage);
  1133. break;
  1134. case Keys.PageDown:
  1135. new_item = NavigateItemVisually (ItemNavigation.NextPage);
  1136. break;
  1137. case Keys.Right:
  1138. if (multicolumn == true) {
  1139. new_item = NavigateItemVisually (ItemNavigation.NextColumn);
  1140. }
  1141. break;
  1142. case Keys.Left:
  1143. if (multicolumn == true) {
  1144. new_item = NavigateItemVisually (ItemNavigation.PreviousColumn);
  1145. }
  1146. break;
  1147. case Keys.Space:
  1148. if (selection_mode == SelectionMode.MultiSimple) {
  1149. SelectedItemFromNavigation (focused_item);
  1150. }
  1151. break;
  1152. default:
  1153. break;
  1154. }
  1155. if (new_item != -1) {
  1156. SetFocusedItem (new_item);
  1157. }
  1158. if (new_item != -1) {
  1159. if (selection_mode != SelectionMode.MultiSimple && selection_mode != SelectionMode.None) {
  1160. SelectedItemFromNavigation (new_item);
  1161. }
  1162. }
  1163. }
  1164. private void OnKeyUpLB (object sender, KeyEventArgs e)
  1165. {
  1166. switch (e.KeyCode) {
  1167. case Keys.ControlKey:
  1168. ctrl_pressed = false;
  1169. break;
  1170. case Keys.ShiftKey:
  1171. shift_pressed = false;
  1172. break;
  1173. default:
  1174. break;
  1175. }
  1176. }
  1177. internal virtual void OnMouseDownLB (object sender, MouseEventArgs e)
  1178. {
  1179. if (Click != null) {
  1180. if (e.Button == MouseButtons.Left) {
  1181. Click (this, e);
  1182. }
  1183. }
  1184. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  1185. if (index != -1) {
  1186. SelectedItemFromNavigation (index);
  1187. SetFocusedItem (index);
  1188. }
  1189. }
  1190. private void OnPaintLB (PaintEventArgs pevent)
  1191. {
  1192. if (Paint != null)
  1193. Paint (this, pevent);
  1194. if (suspend_ctrlupdate == true)
  1195. return;
  1196. Draw (pevent.ClipRectangle, pevent.Graphics);
  1197. }
  1198. internal void RellocateScrollBars ()
  1199. {
  1200. if (listbox_info.show_verticalsb) {
  1201. vscrollbar_ctrl.Size = new Size (vscrollbar_ctrl.Width, ClientRectangle.Height);
  1202. vscrollbar_ctrl.Location = new Point (ClientRectangle.Width - vscrollbar_ctrl.Width, 0);
  1203. }
  1204. if (listbox_info.show_horizontalsb) {
  1205. int width = listbox_info.client_rect.Width;
  1206. if (listbox_info.show_verticalsb)
  1207. width -= vscrollbar_ctrl.Width;
  1208. hscrollbar_ctrl.Size = new Size (width, hscrollbar_ctrl.Height);
  1209. hscrollbar_ctrl.Location = new Point (0, ClientRectangle.Height - hscrollbar_ctrl.Height);
  1210. }
  1211. CalcClientArea ();
  1212. }
  1213. // Add an item in the Selection array and marks it visually as selected
  1214. private void SelectItem (int index)
  1215. {
  1216. if (index == -1)
  1217. return;
  1218. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  1219. (Items.GetListBoxItem (index)).Selected = true;
  1220. selected_indices.AddIndex (index);
  1221. selected_items.AddObject (Items[index]);
  1222. if (ClientRectangle.Contains (invalidate))
  1223. Invalidate (invalidate);
  1224. }
  1225. // An item navigation operation (mouse or keyboard) has caused to select a new item
  1226. internal void SelectedItemFromNavigation (int index)
  1227. {
  1228. switch (SelectionMode) {
  1229. case SelectionMode.None: // Do nothing
  1230. break;
  1231. case SelectionMode.One: {
  1232. SelectedIndex = index;
  1233. break;
  1234. }
  1235. case SelectionMode.MultiSimple: {
  1236. if (selected_index == -1) {
  1237. SelectedIndex = index;
  1238. } else {
  1239. if ((Items.GetListBoxItem (index)).Selected) // BUG: index or selected_index?
  1240. UnSelectItem (index, true);
  1241. else {
  1242. SelectItem (index);
  1243. OnSelectedIndexChanged (new EventArgs ());
  1244. OnSelectedValueChanged (new EventArgs ());
  1245. }
  1246. }
  1247. break;
  1248. }
  1249. case SelectionMode.MultiExtended: {
  1250. if (selected_index == -1) {
  1251. SelectedIndex = index;
  1252. } else {
  1253. if (ctrl_pressed == false && shift_pressed == false) {
  1254. ClearSelected ();
  1255. }
  1256. if (shift_pressed == true) {
  1257. ShiftSelection (index);
  1258. } else { // ctrl_pressed or single item
  1259. SelectItem (index);
  1260. }
  1261. OnSelectedIndexChanged (new EventArgs ());
  1262. OnSelectedValueChanged (new EventArgs ());
  1263. }
  1264. break;
  1265. }
  1266. default:
  1267. break;
  1268. }
  1269. }
  1270. private void ShiftSelection (int index)
  1271. {
  1272. int shorter_item = -1, dist = Items.Count + 1, cur_dist;
  1273. foreach (int idx in selected_indices) {
  1274. if (idx > index) {
  1275. cur_dist = idx - index;
  1276. }
  1277. else {
  1278. cur_dist = index - idx;
  1279. }
  1280. if (cur_dist < dist) {
  1281. dist = cur_dist;
  1282. shorter_item = idx;
  1283. }
  1284. }
  1285. if (shorter_item != -1) {
  1286. int start, end;
  1287. if (shorter_item > index) {
  1288. start = index;
  1289. end = shorter_item;
  1290. } else {
  1291. start = shorter_item;
  1292. end = index;
  1293. }
  1294. ClearSelected ();
  1295. for (int idx = start; idx <= end; idx++) {
  1296. SelectItem (idx);
  1297. }
  1298. }
  1299. }
  1300. internal void SetFocusedItem (int index)
  1301. {
  1302. Rectangle invalidate;
  1303. int prev = focused_item;
  1304. focused_item = index;
  1305. if (has_focus == false)
  1306. return;
  1307. if (prev != -1) { // Invalidates previous item
  1308. invalidate = GetItemDisplayRectangle (prev, LBoxInfo.top_item);
  1309. Invalidate (invalidate);
  1310. }
  1311. if (index != -1) {
  1312. invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  1313. Invalidate (invalidate);
  1314. }
  1315. }
  1316. // Removes an item in the Selection array and marks it visually as unselected
  1317. private void UnSelectItem (int index, bool remove)
  1318. {
  1319. if (index == -1)
  1320. return;
  1321. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  1322. (Items.GetListBoxItem (index)).Selected = false;
  1323. if (remove) {
  1324. selected_indices.RemoveIndex (index);
  1325. selected_items.RemoveObject (Items[index]);
  1326. }
  1327. if (ClientRectangle.Contains (invalidate))
  1328. Invalidate (invalidate);
  1329. }
  1330. internal StringFormat GetFormatString ()
  1331. {
  1332. StringFormat string_format = new StringFormat ();
  1333. if (RightToLeft == RightToLeft.Yes)
  1334. string_format.Alignment = StringAlignment.Far;
  1335. else
  1336. string_format.Alignment = StringAlignment.Near;
  1337. if (UseTabStops)
  1338. string_format.SetTabStops (0, new float [] {(float)(Font.Height * 3.7)});
  1339. return string_format;
  1340. }
  1341. // Updates the scrollbar's position with the new items and inside area
  1342. internal virtual void UpdateItemInfo (UpdateOperation operation, int first, int last)
  1343. {
  1344. if (!IsHandleCreated || suspend_ctrlupdate == true)
  1345. return;
  1346. UpdateShowVerticalScrollBar ();
  1347. if (listbox_info.show_verticalsb && Items.Count > listbox_info.page_size)
  1348. if (vscrollbar_ctrl.Enabled)
  1349. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  1350. if (listbox_info.show_horizontalsb) {
  1351. if (MultiColumn) {
  1352. int fullpage = (listbox_info.page_size * (listbox_info.client_rect.Width / ColumnWidthInternal));
  1353. if (hscrollbar_ctrl.Enabled && listbox_info.page_size > 0)
  1354. hscrollbar_ctrl.Maximum = Math.Max (0, 1 + ((Items.Count - fullpage) / listbox_info.page_size));
  1355. }
  1356. }
  1357. if (MultiColumn == false) {
  1358. /* Calc the longest items for non multicolumn listboxes */
  1359. if (operation == UpdateOperation.AllItems || operation == UpdateOperation.DeleteItems) {
  1360. SizeF size;
  1361. for (int i = 0; i < Items.Count; i++) {
  1362. size = DeviceContext.MeasureString (GetItemText (Items[i]), Font);
  1363. if ((int) size.Width > listbox_info.max_itemwidth)
  1364. listbox_info.max_itemwidth = (int) size.Width;
  1365. }
  1366. }
  1367. else {
  1368. if (operation == UpdateOperation.AddItems) {
  1369. SizeF size;
  1370. for (int i = first; i < last + 1; i++) {
  1371. size = DeviceContext.MeasureString (GetItemText (Items[i]), Font);
  1372. if ((int) size.Width > listbox_info.max_itemwidth)
  1373. listbox_info.max_itemwidth = (int) size.Width;
  1374. }
  1375. }
  1376. }
  1377. }
  1378. if (sorted)
  1379. Sort ();
  1380. if (Items.Count == 0) {
  1381. selected_index = -1;
  1382. focused_item = -1;
  1383. }
  1384. SelectedItems.ReCreate ();
  1385. SelectedIndices.ReCreate ();
  1386. UpdateShowHorizontalScrollBar ();
  1387. LBoxInfo.last_item = LastVisibleItem ();
  1388. base.Refresh ();
  1389. }
  1390. private void UpdateInternalClientRect (Rectangle client_rectangle)
  1391. {
  1392. listbox_info.client_rect = client_rectangle;
  1393. UpdateShowHorizontalScrollBar ();
  1394. UpdateShowVerticalScrollBar ();
  1395. RellocateScrollBars ();
  1396. UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  1397. }
  1398. /* Determines if the horizontal scrollbar has to be displyed */
  1399. private void UpdateShowHorizontalScrollBar ()
  1400. {
  1401. bool show = false;
  1402. bool enabled = true;
  1403. if (MultiColumn) { /* Horizontal scrollbar is always shown in Multicolum mode */
  1404. /* Is it really need it */
  1405. int page_size = listbox_info.client_rect.Height / listbox_info.item_height;
  1406. int fullpage = (page_size * (listbox_info.textdrawing_rect.Height / ColumnWidthInternal));
  1407. if (Items.Count > fullpage) {
  1408. show = true;
  1409. }
  1410. else { /* Acording to MS Documentation ScrollAlwaysVisible only affects Horizontal scrollbars but
  1411. this is not true for MultiColumn listboxes */
  1412. if (ScrollAlwaysVisible == true) {
  1413. enabled = false;
  1414. show = true;
  1415. }
  1416. }
  1417. } else { /* If large item*/
  1418. if (listbox_info.max_itemwidth > listbox_info.client_rect.Width && HorizontalScrollbar) {
  1419. show = true;
  1420. hscrollbar_ctrl.Maximum = listbox_info.max_itemwidth;
  1421. hscrollbar_ctrl.LargeChange = listbox_info.textdrawing_rect.Width;
  1422. }
  1423. }
  1424. if (hscrollbar_ctrl.Enabled != enabled)
  1425. hscrollbar_ctrl.Enabled = enabled;
  1426. if (listbox_info.show_horizontalsb == show)
  1427. return;
  1428. listbox_info.show_horizontalsb = show;
  1429. hscrollbar_ctrl.Visible = show;
  1430. if (show == true) {
  1431. RellocateScrollBars ();
  1432. }
  1433. CalcClientArea ();
  1434. }
  1435. /* Determines if the vertical scrollbar has to be displyed */
  1436. private void UpdateShowVerticalScrollBar ()
  1437. {
  1438. bool show = false;
  1439. bool enabled = true;
  1440. if (!MultiColumn) { /* Vertical scrollbar is never shown in Multicolum mode */
  1441. if (Items.Count > listbox_info.page_size) {
  1442. show = true;
  1443. }
  1444. else
  1445. if (ScrollAlwaysVisible) {
  1446. show = true;
  1447. enabled = false;
  1448. }
  1449. }
  1450. if (vscrollbar_ctrl.Enabled != enabled)
  1451. vscrollbar_ctrl.Enabled = enabled;
  1452. if (listbox_info.show_verticalsb == show)
  1453. return;
  1454. listbox_info.show_verticalsb = show;
  1455. vscrollbar_ctrl.Visible = show;
  1456. if (show == true) {
  1457. if (vscrollbar_ctrl.Enabled)
  1458. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  1459. RellocateScrollBars ();
  1460. } else if (vscrollbar_ctrl.Maximum > 0) {
  1461. vscrollbar_ctrl.Maximum = 0;
  1462. }
  1463. CalcClientArea ();
  1464. }
  1465. // Value Changed
  1466. private void VerticalScrollEvent (object sender, EventArgs e)
  1467. {
  1468. int top_item = LBoxInfo.top_item;
  1469. int last_item = LBoxInfo.last_item;
  1470. LBoxInfo.top_item = /*listbox_info.page_size + */ vscrollbar_ctrl.Value;
  1471. LBoxInfo.last_item = LastVisibleItem ();
  1472. if (top_item != LBoxInfo.top_item || last_item != LBoxInfo.last_item)
  1473. base.Refresh ();
  1474. }
  1475. #endregion Private Methods
  1476. /*
  1477. ListBox.ObjectCollection
  1478. */
  1479. [ListBindable (false)]
  1480. public class ObjectCollection : IList, ICollection, IEnumerable
  1481. {
  1482. // Compare objects
  1483. internal class ListObjectComparer : IComparer
  1484. {
  1485. private ListBox owner;
  1486. public ListObjectComparer (ListBox owner)
  1487. {
  1488. this.owner = owner;
  1489. }
  1490. public int Compare (object a, object b)
  1491. {
  1492. string str1 = a.ToString ();
  1493. string str2 = b.ToString ();
  1494. return str1.CompareTo (str2);
  1495. }
  1496. }
  1497. // Compare ListItem
  1498. internal class ListItemComparer : IComparer
  1499. {
  1500. private ListBox owner;
  1501. public ListItemComparer (ListBox owner)
  1502. {
  1503. this.owner = owner;
  1504. }
  1505. public int Compare (object a, object b)
  1506. {
  1507. int index1 = ((ListBox.ListBoxItem) (a)).Index;
  1508. int index2 = ((ListBox.ListBoxItem) (b)).Index;
  1509. string str1 = owner.GetItemText (owner.Items[index1]);
  1510. string str2 = owner.GetItemText (owner.Items[index2]);
  1511. return str1.CompareTo (str2);
  1512. }
  1513. }
  1514. private ListBox owner;
  1515. internal ArrayList object_items = new ArrayList ();
  1516. internal ArrayList listbox_items = new ArrayList ();
  1517. public ObjectCollection (ListBox owner)
  1518. {
  1519. this.owner = owner;
  1520. }
  1521. public ObjectCollection (ListBox owner, object[] obj)
  1522. {
  1523. this.owner = owner;
  1524. AddRange (obj);
  1525. }
  1526. public ObjectCollection (ListBox owner, ObjectCollection obj)
  1527. {
  1528. this.owner = owner;
  1529. AddRange (obj);
  1530. }
  1531. #region Public Properties
  1532. public virtual int Count {
  1533. get { return object_items.Count; }
  1534. }
  1535. public virtual bool IsReadOnly {
  1536. get { return false; }
  1537. }
  1538. [Browsable(false)]
  1539. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1540. public virtual object this [int index] {
  1541. get {
  1542. if (index < 0 || index >= Count)
  1543. throw new ArgumentOutOfRangeException ("Index of out range");
  1544. return object_items[index];
  1545. }
  1546. set {
  1547. if (index < 0 || index >= Count)
  1548. throw new ArgumentOutOfRangeException ("Index of out range");
  1549. object_items[index] = value;
  1550. }
  1551. }
  1552. bool ICollection.IsSynchronized {
  1553. get { return false; }
  1554. }
  1555. object ICollection.SyncRoot {
  1556. get { return this; }
  1557. }
  1558. bool IList.IsFixedSize {
  1559. get { return false; }
  1560. }
  1561. #endregion Public Properties
  1562. #region Private Properties
  1563. internal ArrayList ObjectItems {
  1564. get { return object_items;}
  1565. set {
  1566. object_items = value;
  1567. }
  1568. }
  1569. internal ArrayList ListBoxItems {
  1570. get { return listbox_items;}
  1571. set {
  1572. listbox_items = value;
  1573. }
  1574. }
  1575. #endregion Private Properties
  1576. #region Public Methods
  1577. public int Add (object item)
  1578. {
  1579. int idx;
  1580. idx = AddItem (item);
  1581. owner.UpdateItemInfo (UpdateOperation.AddItems, idx, idx);
  1582. return idx;
  1583. }
  1584. public void AddRange (object[] items)
  1585. {
  1586. int cnt = Count;
  1587. foreach (object mi in items)
  1588. AddItem (mi);
  1589. owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, Count - 1);
  1590. }
  1591. public void AddRange (ObjectCollection col)
  1592. {
  1593. int cnt = Count;
  1594. foreach (object mi in col)
  1595. AddItem (mi);
  1596. owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, Count - 1);
  1597. }
  1598. internal void AddRange (IList list)
  1599. {
  1600. int cnt = Count;
  1601. foreach (object mi in list)
  1602. AddItem (mi);
  1603. owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, Count - 1);
  1604. }
  1605. public virtual void Clear ()
  1606. {
  1607. owner.selected_index = -1;
  1608. owner.focused_item = -1;
  1609. object_items.Clear ();
  1610. listbox_items.Clear ();
  1611. owner.UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
  1612. }
  1613. public virtual bool Contains (object obj)
  1614. {
  1615. return object_items.Contains (obj);
  1616. }
  1617. public void CopyTo (object[] dest, int arrayIndex)
  1618. {
  1619. object_items.CopyTo (dest, arrayIndex);
  1620. }
  1621. void ICollection.CopyTo (Array dest, int index)
  1622. {
  1623. object_items.CopyTo (dest, index);
  1624. }
  1625. public virtual IEnumerator GetEnumerator ()
  1626. {
  1627. return object_items.GetEnumerator ();
  1628. }
  1629. int IList.Add (object item)
  1630. {
  1631. return Add (item);
  1632. }
  1633. public virtual int IndexOf (object value)
  1634. {
  1635. return object_items.IndexOf (value);
  1636. }
  1637. public virtual void Insert (int index, object item)
  1638. {
  1639. if (index < 0 || index > Count)
  1640. throw new ArgumentOutOfRangeException ("Index of out range");
  1641. int idx;
  1642. ObjectCollection new_items = new ObjectCollection (owner);
  1643. owner.BeginUpdate ();
  1644. for (int i = 0; i < index; i++) {
  1645. idx = new_items.AddItem (ObjectItems[i]);
  1646. (new_items.GetListBoxItem (idx)).CopyState (GetListBoxItem (i));
  1647. }
  1648. new_items.AddItem (item);
  1649. for (int i = index; i < Count; i++){
  1650. idx = new_items.AddItem (ObjectItems[i]);
  1651. (new_items.GetListBoxItem (idx)).CopyState (GetListBoxItem (i));
  1652. }
  1653. ObjectItems = new_items.ObjectItems;
  1654. ListBoxItems = new_items.ListBoxItems;
  1655. owner.EndUpdate (); // Calls UpdateItemInfo
  1656. }
  1657. public virtual void Remove (object value)
  1658. {
  1659. RemoveAt (IndexOf (value));
  1660. }
  1661. public virtual void RemoveAt (int index)
  1662. {
  1663. if (index < 0 || index >= Count)
  1664. throw new ArgumentOutOfRangeException ("Index of out range");
  1665. object_items.RemoveAt (index);
  1666. listbox_items.RemoveAt (index);
  1667. owner.UpdateItemInfo (UpdateOperation.DeleteItems, index, index);
  1668. }
  1669. #endregion Public Methods
  1670. #region Private Methods
  1671. internal int AddItem (object item)
  1672. {
  1673. int cnt = object_items.Count;
  1674. object_items.Add (item);
  1675. listbox_items.Add (new ListBox.ListBoxItem (cnt));
  1676. return cnt;
  1677. }
  1678. internal ListBox.ListBoxItem GetListBoxItem (int index)
  1679. {
  1680. if (index < 0 || index >= Count)
  1681. throw new ArgumentOutOfRangeException ("Index of out range");
  1682. return (ListBox.ListBoxItem) listbox_items[index];
  1683. }
  1684. internal void SetListBoxItem (ListBox.ListBoxItem item, int index)
  1685. {
  1686. if (index < 0 || index >= Count)
  1687. throw new ArgumentOutOfRangeException ("Index of out range");
  1688. listbox_items[index] = item;
  1689. }
  1690. internal void Sort ()
  1691. {
  1692. /* Keep this order */
  1693. listbox_items.Sort (new ListItemComparer (owner));
  1694. object_items.Sort (new ListObjectComparer (owner));
  1695. for (int i = 0; i < listbox_items.Count; i++) {
  1696. ListBox.ListBoxItem item = GetListBoxItem (i);
  1697. item.Index = i;
  1698. }
  1699. }
  1700. #endregion Private Methods
  1701. }
  1702. /*
  1703. ListBox.SelectedIndexCollection
  1704. */
  1705. public class SelectedIndexCollection : IList, ICollection, IEnumerable
  1706. {
  1707. private ListBox owner;
  1708. private ArrayList indices = new ArrayList ();
  1709. public SelectedIndexCollection (ListBox owner)
  1710. {
  1711. this.owner = owner;
  1712. }
  1713. #region Public Properties
  1714. [Browsable (false)]
  1715. public virtual int Count {
  1716. get { return indices.Count; }
  1717. }
  1718. public virtual bool IsReadOnly {
  1719. get { return true; }
  1720. }
  1721. public int this [int index] {
  1722. get {
  1723. if (index < 0 || index >= Count)
  1724. throw new ArgumentOutOfRangeException ("Index of out range");
  1725. return (int) indices[index];
  1726. }
  1727. }
  1728. bool ICollection.IsSynchronized {
  1729. get { return true; }
  1730. }
  1731. bool IList.IsFixedSize{
  1732. get { return true; }
  1733. }
  1734. object ICollection.SyncRoot {
  1735. get { return this; }
  1736. }
  1737. #endregion Public Properties
  1738. #region Public Methods
  1739. public bool Contains (int selectedIndex)
  1740. {
  1741. return indices.Contains (selectedIndex);
  1742. }
  1743. public virtual void CopyTo (Array dest, int index)
  1744. {
  1745. indices.CopyTo (dest, index);
  1746. }
  1747. public virtual IEnumerator GetEnumerator ()
  1748. {
  1749. return indices.GetEnumerator ();
  1750. }
  1751. int IList.Add (object obj)
  1752. {
  1753. throw new NotSupportedException ();
  1754. }
  1755. void IList.Clear ()
  1756. {
  1757. throw new NotSupportedException ();
  1758. }
  1759. bool IList.Contains (object selectedIndex)
  1760. {
  1761. return Contains ((int)selectedIndex);
  1762. }
  1763. int IList.IndexOf (object selectedIndex)
  1764. {
  1765. return IndexOf ((int) selectedIndex);
  1766. }
  1767. void IList.Insert (int index, object value)
  1768. {
  1769. throw new NotSupportedException ();
  1770. }
  1771. void IList.Remove (object value)
  1772. {
  1773. throw new NotSupportedException ();
  1774. }
  1775. void IList.RemoveAt (int index)
  1776. {
  1777. throw new NotSupportedException ();
  1778. }
  1779. object IList.this[int index]{
  1780. get {return indices[index]; }
  1781. set {throw new NotImplementedException (); }
  1782. }
  1783. public int IndexOf (int selectedIndex)
  1784. {
  1785. return indices.IndexOf (selectedIndex);
  1786. }
  1787. #endregion Public Methods
  1788. #region Private Methods
  1789. internal void AddIndex (int index)
  1790. {
  1791. indices.Add (index);
  1792. }
  1793. internal void ClearIndices ()
  1794. {
  1795. indices.Clear ();
  1796. }
  1797. internal void RemoveIndex (int index)
  1798. {
  1799. indices.Remove (index);
  1800. }
  1801. internal void ReCreate ()
  1802. {
  1803. indices.Clear ();
  1804. for (int i = 0; i < owner.Items.Count; i++) {
  1805. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1806. if (item.Selected)
  1807. indices.Add (item.Index);
  1808. }
  1809. }
  1810. #endregion Private Methods
  1811. }
  1812. /*
  1813. SelectedObjectCollection
  1814. */
  1815. public class SelectedObjectCollection : IList, ICollection, IEnumerable
  1816. {
  1817. private ListBox owner;
  1818. private ArrayList object_items = new ArrayList ();
  1819. public SelectedObjectCollection (ListBox owner)
  1820. {
  1821. this.owner = owner;
  1822. }
  1823. #region Public Properties
  1824. public virtual int Count {
  1825. get { return object_items.Count; }
  1826. }
  1827. public virtual bool IsReadOnly {
  1828. get { return true; }
  1829. }
  1830. [Browsable(false)]
  1831. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1832. public virtual object this [int index] {
  1833. get {
  1834. if (index < 0 || index >= Count)
  1835. throw new ArgumentOutOfRangeException ("Index of out range");
  1836. return object_items[index];
  1837. }
  1838. set {throw new NotSupportedException ();}
  1839. }
  1840. bool ICollection.IsSynchronized {
  1841. get { return true; }
  1842. }
  1843. object ICollection.SyncRoot {
  1844. get { return this; }
  1845. }
  1846. bool IList.IsFixedSize {
  1847. get { return true; }
  1848. }
  1849. object IList.this[int index] {
  1850. get { return object_items[index]; }
  1851. set { throw new NotSupportedException (); }
  1852. }
  1853. #endregion Public Properties
  1854. #region Public Methods
  1855. public virtual bool Contains (object selectedObject)
  1856. {
  1857. return object_items.Contains (selectedObject);
  1858. }
  1859. public virtual void CopyTo (Array dest, int index)
  1860. {
  1861. object_items.CopyTo (dest, index);
  1862. }
  1863. int IList.Add (object value)
  1864. {
  1865. throw new NotSupportedException ();
  1866. }
  1867. void IList.Clear ()
  1868. {
  1869. throw new NotSupportedException ();
  1870. }
  1871. bool IList.Contains (object selectedIndex)
  1872. {
  1873. throw new NotImplementedException ();
  1874. }
  1875. void IList.Insert (int index, object value)
  1876. {
  1877. throw new NotSupportedException ();
  1878. }
  1879. void IList.Remove (object value)
  1880. {
  1881. throw new NotSupportedException ();
  1882. }
  1883. void IList.RemoveAt (int index)
  1884. {
  1885. throw new NotSupportedException ();
  1886. }
  1887. public int IndexOf (object item)
  1888. {
  1889. return object_items.IndexOf (item);
  1890. }
  1891. public virtual IEnumerator GetEnumerator ()
  1892. {
  1893. return object_items.GetEnumerator ();
  1894. }
  1895. #endregion Public Methods
  1896. #region Private Methods
  1897. internal void AddObject (object obj)
  1898. {
  1899. object_items.Add (obj);
  1900. }
  1901. internal void ClearObjects ()
  1902. {
  1903. object_items.Clear ();
  1904. }
  1905. internal void ReCreate ()
  1906. {
  1907. object_items.Clear ();
  1908. for (int i = 0; i < owner.Items.Count; i++) {
  1909. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1910. if (item.Selected)
  1911. object_items.Add (owner.Items[item.Index]);
  1912. }
  1913. }
  1914. internal void RemoveObject (object obj)
  1915. {
  1916. object_items.Remove (obj);
  1917. }
  1918. #endregion Private Methods
  1919. }
  1920. }
  1921. }