Recent

Author Topic: No more changes in TEdit after activate TButton  (Read 5351 times)

ThomasK1201

  • New Member
  • *
  • Posts: 31
No more changes in TEdit after activate TButton
« on: March 06, 2015, 01:10:55 pm »
Hi guys

I want to a TEdit to change from ReadOnly = False to ReadOnly = True if a button is activated.
How can I make this happen?
I think it should be something like this:

Code: [Select]
If Button = Activated
then Edit.ReadOnly := True;

But I dont know exactly how to do this and I cant find it by using Google..

Thomas.

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: No more changes in TEdit after activate TButton
« Reply #1 on: March 06, 2015, 01:54:50 pm »
Put a ToggleBox component on the form and put this in its OnChange event:
Code: [Select]
procedure TForm1.ToggleBox1Change(Sender: TObject);
begin
  Edit1.Enabled := not ToggleBox1.Checked;
// Better use Edit1.Enabled than Edit1.ReadOnly, because of visual appearance
// or Edit1.ReadOnly := ToggleBox1.Checked;
end;

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: No more changes in TEdit after activate TButton
« Reply #2 on: March 06, 2015, 08:48:35 pm »
Well, the problem is that the users of the form shouldnt be able to change from enabled to disabled.
For school I make a program that students of lower classes can use to practice ciphering (Im not sure if this is the right translation, my English isnt perfect at all). And after putting the answers in de Edits and confirming them, they shouldnt be able to change the values they entered. And a togglebox can be enabled and disabled. So when students disable the togglebox they can change their entered values.

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: No more changes in TEdit after activate TButton
« Reply #3 on: March 06, 2015, 08:52:09 pm »
And I have a second question:
I made a calculator for the program in Lazarus. I placed Buttons for every number and for characters like + - =, etc. and a Edit. How can I make those buttons add a number instead of replacing the previous number. So when I first click on 1, the Edit shows 1. But after that I click 2, and the 1 is gone and Edit only shows 2. How can I make happen that Edit shows 12?

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: No more changes in TEdit after activate TButton
« Reply #4 on: March 06, 2015, 08:58:00 pm »
Apparently, by 'activate TButton' you probably mean 'Button clicked' (i.e. when the user click the pushbutton with mouse or keyboard).

In this case, use the 'OnClick' event of your pushbutton to insert your code. Or just double-click on the concerned pushbutton in the Lazarus IDE: the concerned event/procedure will be proposed to you automatically (it's the "by-default" event for a pushbutton).

For instance, for a form (Form1) with a edit control (Edit1) and a pushbutton (Button1), this gives:

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Enabled := False;;
end;

end.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: No more changes in TEdit after activate TButton
« Reply #5 on: March 06, 2015, 08:58:59 pm »
And I have a second question:
I made a calculator for the program in Lazarus. I placed Buttons for every number and for characters like + - =, etc. and a Edit. How can I make those buttons add a number instead of replacing the previous number. So when I first click on 1, the Edit shows 1. But after that I click 2, and the 1 is gone and Edit only shows 2. How can I make happen that Edit shows 12?

Edit1.Text := Edit1.Text + 'your_new_number';

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: No more changes in TEdit after activate TButton
« Reply #6 on: March 06, 2015, 09:20:50 pm »
This gives you an example of one way in which a confirmation button (which can only be clicked once) can be enabled when entries are completed, which then prevents further editing. Generate an OnCreate event for a new blank form Lazarus project and complete it as follows:

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Forms, Controls, StdCtrls, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    LabeledEdit1: TLabeledEdit;
    procedure FormCreate(Sender: TObject);
  private
    EEntry1, EEntry2: TLabeledEdit;
    BEntriesCompleted: TButton;
    procedure BEntriesCompletedClick(Sender: TObject);
    procedure EEntryEditingDone(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);

  function NewEdit(const aCaption: string; aLeft, aTop: integer): TLabeledEdit;
  begin
    Result:=TLabeledEdit.Create(Self);
    Result.LabelPosition:=lpLeft;
    Result.EditLabel.Caption:=aCaption;
    Result.Left:=aLeft + Result.LabelSpacing + Canvas.TextWidth(aCaption);
    Result.Top:=aTop;
    Result.OnEditingDone:=@EEntryEditingDone;
    Result.Parent:=Self;
  end;

begin
  EEntry1:=NewEdit('Enter cipher 1:',10,10);
  EEntry2:=NewEdit('Enter cipher 2:',10,40);

  BEntriesCompleted:=TButton.Create(Self);
  BEntriesCompleted.Caption:='Click when entries are complete';
  BEntriesCompleted.Align:=alBottom;
  BEntriesCompleted.BorderSpacing.Around:=20;
  BEntriesCompleted.OnClick:=@BEntriesCompletedClick;
  BEntriesCompleted.Enabled:=False;
  BEntriesCompleted.Parent:=Self;
end;

procedure TForm1.BEntriesCompletedClick(Sender: TObject);
begin
  EEntry1.Enabled:=False;
  EEntry2.Enabled:=False;
  BEntriesCompleted.Enabled:=False;
end;

procedure TForm1.EEntryEditingDone(Sender: TObject);
begin
  if not (Sender is TLabeledEdit) then Exit;
  TLabeledEdit(Sender).Tag:=1;

  if (EEntry1.Tag + EEntry2.Tag >= 2) then
    BEntriesCompleted.Enabled:=True;
end;

end.

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: No more changes in TEdit after activate TButton
« Reply #7 on: March 06, 2015, 09:35:11 pm »
And then one more simple question: How can I change the color of the letters and numbers in Edits?

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: No more changes in TEdit after activate TButton
« Reply #8 on: March 06, 2015, 10:12:56 pm »
TEdit.Font.Color (you can change it in OI or in code).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: No more changes in TEdit after activate TButton
« Reply #9 on: March 09, 2015, 01:41:11 pm »
Thanks a lot guys!

 

TinyPortal © 2005-2018