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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem With Setting Tab Focus

Status
Not open for further replies.

TBaz

Technical User
Jun 6, 2000
65
ES
I have an MDI app in Delphi 5 and through necessity have a TTabSet component on the parent form.

When the first new child window is created a new 'Untitled' tab appears correctly with focus, but when subsequent child windows are created the named tab appears, but the first one retains focus. I can't for the life of me figure out how to make the new tab have focus.

Also related is the fact that I can't figure out how to set the focus on the correct tab when a child window is selected. No doubt that the answer to the first problem will also solve the second.

Thanks for any advice in advance...

Barrie
 
Doesnt TTabSet have a TabIndex property? Can you not simply set the property to the index of the newly created tab (which presumably will be TTabSet.Tabs.Count-1???
 
It does, but it appears to be a read-only value instigated by clicking on one of the tabs.

I'm probably not doing something ridiculously simple here...

Barrie
 
hmmm...I'm running delphi 6 and the TTabSet.TabIndex can be set manually....wierd
 
Am working with Delphi 5 and the following code sets the focus to the last Tab.

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
TabSet1.TabIndex := TabSet1.Tabs.Count-1;
end;

You may have a problem elsewhere?

Andrew
 
With you so far Andrew...

But, what if you open three child windows and click on say the first window again to work on it?

What I'm having problems is finding out what tab should then be focused.

I'll try the Count-1 code again - wouldn't work for me before, but it was the early hours of the morning when I thought I was doing it properly so I wouldn't rule out me being at fault.

Any takers on the 'click on a window and highlight the correct tab' side of things please?

Regards...

Barrie
 
when you create said windows, I assumue you are not recording what tabindex they belong to???

if not, you should!!!! would make things much easier...either use a list and match the Form's pointer to the tabindex, or simply set the forms Tag property to the index of the tab it belongs to
 
Sorry...posted before I finished my last one :)

Once you have the Form.Tag property set to the appropriate index, assuming you can then work out how to manually change the tab, then 'bobs your uncle!' (or whatever passes for an appropriate saying in your part of the planet)
 
A question for you Barrie.

How is the user 'clicking' on a different child window??? I assume that you have some tabs as in my amazing ascii diagram below (best viewed a fixed width font):

[tt]
---------
/ Tab1 \ -----------
/ | Tab 2 |-------------------------------
| |
| |
| |
| |
| |
| |
| |
| |
| |
---------------------------------------------------------
[/tt]

When the user clicks on tab1, ChildWindow1 is displayed...once it is displayed, there is no way to click on ChildWindow2 as it is either behind childwindow1 or has been hidden. Do you dock the windows? or do you set the ChildWindow.Parent = Tab1Panel????
 
The MDI child windows are tiled on the main form and the tabset runs across the top of it. The idea is that you can open multiple documents and have say two visible and copy/paste between them.

The tabs would bring one of the otherwise hidden windows to the front.

If I have four windows open in a quadrant pattern, I want the correct tabs to highlight when you click on each of the four child windows.

Sorry - as I said, this is one of those problems which isn't easy to put into words. It's much easier if you could see my screen! :)

Barrie

 
I'm with you now :)

You need to associate the child window with a tab index.

I can give you two obvious solutions:

1) When you create the MDI child form, set its Tag property to the index of the newly created tab.
2) Add a property to all of your MDIChildren called TabIndex and set that as either part of the constructor or once it has been created.
3) Maintain a list of MDIChildren using either an array of TObject or a TList...The index of the item within the list will match with the tab index that you require as you add new windows to the end of the tab list...
 
OK thanks - I'll give that a try in the morning - pretty bushed now.

I'll report back with how I get on! :)

Barrie
 
The tabs now highlight correctly, so one problem down, one to go!

I'm using the Tag option to record the MDI child number with:

procedure TMainForm.CreateMDIChild(const Name: string);
var
Child: TMDIChild;
begin
{ create the new MDI child window }
Child := TMDIChild.Create(Application);
Child.Caption := Name;
Child.Tag := MDIChildCount;
if FileExists(Name) then
Child.DCMemo1.Lines.LoadFromFile(Name);
end;

Now, if I create say 4 child windows, when I click on the tabs across the top of the main form, they now set focus and come to the top (unlike before), but if you click on the tabs at random, different windows are affected.

I'm using the code:

procedure TMainForm.TabSet1Change(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
begin
// Clicked On A Tab...
If TabSet1.TabIndex<>-1 Then
Begin
MDIChildren[TabSet1.TabIndex].SetFocus;
MDIChildren[TabSet1.TabIndex].Show;
Edit4.Text:=IntToStr(TabSet1.TabIndex);
End;
end;

I put the Edit4 component on the form so I could see what value was being sent and it's the same each time so i guess that the child window is moving position in the list array.

Would you credit it - lost again! :)

Barrie

 
procedure TMainForm.TabSet1Change(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
begin
// Clicked On A Tab...
If TabSet1.TabIndex<>-1 Then
Begin
MDIChildren[TabSet1.TabIndex].SetFocus;
MDIChildren[TabSet1.TabIndex].Show;
Edit4.Text:=IntToStr(TabSet1.TabIndex);
End;
end;

If you look at the header of the procedure, you will see that the NewTab index is passed in....I would imagine that this is done so that the TabSet1.TabIndex is still the last tab you were pointing at. Try:

procedure TMainForm.TabSet1Change(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
begin
// Clicked On A Tab...
if (NewTab <> -1) then
begin
MDIChildren[NewTab].SetFocus;
MDIChildren[NewTab].Show;
Edit4.Text:=IntToStr(NewTab);
end;
end;
 
How about smthng like this?

Code:
procedure TMainForm.TabSet1Change(Sender: TObject; NewTab: Integer; var AllowChange: Boolean);
var i : integer;
begin
  // Clicked On A Tab...
If NewTab <> -1 Then
  for i := 1 to TabSet1.TabCount do
    if MDIChildren[i].Tag = NewTab then
    Begin
      MDIChildren[i].SetFocus;
      MDIChildren[i].Show;
      Edit4.Text:=IntToStr(NewTab);
    End;
end;

HTH
TonHu
 
Ah, Bill was just a little bit quicker (and completer) ;-)
 
Sorry gents, but still no joy.

I tried both of your suggestions and neither worked, (both did the same thing wrong). Here's exactly what I did and what it does:

1. Created 2 untitled MDI child windows:

1st: Tab correctly named, focused and 'Untitled1' window appears. Edit4 contains 0.
2nd: Tab correctly named, focused and 'Untitled2' window appears. Edit4 contains 1.

2. Tiled windows vertically - window 2 and tab 2 highlighted.

3. Clicked on tab 1 - Tab 1 highlighted and window 1 highlighted. Edit4 contains 0.

4. Clicked on tab 2 - Tab 1 highlighted but window 1 focused only - not selected. Edit4 contains 1.

After playing it seems that Setfocus doesn't highlight the window itself - it just turns on the cursor for text input. Show makes the window current (as if it had been clicked on).

Clicking the tabs in turn it looks like the 'show' is playing catch-up, (the window itself highlights one click behind the click which sets focus). Crikey this is getting harder to explain!

I was wondering if it's anything to do with using the OnChange event instead of the OnClick event. If I change it, then you don't get the NewTab like you do with OnChange.

Barrie



 
In the OnChange event, the TabIndex will be current.

HTH
TonHu
 
Yes, and I'm now back to square one (how I had it originally not working before I altered it to the OnChange event). :)

It still does wrong what is outlined above.

I'll battle on with it though - it's not going to beat me! :)

Barrie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top