![]() |
|
|
|||||||
| Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
![]() |
|
|
Опции темы | Поиск в этой теме | Опции просмотра |
|
#1
|
|||
|
|||
|
Пишу вот для обучения маленькую программку для расчета сопротивления электрической цепи для параллельного и последовательного соединения сопротивлений. Для выбора способа соединения (последовательного или параллельного) использую RadioButton. Так вот почему то при нажатии button1 расчитыватся только RadioButton2.checked, и совсем не расчитывается RadioButton2.checked. Где ошибке не могу понять.
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask;
type
TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
rb1: TRadioButton;
rb2: TRadioButton;
btn1: TButton;
lbl1: TLabel;
procedure edt1KeyPress(Sender: TObject; var Key: Char);
procedure edt2KeyPress(Sender: TObject; var Key: Char);
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
R1,R2,R:Real;
implementation
{$R *.dfm}
procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
begin
case Key of
'0'..'9', #8: ;
',', '.':
begin key:=DecimalSeparator;
if Pos(DecimalSeparator, edt1.Text)<>0 then
Key:=chr(0);
end;
#13 : Edt2.SetFocus;
else Key:=chr(0);
end;
end;
procedure TForm1.edt2KeyPress(Sender: TObject; var Key: Char);
begin
case Key of
'0'..'9', #8: ;
',', '.':
begin key:=DecimalSeparator;
if Pos(DecimalSeparator, edt2.Text)<>0 then
Key:=chr(0);
end;
#13 : btn1.SetFocus;
else Key:=chr(0);
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
if (Edt1.Text = '') then
begin
Edt1.SetFocus;
ShowMessage(' ');
Exit;
end;
R1:=StrToFloat(edt1.text);
if (Edt2.Text = '') then
begin
Edt2.SetFocus;
ShowMessage(' ');
Exit;
end;
R2:=StrToFloat(edt2.Text);
if rb1.Checked then
if R1+R2=0 then
begin
ShowMessage(' ');
Exit;
end;
begin
R:=R1+R2 ;
end;
if rb2.Checked then
if R1+R2=0 then
begin
ShowMessage('Îäíî èç çíà÷åíèé ðàâíî 0');
Exit;
end;
begin
R:=(R1*R2)/(R1+R2);
end;
lbl1.Caption:=FloatToStr(R);
end;
end. |
|
#2
|
||||
|
||||
|
Очепятка, однако:
Код:
procedure TForm1.btn1Click(Sender: TObject);
begin
R:=0; //к/з
if (Edt1.Text = '') then
begin
Edt1.SetFocus;
ShowMessage(' ');
Exit;
end;
R1:=StrToFloat(edt1.text);
if (Edt2.Text = '') then
begin
Edt2.SetFocus;
ShowMessage(' ');
Exit;
end;
R2:=StrToFloat(edt2.Text);
if rb1.Checked then
if R1+R2=0 then
begin
ShowMessage(' ');
Exit;
end
else
begin
R:=R1+R2 ;
end;
if rb2.Checked then
if R1+R2=0 then
begin
ShowMessage('Одно из значений равно 0');
Exit;
end
else
begin
R:=(R1*R2)/(R1+R2);
end;
lbl1.Caption:=FloatToStr(R);
end; |
|
#3
|
|||
|
|||
|
Ну елки палки! Точно! Вот в такие моменты чувствую себя полным идиотом. Спасибо большое!!!
![]() |
|
#4
|
|||
|
|||
|
и еще вопрос в догонку. нужно в edit запретить ввод 2х нулей до запятой т.е. чтобы нельзя было ввести 00,1 или 000,1.
нашел вариант в книге: Код:
if edt1.text <> '0' then edt1.text:= edt1.text + '0'; |