Help in Tab Managment

Please discuss general Delphi programming topics here.

Help in Tab Managment

Postby ham » August 16th, 2010, 1:51 pm

Hi,

Please see this sample project. ( http://rapidshare.com/files/413281949/pr_2.rar)

In this project we have two frame's (frame2 , frame3) and a form (form1).

i want to create frames with the click button's in the form1 and the create tabs in the TabControl1, when click in tab's in TabControl then goto that frames.
Also when click exit button in frame then remove Tab in TabControl.


thank you.
ham
Active Member
Active Member
 
Posts: 9
Joined: August 16th, 2010, 1:37 pm

Re: Help in Tab Managment

Postby Kambiz » August 17th, 2010, 9:27 am

Here is the right code for the main unit:

Code: Select all
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    TabControl1: TTabControl;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  unit2, unit3;

{$R *.dfm}

procedure TForm1.Notification(AComponent: TComponent; Operation: TOperation);
var
  Index: Integer;
  TabName: String;
begin
  inherited Notification(AComponent, Operation);
  if (ComponentState * [csLoading, csDestroying]) = [] then
    case Operation of
      opInsert:
      begin
        if AComponent is TFrame2 then
          TabName := 'Frame 2'
        else if AComponent is TFrame3 then
          TabName := 'Frame 3'
        else
          Exit; // it is not one of our frames
        Index := TabControl1.Tabs.AddObject(TabName, AComponent);
        TabControl1.TabIndex := Index;
      end;
      opRemove:
      begin
        Index := TabControl1.Tabs.IndexOfObject(AComponent);
        if Index >= 0 then
          TabControl1.Tabs.Delete(Index);
      end;
    end;
end;

procedure TForm1.TabControl1Change(Sender: TObject);
begin
  TControl(TabControl1.Tabs.Objects[TabControl1.TabIndex]).BringToFront;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TFrame2.Create(Self) do
  begin
    Name := ''; // to be able to have more instances
    Parent := Self;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  with TFrame3.Create(Self) do
  begin
    Name := ''; // to be able to have more instances
    Parent := Self;
  end;
end;

end.

Notice that owner of the created frames are the form, not the application.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Help in Tab Managment

Postby ham » August 17th, 2010, 12:27 pm

Thank you Kambiz.

We can create frames for two (or more) times??
ham
Active Member
Active Member
 
Posts: 9
Joined: August 16th, 2010, 1:37 pm

Re: Help in Tab Managment

Postby Kambiz » August 17th, 2010, 1:59 pm

When we create another instance of a frame, Delphi complains about duplicate component name and raises an exception. To get rid of this exception, before assigning the Parent property, we should give the component a unique name or set the name to an empty string.

Code: Select all
with TFrame2.Create(Self) do
begin
  Name := ''; // to be able to have more instances
  Parent := Self;
end;

I modified the original code to be able to have several instance of each frame.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Help in Tab Managment

Postby ham » August 19th, 2010, 8:26 am

Hi,

Very thank's for your reply.

I have 2 problem in this project.

1.When we click Button1 and Button2 in Form1, then create 2 tab's in TabControl. If "Frame 2" tab is selected and we click exit button in frame2 then automatically "Frame

1" tab select. but if "Frame 1" tab is selected and we click exit button in frame1, then "Frame 2" tab can not select.

This means : when 2nd Tab in TabControl is selected, when we exit this frame then first tab in TabControl automatically selected. and if First tab in TabControl is seleted

and we want to exit it, then select next tab (2nd) in TabControl.


2.When any TabControl is not any tab, this means any frames not created.Thereover Tabcontrol must not visibe for user. but now user can TabContol when haven't any

Tab.

I modified the your code to be able solve these problems but that codes don't work propertly. (download : http://rapidshare.com/files/413831113/pr_22.rar )

Really: my purpose of this sample project is, i want to make a multi-tab program like firefox. you have any better idea for it??
ham
Active Member
Active Member
 
Posts: 9
Joined: August 16th, 2010, 1:37 pm

Re: Help in Tab Managment

Postby Kambiz » August 19th, 2010, 5:38 pm

I read your post several times but I couldn't get what you mean, sorry.

By the way, please attach the files/images to your post (use Upload attachment). It's easier for others to get/see them.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Help in Tab Managment

Postby ham » August 19th, 2010, 9:55 pm

Kambiz wrote:I read your post several times but I couldn't get what you mean, sorry.

By the way, please attach the files/images to your post (use Upload attachment). It's easier for others to get/see them.


Hi,
I solve first problem in my previous post

First please download attach file. but now i have 3 problem.

Problem A: i want to show TabControl only it have minimum 1 tab. If TabControl don't have tabs then must unvisible to user.

Problem B: how we can to create a button in Tabs for exit it? like Firefox. (see pic)

Problem C: how we can to use Middle mouse button for goto next or previous tabs??
Attachments
pr_23.rar
(13.08 KiB) Downloaded 126 times
close.JPG
close.JPG (1.95 KiB) Viewed 3891 times
ham
Active Member
Active Member
 
Posts: 9
Joined: August 16th, 2010, 1:37 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 3 guests

cron