Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change the Button color in Main Form by change value of CheckBox

Status
Not open for further replies.

EZLI

IS-IT--Management
Sep 3, 2002
31
LV
Hi,
How to change Button1 color placed in Main Form by checking CheckBox1 which is
placed in Slave Form?

Thank's in advance.
 
Hi. The first and simpliest way is to add the Mainform in the save's uses clause and then access the button1 by Form1.Button1.Color := clRed


The cooler way is to override the slaveform's contructor to pass the ownerform property. Then you assign the ownerform vaiable to a local variable in the slaveform, and then access the buttoncolor by using properties....

Shout for code example... :)

-----------------------------------------------------
-"There is always a different way to solve it"

//Nordlund
 
Thank You.
Here are codes below

unit MUnit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.dfm}
uses
Sunit2;
procedure TForm1.Button1Click(Sender: TObject);
begin
SUnit2.Form2.ShowModal
end;

end.

unit SUnit2;

interface

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

type
TForm2 = class(TForm)
Button1: TButton;

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

var
Form2: TForm2;

implementation

{$R *.dfm}

end.
 
Try this:
Code:
unit SUnit2;

interface

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

type
  TForm2 = class(TForm)
    CheckBox1: TCheckBox;
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox1KeyUp(Sender: TObject; 
                var Key: Word; Shift: TShiftState)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses MUnit1;

//handle the change through a mouse click
procedure TForm2.CheckBox1Click(Sender: TObject);
begin
  if CheckBox.Checked then
    MUnit1.Form1.Button1.Font.Color := crGreen
  else
    MUnit1.Form1.Button1.Font.Color := crBlack;
  application.processmessages; 
end;

//handle the change through the keyboard
procedure TForm2.CheckBox1KeyUp(Sender: TObject; 
                var Key: Word; Shift: TShiftState);
begin
  if Key = VK_SPACE then  //spacebar to change
    CheckBox1Click(Sender);
end;

end.

-Dell
 
Hi.
I did a little bit more complex solution, just for fun...

Add a TLabel and a TButton on Form1
Add a TEdit and a TButotn on Form2

Use this codeexample to manipulate the Form1's objects.

(The best way would be to declare the class in it's own unit, but I didn't do that....

Form1:
Code:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  Unit2;

procedure TForm1.Button1Click(Sender: TObject);
var
  myButtonHolder: TButtonClass;
begin
  myButtonHolder := TButtonClass.Create;
  myButtonHolder.AButton := Button1;
  myButtonHolder.ALabel := Label1;

  with TForm2.Create(Application) do
  try
    Setup(myButtonHolder);
    ShowModal;
  finally
    Free;
  end;
end;

end.

Form2:
Code:
unit Unit2;

interface

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

type
  TButtonClass = class
  private
    FButton: TButton;
    FLabel: TLabel;
  public
    property AButton: TButton read FButton write FButton;
    property ALabel: TLabel read FLabel write FLabel;
  end;

  TForm2 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Edit1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    ButtonHolder: TButtonClass;
  public
    procedure Setup(bh: TButtonClass);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

{ TForm2 }

procedure TForm2.Setup(bh: TButtonClass);
begin
  ButtonHolder := bh;
end;

procedure TForm2.Edit1Change(Sender: TObject);
begin
  ButtonHolder.ALabel.Caption := Edit1.Text;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  ButtonHolder.AButton.Caption := Edit1.Text;
end;

end.


-----------------------------------------------------
-"There is always a different way to solve it"

//Nordlund
 
Thank's both for help.
It's working.

I have another question.
How to change Form1 Backgrond color as like in Web Site
choosing Theme.
Bob
 
I think you have to explain a little bit more...

-----------------------------------------------------
-"There is always a different way to solve it"

//Nordlund
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top