|
Вопрос # 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 между обоими листбоксами. Я думаю, это может пригодиться.
Приложение: Переключить в обычный режим- object ExpLinksForm: TExpLinksForm
- Left = 398
- Top = 283
- BorderStyle = bsDialog
- Caption = #1069#1090#1072#1083#1086#1085#1085#1099#1077' '#1089#1074#1103#1079#1080
- ClientHeight = 353
- ClientWidth = 385
- Color = clBtnFace
- Font.Charset = DEFAULT_CHARSET
- Font.Color = clWindowText
- Font.Height = -11
- Font.Name = 'Tahoma'
- Font.Style = []
- KeyPreview = True
- OldCreateOrder = False
- Scaled = False
- DesignSize = (
- 385
- 353)
- PixelsPerInch = 96
- TextHeight = 13
- object Label1: TLabel
- Left = 8
- Top = 8
- Width = 92
- Height = 13
- Caption = #1069#1090#1072#1083#1086#1085#1085#1099#1077' '#1089#1074#1103#1079#1080':'
- end
- object Label2: TLabel
- Left = 216
- Top = 8
- Width = 116
- Height = 13
- Anchors = [akTop, akRight]
- Caption = #1050#1086#1085#1090#1072#1082#1090#1099'
'#1091#1089#1090#1088#1086#1081#1089#1090#1074#1072':'
- end
- object ExpLinksListBox: TListBox
- Left = 8
- Top = 24
- Width = 161
- Height = 289
- Anchors = [akLeft, akTop, akBottom]
- DragMode = dmAutomatic
- ItemHeight = 13
- MultiSelect = True
- TabOrder = 0
- OnDragDrop = ExpLinksListBoxDragDrop
- OnDragOver = ExpLinksListBoxDragOver
- end
- object AddButton: TButton
- Tag = 10
- Left = 176
- Top = 144
- Width = 33
- Height = 25
- Anchors = [akLeft, akTop, akRight]
- Caption = #1079
- Font.Charset = DEFAULT_CHARSET
- Font.Color = clWindowText
- Font.Height = -11
- Font.Name = 'Wingdings'
- Font.Style = []
- ParentFont = False
- TabOrder = 1
- OnClick = ButtonClick
- end
- object DeleteButton: TButton
- Tag = 11
- Left = 176
- Top = 176
- Width = 33
- Height = 25
- Anchors = [akLeft, akTop, akRight]
- Caption = #1080
- Font.Charset = DEFAULT_CHARSET
- Font.Color = clWindowText
- Font.Height = -11
- Font.Name = 'Wingdings'
- Font.Style = []
- ParentFont = False
- TabOrder = 2
- OnClick = ButtonClick
- end
- object PinsListBox: TListBox
- Left = 216
- Top = 24
- Width = 161
- Height = 289
- Anchors = [akTop, akRight, akBottom]
- DragMode = dmAutomatic
- ItemHeight = 13
- MultiSelect = True
- TabOrder = 3
- OnDragDrop = PinsListBoxDragDrop
- OnDragOver = PinsListBoxDragOver
- end
- object OkButton: TButton
- Left = 223
- Top = 321
- Width = 75
- Height = 25
- Anchors = [akRight, akBottom]
- Caption = 'OK'
- TabOrder = 4
- OnClick = ButtonClick
- end
- object CancelButton: TButton
- Tag = 1
- Left = 303
- Top = 321
- Width = 75
- Height = 25
- Anchors = [akRight, akBottom]
- Cancel = True
- Caption = #1054#1090#1084#1077#1085#1072
- ModalResult = 2
- TabOrder = 5
- end
- end
-
-
-
- unit uExpLinksForm;
-
- interface
-
- uses
-
- uConsts, uDevPlugs, uSettings, uTypes,
-
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
-
- type
- TExpLinksForm = class(TProjectCustomForm)
- ExpLinksListBox: TListBox;
- Label1: TLabel;
- AddButton: TButton;
- DeleteButton: TButton;
- PinsListBox: TListBox;
- OkButton: TButton;
- CancelButton: TButton;
- Label2: TLabel;
- procedure ButtonClick(Sender: TObject);
- procedure ExpLinksListBoxDragOver(Sender, Source: TObject; X,
- Y: Integer; State: TDragState; var Accept: Boolean);
- procedure ExpLinksListBoxDragDrop(Sender, Source: TObject; X,
- Y: Integer);
- procedure PinsListBoxDragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- procedure PinsListBoxDragDrop(Sender, Source: TObject; X, Y: Integer);
- private
-
-
- public
- Map: TDevPlugMap;
-
-
-
- end;
-
- var
- ExpLinksForm: TExpLinksForm;
-
-
- function ChangeExpLinks(APin: TDevPin): Boolean;
-
- implementation
-
- uses uLogForm;
-
- {$R *.dfm}
-
- procedure AddLink(const AAddress: Cardinal; ALinks: TList);
- begin
-
- if ALinks.IndexOf(Pointer(AAddress)) = -1
- then ALinks.Add(Pointer(AAddress));
- end;
-
- function ChangeExpLinks(APin: TDevPin): Boolean;
- var
- ALinks: TList;
- Index: Integer;
- //LinkIndex: Integer;
- TempPin: TDevPin;
- begin
- if not Assigned(ExpLinksForm)
- then ExpLinksForm:= TExpLinksForm.Create(Application);
-
- ALinks:= TList.Create();
- try
- with ExpLinksForm do
- begin
- Map:= APin.OwnerPlug.OwnerPlugMap;
-
-
-
-
- Result:= ShowModal() = mrOk;
-
- if Result
- then begin
-
-
-
-
-
-
-
- for Index:= 0 to ExpLinksListBox.Count - 1 do
- begin
- TempPin:= TDevPin(ExpLinksListBox.Items.Objects[Index]);
-
-
-
-
- //for LinkIndex:= 0 to TempPin.ExpectedLinksCount - 1 do
- // AddLink(TempPin.ExpectedLinks[LinkIndex], ALinks);
- end;
-
-
- AddLinks2Log(ALinks);
- Map.ClearExpectedLinks(APin);
- Map.AddExpectedLinks(ALinks);
- //APin.AddExpectedLinks(ALinks);
- end;
- end;
- finally
- ALinks.Free();
- FreeAndNil(ExpLinksForm);
- end;
- end;
-
- { TExpLinksForm }
-
- procedure TExpLinksForm.ApplicationIdleProc;
- begin
-
- inherited;
- AddButton.Enabled:= PinsListBox.SelCount <> 0;
- DeleteButton.Enabled:= ExpLinksListBox.SelCount <> 0;
- end;
-
- procedure TExpLinksForm.ApplySettingsProc;
- begin
-
- inherited;
- end;
-
- procedure TExpLinksForm.FillExpLinks(APin: TDevPin);
- var
- Index, LinkIndex: Integer;
- ALinks: TList;
- Pin: TDevPin;
- begin
-
- ExpLinksListBox.Items.BeginUpdate();
- ALinks:= TList.Create();
- try
- ExpLinksListBox.Clear();
-
-
- Map.LinkAddress2Pins(APin.ExpectedLinks[Index], ALinks);
-
-
-
-
- begin
- Pin:= TDevPin(ALinks[LinkIndex]);
- ExpLinksListBox.AddItem(Pin.SchemeName, Pin);
- end;
- finally
- ExpLinksListBox.Items.EndUpdate();
- ALinks.Free();
- end;
- end;
-
- procedure TExpLinksForm.FillPins(APin: TDevPin);
- var
- PlugIndex, PinIndex: Integer;
- TempPin: TDevPin;
- begin
-
-
- PinsListBox.Items.BeginUpdate();
- try
- PinsListBox.Clear();
-
-
-
- begin
- TempPin:= Map.Pins[PlugIndex, PinIndex];
-
-
-
- if APin.ExpectedLinksList.IndexOf(Pointer(TempPin.Address)) <> -1
- then Continue;
-
-
-
- if TempPin = APin
- then Continue;
-
- PinsListBox.AddItem(TempPin.SchemeName, TempPin);
- end;
- finally
- PinsListBox.Items.EndUpdate();
- end;
- end;
-
- procedure TExpLinksForm.ButtonClick(Sender: TObject);
- var
- Index: Integer;
- begin
-
- case TControl(Sender).Tag of
-
- 0: ModalResult:= mrOk;
-
- // "<--"
- 10: begin
- for Index:= 0 to PinsListBox.Count - 1 do
- if PinsListBox.Selected[Index]
- then ExpLinksListBox.AddItem(PinsListBox.Items[Index],
PinsListBox.Items.Objects[Index]);
-
- PinsListBox.DeleteSelected();
- //ExpLinksListBox.Items.C
- end;
- // "-->"
- 11: begin
- for Index:= 0 to ExpLinksListBox.Count - 1 do
- if ExpLinksListBox.Selected[Index]
- then PinsListBox.AddItem(ExpLinksListBox.Items[Index],
ExpLinksListBox.Items.Objects[Index]);
-
- ExpLinksListBox.DeleteSelected();
- end;
- end;
- end;
-
- procedure TExpLinksForm.ExpLinksListBoxDragOver(Sender, Source: TObject; X,
- Y: Integer; State: TDragState; var Accept: Boolean);
- begin
- Accept:= Source = PinsListBox;
- end;
-
- procedure TExpLinksForm.ExpLinksListBoxDragDrop(Sender, Source: TObject; X,
- Y: Integer);
- begin
-
- ButtonClick(AddButton);
- end;
-
- procedure TExpLinksForm.PinsListBoxDragOver(Sender, Source: TObject; X,
- Y: Integer; State: TDragState; var Accept: Boolean);
- begin
- Accept:= Source = ExpLinksListBox;
- end;
-
- procedure TExpLinksForm.PinsListBoxDragDrop(Sender, Source: TObject; X,
- Y: Integer);
- begin
-
- ButtonClick(DeleteButton);
- end;
-
-
- end.
 |
Ответ отправил: min@y™ (статус: Доктор наук)
Время отправки: 29 апреля 2009, 14:09
|
Мини-форум вопроса
Мини-форум пуст.
Чтобы оставлять сообщения в мини-форумах, Вы должны авторизироваться на сайте.
|