L
L
Lev Nemirovsky2018-06-24 19:04:11
Delphi
Lev Nemirovsky, 2018-06-24 19:04:11

How to search by given number in StringGrid and file base?

Hello.
There are two tasks:
1. Find a list of employees whose phone numbers begin with a given number;
2. Find the address of an employee by his phone number.
The second problem was solved in this way:

unit Unit4;

interface

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

type
  TForm4 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Label3: TLabel;
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm4.BitBtn2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm4.BitBtn1Click(Sender: TObject);
begin
  label3.Caption := '';
  i := 0;
  if Kol <> 0 then
  while i <> Form1.StringGrid1.RowCount do
  begin
    if Edit1.Text = Form1.StringGrid1.Cells[2,i] then
        label3.Caption:=Form1.StringGrid1.Cells[3,i];
    inc(i);
  end;
  if label3.Caption = '' then ShowMessage('Такого телефона не существует!');
end;

end.

Please help me with the solution of the first task, I can’t do it, I tried something like this, but it doesn’t give errors during compilation, but it doesn’t work:
unit Unit6;

interface

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

type
  TForm6 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Label3: TLabel;
    StringGrid2: TStringGrid;
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form6: TForm6;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm6.BitBtn2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm6.BitBtn1Click(Sender: TObject);
var
 i : integer;
begin
 for i := Form1.StringGrid1.FixedRows to Form1.StringGrid1.RowCount - 1 do
 if Form1.StringGrid1.Cells[2, i] = Edit1.Text
  then begin
        StringGrid2.Rows[StringGrid2.RowCount - 1].CommaText := Form1.StringGrid1.Rows[i].CommaText;
        StringGrid2.RowCount := StringGrid2.RowCount + 1;
       end;
end;

end.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kalapanga, 2018-06-24
@kalapanga

Judging by the code, you want to display the result in StringGrid2. First, you have not defined the initial state of this grid - whether there is something in it or not. I would clean it up first. Next, swap the lines - first increase the number of lines, and then just write the content to the new line. Now I can’t check it, but in my opinion, calling by a non-existent line number does not cause an error, but it also does not give a practical result, which is most likely happening to you.
Well, what is written does not quite reflect the task. You write about searching for the first digit, and you check for string equality. Maybe you just haven't done it yet.

K
Konstantin Tsvetkov, 2018-06-25
@tsklab

There is a universal answer for Delphi: use a component that can do what you need ("everything is already stolen before you").

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question