窗体调用设焦点问题(40分)

  • 主题发起人 主题发起人 huzhaowei116
  • 开始时间 开始时间
H

huzhaowei116

Unregistered / Unconfirmed
GUEST, unregistred user!
现在有两个窗体,form1和form2,form1中有两个按钮,点击后分别显示
form2,form2中有一个PageControl1,中有两个TabSheet,现在要在form1
中点击button1,form2显示,且tabshee1为焦点,即显示 tabsheet1页,
点击button2,form2显示,且tabshee1为焦点,即显示 tabsheet2页,
我直接在form1的button1的click中写tabsheet1.SetFocus是不行的,那
该如何。
 
在Form2所在的单元中定义一个变量No,Form1应该已经Uses了Form2,
在Button1的OnClick事件中令Form2.No:=1;
在Button2的OnClick事件中令Form2.No:=2;
上面的赋值语句加在Form2.Show之前,
在Form2.OnShow事件中写下面代码
PageControl1.ActivePage:=No;

 
更正一下,是PageControl.ActivePageIndex:=No-1;
OK,已经试验成功:)
 
代码贴给你:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.No:=1;
Form2.Show;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Form2.No:=2;
Form2.Show;
end;

end.

unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls;
type
TForm2 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
No:Integer;
end;

var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.FormShow(Sender: TObject);
begin
PageControl1.ActivePageIndex:=No-1;
end;

end.

 

Similar threads

回复
0
查看
804
不得闲
D
回复
0
查看
825
DelphiTeacher的专栏
D
D
回复
0
查看
831
DelphiTeacher的专栏
D
D
回复
0
查看
664
DelphiTeacher的专栏
D
后退
顶部