Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

select a specific line in a listbox if you know the item index

Posted on 2008-10-20
6
Medium Priority
?
1,016 Views
Last Modified: 2013-11-23
Delphi 6.

I have a listbox with multiple lines.

What i want to do is somethng like:

1) User Select a line.
2) application remembers the index of the selected line
3) user manipulates/edits the selected line
4) save the new listbox info to a text file (savetofile)
5) automatically scroll and select the originally selected line as I remebered the item index.  so from a user point of view they don't lose their 'place'    <<HELP  REQUIRED FOR STEP 5

Cheers

Brad
0
Comment
Question by:delphinewbie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22764968
by default a listbox does not allow editing

it may be better to use a StringGrid
in essence nearly the same when you use 1 column
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22765027
here is an example

to see the text of the dfm file
right click on the form and click on "view as text"
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls, Buttons;
 
type
  TForm1 = class(TForm)
    DataGrid: TStringGrid;
    btnAddRow: TBitBtn;
    procedure DataGridSetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: String);
    procedure btnAddRowClick(Sender: TObject);
  private
    fDataFileName: string;
    procedure LoadData;
  public
    constructor Create(AOwner: TComponent); override;
    property DataFileName: string read fDataFileName;
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
constructor TForm1.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fDataFileName := ExtractFilePath(Application.ExeName) + 'data.txt';
  LoadData;
end;
 
procedure TForm1.LoadData;
var
  List: TStrings;
  I: Integer;
begin
  DataGrid.ColCount := 2;
  DataGrid.RowCount := 2;
  DataGrid.FixedCols := 1;
  DataGrid.FixedRows := 1;
  DataGrid.ColWidths[0] := 12;
  DataGrid.ColWidths[1] := 500;
  DataGrid.Cells[1, 0] := 'Column Title';
  DataGrid.Cells[1, 1] := '';
  DataGrid.Cells[0, 1] := '1';
  if FileExists(fDataFileName) then
  begin
    List := TStringList.Create;
    try
      List.LoadFromFile(fDataFileName);
      for I := 0 to List.Count -1 do
      begin
        if I > 0 then
          DataGrid.RowCount := DataGrid.RowCount + 1;
        DataGrid.Cells[0, I+1] := IntToStr(I+1);
        DataGrid.Cells[1, I+1] := List[I];
      end;
    finally
      FreeAndNil(List);
    end;
  end;
end;
 
procedure TForm1.DataGridSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String);
var
  List: TStrings;
  I: Integer;
begin
  List := TStringList.Create;
  try
    for I := 1 to DataGrid.RowCount-1 do
    begin
      if ARow = I then
        List.Add(Value)
      else
        List.Add(DataGrid.Cells[1, I]);
    end;
    List.SaveToFile(DataFileName);
  finally
    FreeAndNil(List);
  end;
end;
 
procedure TForm1.btnAddRowClick(Sender: TObject);
begin
  with DataGrid do
  begin
    RowCount := RowCount + 1;
    Cells[0, RowCount-1] := IntToStr(RowCount-1);
    Cells[1, RowCount-1] := '';
    Row := RowCount-1;
    Col := 1;
  end;
end;
 
end.
 
-- dfm --
object Form1: TForm1
  Left = 221
  Top = 181
  Width = 870
  Height = 640
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object DataGrid: TStringGrid
    Left = 80
    Top = 35
    Width = 601
    Height = 501
    ColCount = 2
    RowCount = 2
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing]
    TabOrder = 0
    OnSetEditText = DataGridSetEditText
    ColWidths = (
      64
      420)
  end
  object btnAddRow: TBitBtn
    Left = 705
    Top = 45
    Width = 75
    Height = 25
    Caption = 'btnAddRow'
    TabOrder = 1
    OnClick = btnAddRowClick
  end
end

Open in new window

0
 

Expert Comment

by:bluetoe
ID: 22765137
Is this the sort of thing you want
unit listboxUnit2;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm2 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    giListBoxPos : integer;
  public
    { Public declarations }
  end;
 
var
  Form2: TForm2;
 
implementation
 
{$R *.dfm}
 
procedure TForm2.Button1Click(Sender: TObject);
begin
	ListBox1.Items.Delete(giListBoxPos);
	ListBox1.Items.Insert(giListBoxPos, Edit1.text);
	ListBox1.ItemIndex := giListBoxPos;
end;
 
procedure TForm2.FormCreate(Sender: TObject);
var i : integer;
begin
	for i  := 0 to 20 do
		ListBox1.Items.Add( inttostr(i));
 
 
end;
 
procedure TForm2.ListBox1Click(Sender: TObject);
begin
	giListBoxPos := ListBox1.ItemIndex;
 
	edit1.Text := ListBox1.Items[giListBoxPos];
	edit1.SetFocus;
 
end;
 
end.

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22765146
with the stringgrid you get inplace editing
0
 
LVL 1

Accepted Solution

by:
delphinewbie earned 0 total points
ID: 22765306
I solved my issue.

listbox2.Selected[MyItemIndex]:=true; << is the line i need

problem i had was the writetofile slowed down the processing, so i needed to insert a delay before the above line.

Thanks for your suggestions!
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23042619
you almost solved your issue ...

if you want the selected item from the Listbox to be in the same place
you need to save the ListBox.TopIndex too

just like you are doing with ListBox.ItemIndex

and you should accept bluetoe's comment
as the code snippet is the same as your solution
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question