delphi wyswietlanie okien komunikatów o błedach

0

Witam wszystkich .

Jestem bardzo zielony z delphi i proszę o pomoc.

Mam do napisania program o macierzach .

  1. Wyznaczanie wyznacznika macierzy 2x2
  2. Mnożenie macierzy przez liczbę.
    3, Mnożenie macierzy przez wektor.

Program napisałem ale zatrzymałem sie w jednym miejscu

nie mogę opanować wyświetlania komuninkatów jeżeli nic nie wpisze w pola edit a jest ich 6
i do każdego ma sie wyświetlac inny dialogbox z errorem.

Próbowałem ale oprócz okna które chce uzyskać zaraz po nim jest okno "is not a integer value"

Oto mój kod programu:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
Edit10: TEdit;
Edit11: TEdit;
ComboBox1: TComboBox;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
procedure RadioButton2Click(Sender: TObject);
procedure RadioButton1Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Edit4KeyPress(Sender: TObject; var Key: Char);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
procedure Edit3KeyPress(Sender: TObject; var Key: Char);
procedure Edit6KeyPress(Sender: TObject; var Key: Char);
procedure Edit7KeyPress(Sender: TObject; var Key: Char);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
if ( RadioButton2.Checked = True ) then begin
Edit5.Visible := False;
Edit6.Visible := False;
Edit7.Visible := True;
Edit8.Visible := True;
Edit9.Visible := True;
Edit10.Visible := True;
Edit11.Visible := True;
Label7.Visible := True;
Label2.Visible := False;
Label4.Visible := True;
Label3.Visible := True;
Label5.Visible := True;
Label6.Visible := True;
ComboBox1.Visible := True;
Edit8.Clear;
Edit9.Clear;
Edit10.Clear;
Edit11.Clear;
end;

end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
if ( RadioButton1.Checked = True ) then begin
Edit6.Visible := False;
Edit7.Visible := False;
Edit8.Visible := False;
Edit9.Visible := False;
Edit10.Visible := False;
Edit11.Visible := False;
Edit5.Visible := True;
Label5.Visible := False;
Label6.Visible := False;
Label4.Visible := False;
Label3.Visible := False;
Label2.Visible := True;
Label7.Visible := False;
ComboBox1.Visible := False
end;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if (ComboBox1.ItemIndex = 1 ) then begin

  Edit9.Visible := False;
  Edit11.Visible := False;
  Edit6.Visible:= True;
  Edit8.Clear;
  Edit10.Clear;
end;

if ( ComboBox1.ItemIndex = 0 ) then begin
  Edit9.Visible := True;
  Edit11.Visible := True;
  EDit6.Visible := False;
  Edit8.Clear;
  Edit9.Clear;
  Edit10.Clear;
  Edit11.Clear;

end;

end;

procedure TForm1.Button1Click(Sender: TObject);

var
a,b,c,d : Integer;
w : Integer;
l,k : Integer;
f,g,h,i : Integer;
m,n : Integer;

   begin

   if ( RadioButton1.Checked = True ) then begin

     a := StrToInt(Edit1.Text);
     b := StrToInt(Edit2.Text);
     c := StrToInt(Edit3.Text);
     d := StrToInt(Edit4.Text);


     w := (a*d)-(b*c);

     Edit5.Text := IntToStr(w)

    end;


    if ( RadioButton2.Checked = True ) then Begin

     a := StrToInt(Edit1.Text);
     b := StrToInt(Edit2.Text);
     c := StrToInt(Edit3.Text);
     d := StrToInt(Edit4.Text);


     l := StrToInt( Edit7.Text);
     f := (a*l);
     g := (b*l);
     h := (c*l);
     i := (d*l);

     Edit8.Text := IntToStr(f);
     Edit9.Text := IntToStr(g);
     Edit10.Text := IntToStr(h);
     Edit11.Text := IntToStr(i)

     end;


    if  (ComboBox1.ItemIndex = 1 )  then begin

     a := StrToInt(Edit1.Text);
     b := StrToInt(Edit2.Text);
     c := StrToInt(Edit3.Text);
     d := StrToInt(Edit4.Text);
     l := StrToInt(Edit7.Text);
     k := StrToInt(Edit6.Text);

     m :=(a+b)*k;
     n :=(c+d)*l;
     Edit8.Text := IntToStr(m);
     Edit10.Text := IntToStr(n);
     end;

end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
end;

procedure TForm1.Edit4KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
end;

procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
end;

procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
end;

procedure TForm1.Edit6KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
end;

procedure TForm1.Edit7KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
end;

end.
// to próbowałem zastosować:
// if Edit1.Text = '' then begin
// MessageBox(Handle,'Brak danych11 ','Macierze',MB_IconError);
// Edit1.SetFocus;
// a := StrToInt(Edit1.Text);
// end;

Przepraszam za mój język programowania ale dopiero zaczynam.

0

nie mogę opanować wyświetlania komuninkatów jeżeli nic nie wpisze w pola edit a jest ich 6
i do każdego ma sie wyświetlac inny dialogbox z errorem.

Możesz to wykonać na zasadzie "drabinki if'ów", lub wykorzystać tablicę z komunikatami i w pętli za pomocą choćby FindComponent (bo widzę, że nie nadajesz swoich identyfikatorów kontrolkom) sprawdzać ich zawartość, i jeśli EditX.Text = '' wyświetlić komunikat, aktywować puste pole edycyjne i wyjść z procedury/zdarzenia;

A jeśli chodzi o kod, to tą część:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
 begin
  if not (Key in ['0'..'9',#8,'-']) then  Key:=#0;
 end;

 procedure TForm1.Edit4KeyPress(Sender: TObject; var Key: Char);
 begin
   if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
 end;

 procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
 begin
    if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
 end;

 procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char);
 begin
    if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
 end;

 procedure TForm1.Edit6KeyPress(Sender: TObject; var Key: Char);
 begin
   if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
 end;

 procedure TForm1.Edit7KeyPress(Sender: TObject; var Key: Char);
 begin
   if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
 end;

można znacznie skrócić - napisz to dla jednego edita, a w Inspektorze Obiektów wybierz to zdarzenie dla pozostałych pól;


I na przyszłość pamiętaj, żeby wklejać problematyczny kod, nie cały kod modułu, a także wstawiaj kod w specjalne znaczniki <code class="delphi"></code>

0
try
   a := StrToInt(Edit1.Text);
except
  MessageBox('W polu Edit1 powinna być liczba',mtError,[mbOk],0);
  Exit;
end;
0

Dzięki Furious Programming za szybką reakcję i pomoc.

"A jeśli chodzi o kod, to tą część:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9',#8,'-']) then Key:=#0;
end;"

poprawiłem i jest ok

co do :
"Możesz to wykonać na zasadzie "drabinki if'ów", lub wykorzystać tablicę z komunikatami i w pętli za pomocą choćby FindComponent (bo widzę, że nie nadajesz swoich identyfikatorów kontrolkom) sprawdzać ich zawartość, i jeśli EditX.Text = '' wyświetlić komunikat, aktywować puste pole edycyjne i wyjść z procedury/zdarzenia;"

przepraszam ale nie mogę się w tym odnaleźć. za dużo pojęć, o których muszę jeszcze doczytać.

Dzięki również za pomoc _13th_Dragon`owi

procedura

"try
a := StrToInt(Edit1.Text);
except
MessageBox('W polu Edit1 powinna być liczba',mtError,[mbOk],0);
Exit;
end;"

daje radę i działa zmineniłem tylko właściwości Messagebox

do editow
a := StrToInt(Edit1.Text);
b := StrToInt(Edit2.Text);
c := StrToInt(Edit3.Text);
d := StrToInt(Edit4.Text);
l := StrToInt(Edit7.Text);

a napisałem to tak na przykładzie :

try
a := StrToInt(Edit1.Text);
except
MessageBox(Handle,'Brak danych11 ','Macierze',MB_IconError);
Edit1.SetFocus;
Exit;

nie działa tylko przy ostatnim z pól edit

     k := StrToInt(Edit6.Text);

przy warunku:

if (ComboBox1.ItemIndex = 1 ) then begin

     a := StrToInt(Edit1.Text);
     b := StrToInt(Edit2.Text);
     c := StrToInt(Edit3.Text);
     d := StrToInt(Edit4.Text);
     l := StrToInt(Edit7.Text);
     k := StrToInt(Edit6.Text);

     f :=((a+b)*k);
     g :=((c+d)*l);
     Edit8.Text := IntToStr(f);
     Edit10.Text := IntToStr(g);
     end;

Co może być przyczyną ?
Jeszce raz dzięki za wsparcie .

0

Ja w tym kodzie nie widzę wyłapywania wyjątku. Poza tym wyjątek czasami może pokazac się tylko po uruchomieniu programu z poza IDE. Radził bym użyć tego http://4programmers.net/Delphi/Trystrtoint a o FindComponent sobie poczytać. Na google jest o tym mnóstwo informacji. A jeśli tak stronisz od google i o wszystko pytasz, to pytaj też wbudowanego helpa. Natomiast nazywanie zmiennych poza liczbowych iteratorami jedną literką oraz pól edycyjnych od Edit1 do na przykład Edit31337, według mnie jest złym rozwiązaniem i złym nawykiem. Popraw to.

0

Nie podałeś jak to u ciebie teraz wygląda, tylko na jedno pole nie działać nie może, więc prawdopodobnie coś sknociłeś.

0

Mam wkleić cały kod programu?

0

Tak to teraz wygląda .

 

procedure TForm1.Button1Click(Sender: TObject);

var
a,b,c,d : Integer; //macierz
w : Integer; //wyznacznik
l,k : Integer; //liczba;wektor
f,g,h,i : Integer; // wynik

begin

try
a := StrToInt(Edit1.Text);
except
MessageBox(Handle,'Brak danych11 ','Macierze',MB_IconError);
Edit1.SetFocus;
Exit;
end;

try
b := StrToInt(Edit2.Text);
except
MessageBox(Handle,'Brak danych12 ','Macierze',MB_IconError);
Edit2.SetFocus;
Exit ;

end;

try
c := StrToInt(Edit3.Text);
except
MessageBox(Handle,'Brak danych21 ','Macierze',MB_IconError);
Edit3.SetFocus;
Exit ;

end;

try
d := StrToInt(Edit4.Text);
except
MessageBox(Handle,'Brak danych22 ','Macierze',MB_IconError);
Edit4.SetFocus;
Exit ;

end;

    if ( RadioButton1.Checked = True ) then begin

     a := StrToInt(Edit1.Text);
     b := StrToInt(Edit2.Text);
     c := StrToInt(Edit3.Text);
     d := StrToInt(Edit4.Text);


     w := (a*d)-(b*c);

     Edit5.Text := IntToStr(w)

end else
try
l := StrToInt(Edit7.Text);
except
MessageBox(Handle,'Brak Wartosci ','Macierze',MB_IconError);
Edit7.SetFocus;
Exit ;
end;

    if ( RadioButton2.Checked = True ) then Begin

     a := StrToInt(Edit1.Text);
     b := StrToInt(Edit2.Text);
     c := StrToInt(Edit3.Text);
     d := StrToInt(Edit4.Text);


     l := StrToInt( Edit7.Text);
     f := (a*l);
     g := (b*l);
     h := (c*l);
     i := (d*l);

     Edit8.Text := IntToStr(f);
     Edit9.Text := IntToStr(g);
     Edit10.Text := IntToStr(h);
     Edit11.Text := IntToStr(i);

end;

if (ComboBox1.ItemIndex = 1 ) then begin
try
k := StrToInt(Edit6.Text);
except
MessageBox(Handle,'Brak Wartosci ','Macierze',MB_IconError);
Edit6.SetFocus;
Exit ;
end;

     a := StrToInt(Edit1.Text);
     b := StrToInt(Edit2.Text);
     c := StrToInt(Edit3.Text);
     d := StrToInt(Edit4.Text);
     l := StrToInt(Edit7.Text);
     k := StrToInt(Edit6.Text);

     f :=((a+b)*k);
     g :=((c+d)*l);
     Edit8.Text := IntToStr(f);
     Edit10.Text := IntToStr(g);
     end;

end;

0

Jak sformatujesz kod po ludzku to sam zobaczysz błąd. Po ludzku oznacza że wizualnie widać jaka instrukcja w jakim if'e/else/for'ze/try'ju siedzi dana instrukcja.

1 użytkowników online, w tym zalogowanych: 0, gości: 1