Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2008.07.13;
Скачать: CL | DM;

Вниз

Как привязаь компонент ProgressBar   Найти похожие ветки 

 
Viktor198 ©   (2008-06-16 18:59) [0]

При считывании файла программа должна показывать хода выполнения процесса. Вот код проги:
unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, Grids, ComCtrls, DateUtils;

type
 TForm1 = class(TForm)
   Button1: TButton;
   StringGrid1: TStringGrid;
   OpenDialog1: TOpenDialog;
   ComboBox1: TComboBox;
   Label1: TLabel;
   DateTimePicker1: TDateTimePicker;
   DateTimePicker2: TDateTimePicker;
   Button3: TButton;
   Label2: TLabel;
   Label3: TLabel;
   procedure Button1Click(Sender: TObject);
   procedure FormCreate(Sender: TObject);
   procedure ComboBox1Enter(Sender: TObject);
   procedure Button3Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;
type code_http=record
data,time,process,command,traffic:string;
date:TDateTime;
end;

var
 Form1: TForm1;
 tabl:code_http;
 Words:TStringList;// Список процессов
 numb:integer;
 bool:boolean;
 summa:int64;
 year,Month,Day:Word;
 F:textfile;
implementation

{$R *.dfm}
{$R resfile.res}
Function GetWord(var S:string):string;
{ Вспомогательная функция для выделения очередного слова из строки }
const     // Множество символов слова:
Letters: set of Char = [chr(33)..chr(255)];
begin
Result:="";
// Удаляем в начале строки все символы, не относящиеся к слову
while  (S <> "")  and not (S[1]  in Letters) do Delete(S,1,1);
// Формируем очередное слово
while  (S <> "")  and (S[1] in Letters) do begin
Result:=Result + S[1];
Delete(S,1,1);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
label 2;
var i:integer;s1,s:string;
begin
// Готовим список Words:
Words:=TStringList.Create;
Words.Sorted:=True; // Сортируем строки
Words.Duplicates  := dupIgnore;
// С помощью стандартного диалогового окна получаем имя файла
if not(OpenDialog1.Execute) then begin
// Файл нельзя открыть:
ShowMessage("Невозможно открыть файл  " + OpenDialog1.FileName); Exit;
end
else begin
AssignFile(F,  OpenDialog1.FileName);
if fileexists(OpenDialog1.FileName) then begin
Reset(F);
repeat
2:readln(F,S);
s1:=GetWord(S);s1:=GetWord(S);s1:=GetWord(S);
if (copy(s1,3,1)="/")or(copy(s1,1,1)<>"[") then goto 2
else s1:=copy(s1,2,pos(":",s1)-2);
Words.Add(s1);
Words.Add("все процессы");
until EOF(F);
closefile(f);
end;end;
ComboBox1.Enabled:=true;
DateTimePicker1.Enabled:=true;
DateTimePicker2.Enabled:=true;
Button3.Enabled:=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin bool:=false;
StringGrid1.Cells[0,0]:="Дата";
StringGrid1.Cells[1,0]:="Время";
StringGrid1.Cells[2,0]:="Процесс";
StringGrid1.Cells[3,0]:="Событие";
StringGrid1.Cells[4,0]:="Трафик";
end;

procedure TForm1.ComboBox1Enter(Sender: TObject);
var i:integer;
begin
if bool=false then begin
for i:=0 to Words.Count-1 do ComboBox1.Items.Add(Words[i]);
bool:=true;end;
end;

procedure TForm1.Button3Click(Sender: TObject);
label 1;
var i,j:integer;S,stroka:string;
begin
StringGrid1.RowCount:=2;
StringGrid1.Cells[0,1]:="";
StringGrid1.Cells[1,1]:="";
StringGrid1.Cells[2,1]:="";
StringGrid1.Cells[3,1]:="";
StringGrid1.Cells[4,1]:="";
// Пытаемся открыть файл
AssignFile(F,  OpenDialog1.FileName);
if fileexists(OpenDialog1.FileName) then begin
Reset(F);
// Читаем файл по строкам
i:=1;summa:=0;
Screen.Cursor:=crHourGlass;
repeat
1:readln(F,S); // Читаем очередную строк,
// Выделяем из строки слова и заносим их в запись.
stroka:=GetWord(S);
Year:=strtoint(copy(stroka,1,pos("/",stroka)-1));
stroka:=copy(stroka,pos("/",stroka)+1,length(stroka)-pos("/",stroka));
Month:=strtoint(copy(stroka,1,pos("/",stroka)-1));
Day:=strtoint(copy(stroka,pos("/",stroka)+1,length(stroka)-pos("/",stroka)));
tabl.date:=EncodeDate(Year,Month,Day);
tabl.data:=FormatDateTime("dddddd",tabl.date);
{tabl.data:=DateToStr(tabl.date); }
tabl.time:=GetWord(S);
tabl.process:=GetWord(S);
if (copy(tabl.process,3,1)="/")or(copy(tabl.process,1,1)<>"[") then goto 1
else tabl.process:=copy(tabl.process,2,pos(":",tabl.process)-2);
Words.Add(tabl.process);
Words.Add("все процессы");
tabl.command:=GetWord(S);
tabl.traffic:=GetWord(S);
if not((copy(tabl.traffic,1,1)<>"f")) then goto 1;
if ((CompareDate(DateTimePicker1.Date,tabl.date)=0)or(CompareDate(DateTimePicker1.D ate,tabl.date)=-1))and
  ((CompareDate(DateTimePicker2.Date,tabl.date)=0)or(CompareDate(DateTimePicker2.D ate,tabl.date)=1))and
  ((ComboBox1.Items.Strings[ComboBox1.ItemIndex]=tabl.process)or(ComboBox1.Items.S trings[ComboBox1.ItemIndex]="все процессы"))
   then begin
StringGrid1.Cells[0,i]:=tabl.data;
StringGrid1.Cells[1,i]:=tabl.time;
StringGrid1.Cells[2,i]:=tabl.process;
StringGrid1.Cells[3,i]:=tabl.command;
StringGrid1.Cells[4,i]:=tabl.traffic;
StringGrid1.RowCount:=StringGrid1.RowCount+1;
summa:=summa+strtoint (tabl.traffic);
i:=i+1;
end;  
until EOF(F);
end;
numb:=i-1;
Screen.Cursor:=crDefault;
CloseFile(F);
Screen.Cursor:=crDefault;
if i>1 then StringGrid1.RowCount:=StringGrid1.RowCount-1;
Label1.Visible:=true;
summa:=(summa div 1024) div 1024;
Label1.Caption:="Сумма: "+inttostr(summa)+" Мбайт";
end;

end.


 
AlexanderMS ©   (2008-06-16 19:11) [1]

А конкретнее, в чём проблема? Установить ProgressBar1.Max и изменять ProgressBar1.Position. Остальное - мозг.


 
Viktor198 ©   (2008-06-16 19:18) [2]

Если не трудно внеси изменения в этот код, я не понимаю как его привязать к процессу считывания файла.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, ComCtrls, DateUtils;

type
TForm1 = class(TForm)
  Button1: TButton;
  StringGrid1: TStringGrid;
  OpenDialog1: TOpenDialog;
  ComboBox1: TComboBox;
  Label1: TLabel;
  DateTimePicker1: TDateTimePicker;
  DateTimePicker2: TDateTimePicker;
  Button3: TButton;
  Label2: TLabel;
  Label3: TLabel;
  procedure Button1Click(Sender: TObject);
  procedure FormCreate(Sender: TObject);
  procedure ComboBox1Enter(Sender: TObject);
  procedure Button3Click(Sender: TObject);
private
  { Private declarations }
public
  { Public declarations }
end;
type code_http=record
data,time,process,command,traffic:string;
date:TDateTime;
end;

var
Form1: TForm1;
tabl:code_http;
Words:TStringList;// Список процессов
numb:integer;
bool:boolean;
summa:int64;
year,Month,Day:Word;
F:textfile;
implementation

{$R *.dfm}
{$R resfile.res}
Function GetWord(var S:string):string;
{ Вспомогательная функция для выделения очередного слова из строки }
const     // Множество символов слова:
Letters: set of Char = [chr(33)..chr(255)];
begin
Result:="";
// Удаляем в начале строки все символы, не относящиеся к слову
while  (S <> "")  and not (S[1]  in Letters) do Delete(S,1,1);
// Формируем очередное слово
while  (S <> "")  and (S[1] in Letters) do begin
Result:=Result + S[1];
Delete(S,1,1);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
label 2;
var i:integer;s1,s:string;
begin
// Готовим список Words:
Words:=TStringList.Create;
Words.Sorted:=True; // Сортируем строки
Words.Duplicates  := dupIgnore;
// С помощью стандартного диалогового окна получаем имя файла
if not(OpenDialog1.Execute) then begin
// Файл нельзя открыть:
ShowMessage("Невозможно открыть файл  " + OpenDialog1.FileName); Exit;
end
else begin
AssignFile(F,  OpenDialog1.FileName);
if fileexists(OpenDialog1.FileName) then begin
Reset(F);
repeat
2:readln(F,S);
s1:=GetWord(S);s1:=GetWord(S);s1:=GetWord(S);
if (copy(s1,3,1)="/")or(copy(s1,1,1)<>"[") then goto 2
else s1:=copy(s1,2,pos(":",s1)-2);
Words.Add(s1);
Words.Add("все процессы");
until EOF(F);
closefile(f);
end;end;
ComboBox1.Enabled:=true;
DateTimePicker1.Enabled:=true;
DateTimePicker2.Enabled:=true;
Button3.Enabled:=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin bool:=false;
StringGrid1.Cells[0,0]:="Дата";
StringGrid1.Cells[1,0]:="Время";
StringGrid1.Cells[2,0]:="Процесс";
StringGrid1.Cells[3,0]:="Событие";
StringGrid1.Cells[4,0]:="Трафик";
end;

procedure TForm1.ComboBox1Enter(Sender: TObject);
var i:integer;
begin
if bool=false then begin
for i:=0 to Words.Count-1 do ComboBox1.Items.Add(Words[i]);
bool:=true;end;
end;

procedure TForm1.Button3Click(Sender: TObject);
label 1;
var i,j:integer;S,stroka:string;
begin
StringGrid1.RowCount:=2;
StringGrid1.Cells[0,1]:="";
StringGrid1.Cells[1,1]:="";
StringGrid1.Cells[2,1]:="";
StringGrid1.Cells[3,1]:="";
StringGrid1.Cells[4,1]:="";
// Пытаемся открыть файл
AssignFile(F,  OpenDialog1.FileName);
if fileexists(OpenDialog1.FileName) then begin
Reset(F);
// Читаем файл по строкам
i:=1;summa:=0;
Screen.Cursor:=crHourGlass;
repeat
1:readln(F,S); // Читаем очередную строк,
// Выделяем из строки слова и заносим их в запись.
stroka:=GetWord(S);
Year:=strtoint(copy(stroka,1,pos("/",stroka)-1));
stroka:=copy(stroka,pos("/",stroka)+1,length(stroka)-pos("/",stroka));
Month:=strtoint(copy(stroka,1,pos("/",stroka)-1));
Day:=strtoint(copy(stroka,pos("/",stroka)+1,length(stroka)-pos("/",stroka)));
tabl.date:=EncodeDate(Year,Month,Day);
tabl.data:=FormatDateTime("dddddd",tabl.date);
{tabl.data:=DateToStr(tabl.date); }
tabl.time:=GetWord(S);
tabl.process:=GetWord(S);
if (copy(tabl.process,3,1)="/")or(copy(tabl.process,1,1)<>"[") then goto 1
else tabl.process:=copy(tabl.process,2,pos(":",tabl.process)-2);
Words.Add(tabl.process);
Words.Add("все процессы");
tabl.command:=GetWord(S);
tabl.traffic:=GetWord(S);
if not((copy(tabl.traffic,1,1)<>"f")) then goto 1;
if ((CompareDate(DateTimePicker1.Date,tabl.date)=0)or(CompareDate(DateTimePicker1.D  ate,tabl.date)=-1))and
 ((CompareDate(DateTimePicker2.Date,tabl.date)=0)or(CompareDate(DateTimePicker2.D  ate,tabl.date)=1))and
 ((ComboBox1.Items.Strings[ComboBox1.ItemIndex]=tabl.process)or(ComboBox1.Items.S  trings[ComboBox1.ItemIndex]="все процессы"))
  then begin
StringGrid1.Cells[0,i]:=tabl.data;
StringGrid1.Cells[1,i]:=tabl.time;
StringGrid1.Cells[2,i]:=tabl.process;
StringGrid1.Cells[3,i]:=tabl.command;
StringGrid1.Cells[4,i]:=tabl.traffic;
StringGrid1.RowCount:=StringGrid1.RowCount+1;
summa:=summa+strtoint (tabl.traffic);
i:=i+1;
end;  
until EOF(F);
end;
numb:=i-1;
Screen.Cursor:=crDefault;
CloseFile(F);
Screen.Cursor:=crDefault;
if i>1 then StringGrid1.RowCount:=StringGrid1.RowCount-1;
Label1.Visible:=true;
summa:=(summa div 1024) div 1024;
Label1.Caption:="Сумма: "+inttostr(summa)+" Мбайт";
end;

end.


 
AlexanderMS ©   (2008-06-16 19:48) [3]

А как долго происходит чтение из файла и обработка данных? Сколько строк в файле?
Просто, имеет ли смысл ставить ProgressBar?


 
AlexanderMS ©   (2008-06-16 20:05) [4]

> Screen.Cursor:=crDefault;
> CloseFile(F);
> Screen.Cursor:=crDefault;


- Зачем 2 раза?

Этот подход чтения из файла не совсем удобен. Лучше бы через TStringList.

procedure TForm1.Button3Click(Sender: TObject);
label 1;
var i,j:integer;S,stroka:string;
begin
StringGrid1.RowCount:=2;
StringGrid1.Cells[0,1]:="";
StringGrid1.Cells[1,1]:="";
StringGrid1.Cells[2,1]:="";
StringGrid1.Cells[3,1]:="";
StringGrid1.Cells[4,1]:="";
// Пытаемся открыть файл

if fileexists(OpenDialog1.FileName) then begin
summa:=0;
Screen.Cursor:=crHourGlass;

with TStringList.Create do
begin
LoadFromFile(OpenDialog1.FileName);
ProgressBar1.Max := Count;
ProgressBar1.Position := 0;


for i := 1 to Count do
begin

1:
ProgressBar1.Position := ProgressBar1.Position + 1;
// Выделяем из строки слова и заносим их в запись.
stroka:=GetWord(S);
Year:=strtoint(copy(stroka,1,pos("/",stroka)-1));
stroka:=copy(stroka,pos("/",stroka)+1,length(stroka)-pos("/",stroka));
Month:=strtoint(copy(stroka,1,pos("/",stroka)-1));
Day:=strtoint(copy(stroka,pos("/",stroka)+1,length(stroka)-pos("/",stroka)));
tabl.date:=EncodeDate(Year,Month,Day);
tabl.data:=FormatDateTime("dddddd",tabl.date);
{tabl.data:=DateToStr(tabl.date); }
tabl.time:=GetWord(S);
tabl.process:=GetWord(S);
if (copy(tabl.process,3,1)="/")or(copy(tabl.process,1,1)<>"[") then goto 1
else tabl.process:=copy(tabl.process,2,pos(":",tabl.process)-2);
Words.Add(tabl.process);
Words.Add("все процессы");
tabl.command:=GetWord(S);
tabl.traffic:=GetWord(S);
if not((copy(tabl.traffic,1,1)<>"f")) then goto 1;
if ((CompareDate(DateTimePicker1.Date,tabl.date)=0)or(CompareDate(DateTimePicker1.D   ate,tabl.date)=-1))and
((CompareDate(DateTimePicker2.Date,tabl.date)=0)or(CompareDate(DateTimePicker2.D   ate,tabl.date)=1))and
((ComboBox1.Items.Strings[ComboBox1.ItemIndex]=tabl.process)or(ComboBox1.Items.S   trings[ComboBox1.ItemIndex]="все процессы"))
 then begin
StringGrid1.Cells[0,i]:=tabl.data;
StringGrid1.Cells[1,i]:=tabl.time;
StringGrid1.Cells[2,i]:=tabl.process;
StringGrid1.Cells[3,i]:=tabl.command;
StringGrid1.Cells[4,i]:=tabl.traffic;
StringGrid1.RowCount:=StringGrid1.RowCount+1;
summa:=summa+strtoint (tabl.traffic);

end;  
end;

numb:=Count-1;
Screen.Cursor:=crDefault;
// CloseFile(F);
Screen.Cursor:=crDefault;
if i>1 then StringGrid1.RowCount:=StringGrid1.RowCount-1;
Label1.Visible:=true;
summa:=(summa div 1024) div 1024;
Label1.Caption:="Сумма: "+inttostr(summa)+" Мбайт";

end;

end.


Вот, если не ошибся нигде.


 
AlexanderMS ©   (2008-06-16 20:08) [5]

Нет, так будет правильнее.

procedure TForm1.Button3Click(Sender: TObject);
label 1;
var i,j:integer;S,stroka:string;
N : Integer;
begin
StringGrid1.RowCount:=2;
StringGrid1.Cells[0,1]:="";
StringGrid1.Cells[1,1]:="";
StringGrid1.Cells[2,1]:="";
StringGrid1.Cells[3,1]:="";
StringGrid1.Cells[4,1]:="";
// Пытаемся открыть файл

if fileexists(OpenDialog1.FileName) then begin
summa:=0;
Screen.Cursor:=crHourGlass;

with TStringList.Create do
begin
LoadFromFile(OpenDialog1.FileName);
ProgressBar1.Max := Count;
ProgressBar1.Position := 0;
i := 1;
for N := 1 to Count do
begin
1:
ProgressBar1.Position := N;
// Выделяем из строки слова и заносим их в запись.
stroka:=GetWord(S);
Year:=strtoint(copy(stroka,1,pos("/",stroka)-1));
stroka:=copy(stroka,pos("/",stroka)+1,length(stroka)-pos("/",stroka));
Month:=strtoint(copy(stroka,1,pos("/",stroka)-1));
Day:=strtoint(copy(stroka,pos("/",stroka)+1,length(stroka)-pos("/",stroka)));
tabl.date:=EncodeDate(Year,Month,Day);
tabl.data:=FormatDateTime("dddddd",tabl.date);
{tabl.data:=DateToStr(tabl.date); }
tabl.time:=GetWord(S);
tabl.process:=GetWord(S);
if (copy(tabl.process,3,1)="/")or(copy(tabl.process,1,1)<>"[") then goto 1
else tabl.process:=copy(tabl.process,2,pos(":",tabl.process)-2);
Words.Add(tabl.process);
Words.Add("все процессы");
tabl.command:=GetWord(S);
tabl.traffic:=GetWord(S);
if not((copy(tabl.traffic,1,1)<>"f")) then goto 1;
if ((CompareDate(DateTimePicker1.Date,tabl.date)=0)or(CompareDate(DateTimePicker1.D    ate,tabl.date)=-1))and
((CompareDate(DateTimePicker2.Date,tabl.date)=0)or(CompareDate(DateTimePicker2.D    ate,tabl.date)=1))and
((ComboBox1.Items.Strings[ComboBox1.ItemIndex]=tabl.process)or(ComboBox1.Items.S    trings[ComboBox1.ItemIndex]="все процессы"))
then begin
StringGrid1.Cells[0,i]:=tabl.data;
StringGrid1.Cells[1,i]:=tabl.time;
StringGrid1.Cells[2,i]:=tabl.process;
StringGrid1.Cells[3,i]:=tabl.command;
StringGrid1.Cells[4,i]:=tabl.traffic;
StringGrid1.RowCount:=StringGrid1.RowCount+1;
summa:=summa+strtoint (tabl.traffic);
i := i + 1;

end;  
end;

numb:=i-1;
Screen.Cursor:=crDefault;

Screen.Cursor:=crDefault;
if i>1 then StringGrid1.RowCount:=StringGrid1.RowCount-1;
Label1.Visible:=true;
summa:=(summa div 1024) div 1024;
Label1.Caption:="Сумма: "+inttostr(summa)+" Мбайт";

end;

end.


Во втором случае аналогично.


 
app ©   (2008-06-16 20:11) [6]

> AlexanderMS  (16.06.2008 19:48:03)  [3]

Надоело, форум не помойка, что бы в него сваливать столько раз код.



Страницы: 1 вся ветка

Текущий архив: 2008.07.13;
Скачать: CL | DM;

Наверх




Память: 0.51 MB
Время: 0.023 c
15-1211768847
Slider007
2008-05-26 06:27
2008.07.13
С днем рождения ! 26 мая 2008 понедельник


2-1213285768
ivan8511
2008-06-12 19:49
2008.07.13
Индекс в обратном порядке


15-1212307802
alex-drob
2008-06-01 12:10
2008.07.13
Здравствуйте!


2-1213274505
Franzy
2008-06-12 16:41
2008.07.13
Try...Except не ловит ошибки ввода/вывода


2-1213165699
Gringoire
2008-06-11 10:28
2008.07.13
Матрицы в делфи.