Экспертная система Delphi.int.ru

Сообщество программистов
Общение, помощь, обмен опытом

Логин:
Пароль:
Регистрация | Забыли пароль?

Delphi.int.ru Expert

Другие разделы портала

Переход к вопросу:

#   

Статистика за сегодня:  


Лучшие эксперты

Подробнее »



Вопрос # 2 682

/ вопрос открыт /

Здравствуйте, уважаемые эксперты!
Помогите написать код для кнопок >,>>, <, <<, расположенных между двумя списками ListBox. ListBox1 содержит несколько строк, с помощью кнопки >> нужно переместить все строки в ListBox2, а кнопкой > только одну выделенную, причем после перемещения ListBox1 должен очистится

Наталья В. Вопрос ожидает решения (принимаются ответы, доступен мини-форум)

Вопрос задала: Наталья В. (статус: Посетитель)
Вопрос отправлен: 25 апреля 2009, 19:04
Состояние вопроса: открыт, ответов: 2.

Ответ #1. Отвечает эксперт: Вадим К

Здравствуйте, Fonzerelli!
Начнем с кнопок >> и <<. Это очень просто. всего две строки.
Что бы перенести с первого в второй строки пишем так

  ListBox2.Items.AddStrings(ListBox1.Items);
  ListBox1.Items.Clear;
Первой строкой мы копируем строки, а второй удаляем их из исходного места. Логично, что что бы перенести обратно, надо поменять 1 на 2 и наоборот.
Теперь по поводу переноса одной строки.
var ind:integer;
begin
  ind := ListBox1.ItemIndex;
  if ind = -1 then begin
     ShowMessage('Нет выделенных элементов');
     exit;
  end;
  ListBox2.Items.Add(ListBox1.Items[ind]);
  ListBox1.Items.Delete(ind);
end;
Вначале мы проверяем, что есть выделенный элемент. Если он есть, то добавляем его в конец второго списка и удаляем с первого.

Ответ отправил: Вадим К (статус: Академик)
Время отправки: 25 апреля 2009, 19:47
Оценка за ответ: 5

Ответ #2. Отвечает эксперт: min@y™

Прошу прощения, меня не было в интернете 4 дня. У меня были на то свои причины, но это к делу не относится. Все письма с вопросами я просмотрел, на некоторые ответил. Хочу и к этому вопросу добавить немного информации. Прицепляю сюда модуль с формой, на которой как раз 2 TListBox и кнопки <- и ->. Кнопок >> и << там нет, зато есть Drag&Drop между обоими листбоксами. Я думаю, это может пригодиться.

Приложение:
  1. object ExpLinksForm: TExpLinksForm
  2. Left = 398
  3. Top = 283
  4. BorderStyle = bsDialog
  5. Caption = #1069#1090#1072#1083#1086#1085#1085#1099#1077' '#1089#1074#1103#1079#1080
  6. ClientHeight = 353
  7. ClientWidth = 385
  8. Color = clBtnFace
  9. Font.Charset = DEFAULT_CHARSET
  10. Font.Color = clWindowText
  11. Font.Height = -11
  12. Font.Name = 'Tahoma'
  13. Font.Style = []
  14. KeyPreview = True
  15. OldCreateOrder = False
  16. Scaled = False
  17. DesignSize = (
  18. 385
  19. 353)
  20. PixelsPerInch = 96
  21. TextHeight = 13
  22. object Label1: TLabel
  23. Left = 8
  24. Top = 8
  25. Width = 92
  26. Height = 13
  27. Caption = #1069#1090#1072#1083#1086#1085#1085#1099#1077' '#1089#1074#1103#1079#1080':'
  28. end
  29. object Label2: TLabel
  30. Left = 216
  31. Top = 8
  32. Width = 116
  33. Height = 13
  34. Anchors = [akTop, akRight]
  35. Caption = #1050#1086#1085#1090#1072#1082#1090#1099' '#1091#1089#1090#1088#1086#1081#1089#1090#1074#1072':'
  36. end
  37. object ExpLinksListBox: TListBox
  38. Left = 8
  39. Top = 24
  40. Width = 161
  41. Height = 289
  42. Anchors = [akLeft, akTop, akBottom]
  43. DragMode = dmAutomatic
  44. ItemHeight = 13
  45. MultiSelect = True
  46. TabOrder = 0
  47. OnDragDrop = ExpLinksListBoxDragDrop
  48. OnDragOver = ExpLinksListBoxDragOver
  49. end
  50. object AddButton: TButton
  51. Tag = 10
  52. Left = 176
  53. Top = 144
  54. Width = 33
  55. Height = 25
  56. Anchors = [akLeft, akTop, akRight]
  57. Caption = #1079
  58. Font.Charset = DEFAULT_CHARSET
  59. Font.Color = clWindowText
  60. Font.Height = -11
  61. Font.Name = 'Wingdings'
  62. Font.Style = []
  63. ParentFont = False
  64. TabOrder = 1
  65. OnClick = ButtonClick
  66. end
  67. object DeleteButton: TButton
  68. Tag = 11
  69. Left = 176
  70. Top = 176
  71. Width = 33
  72. Height = 25
  73. Anchors = [akLeft, akTop, akRight]
  74. Caption = #1080
  75. Font.Charset = DEFAULT_CHARSET
  76. Font.Color = clWindowText
  77. Font.Height = -11
  78. Font.Name = 'Wingdings'
  79. Font.Style = []
  80. ParentFont = False
  81. TabOrder = 2
  82. OnClick = ButtonClick
  83. end
  84. object PinsListBox: TListBox
  85. Left = 216
  86. Top = 24
  87. Width = 161
  88. Height = 289
  89. Anchors = [akTop, akRight, akBottom]
  90. DragMode = dmAutomatic
  91. ItemHeight = 13
  92. MultiSelect = True
  93. TabOrder = 3
  94. OnDragDrop = PinsListBoxDragDrop
  95. OnDragOver = PinsListBoxDragOver
  96. end
  97. object OkButton: TButton
  98. Left = 223
  99. Top = 321
  100. Width = 75
  101. Height = 25
  102. Anchors = [akRight, akBottom]
  103. Caption = 'OK'
  104. TabOrder = 4
  105. OnClick = ButtonClick
  106. end
  107. object CancelButton: TButton
  108. Tag = 1
  109. Left = 303
  110. Top = 321
  111. Width = 75
  112. Height = 25
  113. Anchors = [akRight, akBottom]
  114. Cancel = True
  115. Caption = #1054#1090#1084#1077#1085#1072
  116. ModalResult = 2
  117. TabOrder = 5
  118. end
  119. end
  120.  
  121.  
  122.  
  123. unit uExpLinksForm;
  124.  
  125. interface
  126.  
  127. uses
  128.  
  129. uConsts, uDevPlugs, uSettings, uTypes,
  130.  
  131. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  132. Dialogs, StdCtrls;
  133.  
  134. type
  135. TExpLinksForm = class(TProjectCustomForm)
  136. ExpLinksListBox: TListBox;
  137. Label1: TLabel;
  138. AddButton: TButton;
  139. DeleteButton: TButton;
  140. PinsListBox: TListBox;
  141. OkButton: TButton;
  142. CancelButton: TButton;
  143. Label2: TLabel;
  144. procedure ButtonClick(Sender: TObject);
  145. procedure ExpLinksListBoxDragOver(Sender, Source: TObject; X,
  146. Y: Integer; State: TDragState; var Accept: Boolean);
  147. procedure ExpLinksListBoxDragDrop(Sender, Source: TObject; X,
  148. Y: Integer);
  149. procedure PinsListBoxDragOver(Sender, Source: TObject; X, Y: Integer;
  150. State: TDragState; var Accept: Boolean);
  151. procedure PinsListBoxDragDrop(Sender, Source: TObject; X, Y: Integer);
  152. private
  153.  
  154.  
  155. public
  156. Map: TDevPlugMap;
  157.  
  158.  
  159.  
  160. end;
  161.  
  162. var
  163. ExpLinksForm: TExpLinksForm;
  164.  
  165.  
  166. function ChangeExpLinks(APin: TDevPin): Boolean;
  167.  
  168. implementation
  169.  
  170. uses uLogForm;
  171.  
  172. {$R *.dfm}
  173.  
  174. procedure AddLink(const AAddress: Cardinal; ALinks: TList);
  175. begin
  176.  
  177. if ALinks.IndexOf(Pointer(AAddress)) = -1
  178. then ALinks.Add(Pointer(AAddress));
  179. end;
  180.  
  181. function ChangeExpLinks(APin: TDevPin): Boolean;
  182. var
  183. ALinks: TList;
  184. Index: Integer;
  185. //LinkIndex: Integer;
  186. TempPin: TDevPin;
  187. begin
  188. if not Assigned(ExpLinksForm)
  189. then ExpLinksForm:= TExpLinksForm.Create(Application);
  190.  
  191. ALinks:= TList.Create();
  192. try
  193. with ExpLinksForm do
  194. begin
  195. Map:= APin.OwnerPlug.OwnerPlugMap;
  196.  
  197.  
  198.  
  199.  
  200. Result:= ShowModal() = mrOk;
  201.  
  202. if Result
  203. then begin
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. for Index:= 0 to ExpLinksListBox.Count - 1 do
  212. begin
  213. TempPin:= TDevPin(ExpLinksListBox.Items.Objects[Index]);
  214.  
  215.  
  216.  
  217.  
  218. //for LinkIndex:= 0 to TempPin.ExpectedLinksCount - 1 do
  219. // AddLink(TempPin.ExpectedLinks[LinkIndex], ALinks);
  220. end;
  221.  
  222.  
  223. AddLinks2Log(ALinks);
  224. Map.ClearExpectedLinks(APin);
  225. Map.AddExpectedLinks(ALinks);
  226. //APin.AddExpectedLinks(ALinks);
  227. end;
  228. end;
  229. finally
  230. ALinks.Free();
  231. FreeAndNil(ExpLinksForm);
  232. end;
  233. end;
  234.  
  235. { TExpLinksForm }
  236.  
  237. procedure TExpLinksForm.ApplicationIdleProc;
  238. begin
  239.  
  240. inherited;
  241. AddButton.Enabled:= PinsListBox.SelCount <> 0;
  242. DeleteButton.Enabled:= ExpLinksListBox.SelCount <> 0;
  243. end;
  244.  
  245. procedure TExpLinksForm.ApplySettingsProc;
  246. begin
  247.  
  248. inherited;
  249. end;
  250.  
  251. procedure TExpLinksForm.FillExpLinks(APin: TDevPin);
  252. var
  253. Index, LinkIndex: Integer;
  254. ALinks: TList;
  255. Pin: TDevPin;
  256. begin
  257.  
  258. ExpLinksListBox.Items.BeginUpdate();
  259. ALinks:= TList.Create();
  260. try
  261. ExpLinksListBox.Clear();
  262.  
  263.  
  264. Map.LinkAddress2Pins(APin.ExpectedLinks[Index], ALinks);
  265.  
  266.  
  267.  
  268.  
  269. begin
  270. Pin:= TDevPin(ALinks[LinkIndex]);
  271. ExpLinksListBox.AddItem(Pin.SchemeName, Pin);
  272. end;
  273. finally
  274. ExpLinksListBox.Items.EndUpdate();
  275. ALinks.Free();
  276. end;
  277. end;
  278.  
  279. procedure TExpLinksForm.FillPins(APin: TDevPin);
  280. var
  281. PlugIndex, PinIndex: Integer;
  282. TempPin: TDevPin;
  283. begin
  284.  
  285.  
  286. PinsListBox.Items.BeginUpdate();
  287. try
  288. PinsListBox.Clear();
  289.  
  290.  
  291.  
  292. begin
  293. TempPin:= Map.Pins[PlugIndex, PinIndex];
  294.  
  295.  
  296.  
  297. if APin.ExpectedLinksList.IndexOf(Pointer(TempPin.Address)) <> -1
  298. then Continue;
  299.  
  300.  
  301.  
  302. if TempPin = APin
  303. then Continue;
  304.  
  305. PinsListBox.AddItem(TempPin.SchemeName, TempPin);
  306. end;
  307. finally
  308. PinsListBox.Items.EndUpdate();
  309. end;
  310. end;
  311.  
  312. procedure TExpLinksForm.ButtonClick(Sender: TObject);
  313. var
  314. Index: Integer;
  315. begin
  316.  
  317. case TControl(Sender).Tag of
  318.  
  319. 0: ModalResult:= mrOk;
  320.  
  321. // "<--"
  322. 10: begin
  323. for Index:= 0 to PinsListBox.Count - 1 do
  324. if PinsListBox.Selected[Index]
  325. then ExpLinksListBox.AddItem(PinsListBox.Items[Index], PinsListBox.Items.Objects[Index]);
  326.  
  327. PinsListBox.DeleteSelected();
  328. //ExpLinksListBox.Items.C
  329. end;
  330. // "-->"
  331. 11: begin
  332. for Index:= 0 to ExpLinksListBox.Count - 1 do
  333. if ExpLinksListBox.Selected[Index]
  334. then PinsListBox.AddItem(ExpLinksListBox.Items[Index], ExpLinksListBox.Items.Objects[Index]);
  335.  
  336. ExpLinksListBox.DeleteSelected();
  337. end;
  338. end;
  339. end;
  340.  
  341. procedure TExpLinksForm.ExpLinksListBoxDragOver(Sender, Source: TObject; X,
  342. Y: Integer; State: TDragState; var Accept: Boolean);
  343. begin
  344. Accept:= Source = PinsListBox;
  345. end;
  346.  
  347. procedure TExpLinksForm.ExpLinksListBoxDragDrop(Sender, Source: TObject; X,
  348. Y: Integer);
  349. begin
  350.  
  351. ButtonClick(AddButton);
  352. end;
  353.  
  354. procedure TExpLinksForm.PinsListBoxDragOver(Sender, Source: TObject; X,
  355. Y: Integer; State: TDragState; var Accept: Boolean);
  356. begin
  357. Accept:= Source = ExpLinksListBox;
  358. end;
  359.  
  360. procedure TExpLinksForm.PinsListBoxDragDrop(Sender, Source: TObject; X,
  361. Y: Integer);
  362. begin
  363.  
  364. ButtonClick(DeleteButton);
  365. end;
  366.  
  367.  
  368. end.


Ответ отправил: min@y™ (статус: Доктор наук)
Время отправки: 29 апреля 2009, 14:09


Мини-форум вопроса

Мини-форум пуст.

Чтобы оставлять сообщения в мини-форумах, Вы должны авторизироваться на сайте.

Версия движка: 2.6+ (26.01.2011)
Текущее время: 22 февраля 2025, 11:32
Выполнено за 0.03 сек.