IT NEWS PC REVUE PC FORUMInternet time: @461
Obsah fóra
Pravidlá  •  Kontakt  •  Prihlásenie  •  Registrácia

Luxusné Hodinky SHARK Akcia!!!

Akcia - poštovné úplne ZADARMO! Výpredaj zľavy až do 75% www.toppredaj.sk

Ako na hackerov

Otestuj bezpečnosť svojho profilu na sociálnej sieti. https://apps.facebook.com

Chceš plat až 2000 Eur?

Ja zarábam z domu a som v pohode! Práca z domu - Nezmeškaj svoju šancu. www.pracadoma.sk

Hodinky - zásielkový predaj

Už o 2 dni môžete mať hodinky na Vašej ruke! CASIO, SEIKO, LORUS, 4YOU. www.casallia.sk

Výnimočný e-shop BiTiTi.sk!

Hardware, software, elektronika za skvelé ceny. Všetko skladom !!! www.bititi.sk

[delphi] listview load/save to file

Zaslať odpoveď
AutorSpráva
ha2
Užívateľ
Užívateľ

Založený: 21.12.2008
Príspevky: 112

PríspevokZaslal: Št 25.02.10 23:37Odpovedať s citátomNávrat hore

potrebujem ukladat data z listview do suboru a naopak. Nasiel som si nato hotovu funkciu. Az nato ze haluzi ak nacitam ulozeny listview zo suboru, nieco pridam a znova ulozin (v mieste pridania to nahadze stvorceky). Alebo ak ukladam aj enter (mam v bunke viac riadkov, napr text z richedit vlozeny do bunky). Vtedy to tiez pridava stvorceky. Jak to vyriesit?

Vyzera to ze ak ulozim, ukoncim program, spustim program a nacitam subor do listview tak vtedy to robi chyby, a akoby odreze prve 2 znaky...

kód:

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, comctrls,
  StdCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    procedure SaveListViewToFile(AListView: TListView; sFileName: string);
    procedure LoadListViewToFile(AListView: TListView; sFileName: string);
  public
  end;

const
  Msg1 = 'File "%s" does not exist!';
  Msg2 = '"%s" is not a ListView file!';

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.SaveListViewToFile(AListView: TListView; sFileName: string);
var
  idxItem, idxSub, IdxImage: Integer;
  F: TFileStream;
  pText: PChar;
  sText: string;
  W, ItemCount, SubCount: Word;
  MySignature: array [0..2] of Char;
begin
  //Initialization
  with AListView do
  begin
    ItemCount := 0;
    SubCount  := 0;
    //****
    MySignature := 'LVF';
    //  ListViewFile
    F := TFileStream.Create(sFileName, fmCreate or fmOpenWrite);
    F.Write(MySignature, SizeOf(MySignature));

    if Items.Count = 0 then
      // List is empty
      ItemCount := 0
    else
      ItemCount := Items.Count;
    F.Write(ItemCount, SizeOf(ItemCount));

    if Items.Count > 0 then
    begin
      for idxItem := 1 to ItemCount do
      begin
        with Items[idxItem - 1] do
        begin
          //Save subitems count
          if SubItems.Count = 0 then
            SubCount := 0
          else
            SubCount := Subitems.Count;
          F.Write(SubCount, SizeOf(SubCount));
          //Save ImageIndex
          IdxImage := ImageIndex;
          F.Write(IdxImage, SizeOf(IdxImage));
          //Save Caption
          sText := Caption;
          w     := Length(sText);
          pText := StrAlloc(Length(sText) + 1);
          StrPLCopy(pText, sText, Length(sText));
          F.Write(w, SizeOf(w));
          F.Write(pText^, w);
          StrDispose(pText);
          if SubCount > 0 then
          begin
            for idxSub := 0 to SubItems.Count - 1 do
            begin
              //Save Item's subitems
              sText := SubItems[idxSub];
              w     := Length(sText);
              pText := StrAlloc(Length(sText) + 1);
              StrPLCopy(pText, sText, Length(sText));
              F.Write(w, SizeOf(w));
              F.Write(pText^, w);
              StrDispose(pText);
            end;
          end;
        end;
      end;
    end;
    F.Free;
  end;
end;



procedure TForm1.LoadListViewToFile(AListView: TListView; sFileName: string);
var
  F: TFileStream;
  IdxItem, IdxSubItem, IdxImage: Integer;
  W, ItemCount, SubCount: Word;
  pText: PChar;
  PTemp: PChar;
  MySignature: array [0..2] of Char;
  sExeName: string;
begin
  with AListView do
  begin
    ItemCount := 0;
    SubCount  := 0;

    sExeName := ExtractFileName(sFileName);

    if not FileExists(sFileName) then
    begin
      MessageBox(Handle, PChar(Format(Msg1, [sExeName])), 'I/O Error', MB_ICONERROR);
      Exit;
    end;

    F := TFileStream.Create(sFileName, fmOpenRead);
    F.Read(MySignature, SizeOf(MySignature));

    if MySignature <> 'LVF' then
    begin
      MessageBox(Handle, PChar(Format(Msg2, [sExeName])), 'I/O Error', MB_ICONERROR);
      Exit;
    end;

    F.Read(ItemCount, SizeOf(ItemCount));
    Items.Clear;

    for idxItem := 1 to ItemCount do
    begin
      with Items.Add do
      begin
        //Read imageindex
        F.Read(SubCount, SizeOf(SubCount));
        //Read imageindex
        F.Read(IdxImage, SizeOf(IdxImage));
        ImageIndex := IdxImage;
        //Read the Caption
        F.Read(w, SizeOf(w));
        pText := StrAlloc(w + 1);
        pTemp := StrAlloc(w + 1);
        F.Read(pTemp^, W);
        StrLCopy(pText, pTemp, W);
        Caption := StrPas(pText);
        StrDispose(pTemp);
        StrDispose(pText);
        if SubCount > 0 then
        begin
          for idxSubItem := 1 to SubCount do
          begin
            F.Read(w, SizeOf(w));
            pText := StrAlloc(w + 1);
            pTemp := StrAlloc(w + 1);
            F.Read(pTemp^, W);
            StrLCopy(pText, pTemp, W);
            Items[idxItem - 1].SubItems.Add(StrPas(pText));
            StrDispose(pTemp);
            StrDispose(pText);
          end;
        end;
      end;
    end;

    F.Free;
  end;
end;

// Example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Save Items and Clear the ListView
  SaveListViewToFile(ListView1, 'MyListView.sav');
  ListView1.Items.Clear;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // Load Items
  LoadListViewToFile(ListView1, 'MyListView.sav');
end;
Zobraziť informácie o autoroviOdoslať súkromnú správu
ha2
Užívateľ
Užívateľ

Založený: 21.12.2008
Príspevky: 112

PríspevokZaslal: Ne 28.02.10 22:28Odpovedať s citátomNávrat hore

tak tyka sa to asi len D2010 ta chyba
Zobraziť informácie o autoroviOdoslať súkromnú správu
coldak
Skúsený užívateľ
Skúsený užívateľ

Založený: 29.10.2008
Príspevky: 927

PríspevokZaslal: Št 15.04.10 12:09Odpovedať s citátomNávrat hore

ak si dobre pamatam tak componenta TListView ma prvok Items ktory je typu TStringList a ten ma metodu SaveToFile. alebo sa mylim ? ak sa mylim tak prepac, davno som s delphi nerobil.
Zobraziť informácie o autoroviOdoslať súkromnú správu
ha2
Užívateľ
Užívateľ

Založený: 21.12.2008
Príspevky: 112

PríspevokZaslal: Ne 18.04.10 22:13Odpovedať s citátomNávrat hore

nie, items nema savetofile. Ale uz som si nato napisal vlastnu funkciu.
Zobraziť informácie o autoroviOdoslať súkromnú správu
Zobraziť príspevky z predchádzajúcich:    
Zaslať odpoveď
Nemôžete pridávať nové témy do tohto fóra.
Nemôžete odpovedať na témy v tomto fóre.
Nemôžete upravovať svoje príspevky v tomto fóre.
Nemôžete mazať svoje príspevky v tomto fóre.
Nemôžete hlasovať v tomto fóre.

Powered by phpBB 2.x.x © 2005 - 2012 PCforum, webhosting by WebSupport, edited by JanoF