PDA

View Full Version : Help in Tab in user form



dansam
02-19-2007, 10:33 PM
Hi
I have a tab control on my user form. I want help that how to use it.
I want that user click on ?tab1? then it shows a massage box called ?tab1 activated ?
And adds a control called textbox 1,
And when user clicks on tab2 then it shows a massage box called ?tab2 activated ?
And adds a control called textbox 2,

How to do it? :think:

I have a sample workbook attached here.

Bob Phillips
02-20-2007, 02:20 AM
Private Sub TabStrip1_Change()
Select Case Me.TabStrip1.Value
Case 0: MsgBox "Tab #1 activated"
Case 1: MsgBox "Tab #2 activated"
End Select
End Sub


As for thetextboxes, I don't subscribe to adding controls dynamically, I prefer to add them at design time and hide/show them as required.

dansam
02-20-2007, 02:26 AM
hi xld
It worked ! Thanks.

dansam
02-20-2007, 03:05 AM
Hi,
how to activate the tab once before the Currant tab ?
for eample when user clicks on tab2 I want to activate tab1
and when user click on tab3 I want activate tab2........

Have you any idea?

moa
02-20-2007, 03:28 AM
Trying to mess with your users' heads?:)


Just set the tabstrip value to whichever tab you want e.g
Me.TabStrip1.Value =2

Bob Phillips
02-20-2007, 03:31 AM
Why? You could then only get to tab2 by clicking tab 3, and never get to tab 3. Seems daft to me.

moa
02-20-2007, 03:32 AM
Oh wait, infinite loop.

Bob Phillips
02-20-2007, 03:41 AM
Shouldn't do if it is click invoked, and you control re-entry.

moa
02-20-2007, 03:53 AM
Does a Tabstrip have a click event?

Not sure.

Must do.

moa
02-20-2007, 03:56 AM
Ah yes, you need send a parameter when it's called.

Bob Phillips
02-20-2007, 03:57 AM
Yes it does, but you are clicking the tab itself (as it is a tabstrip), which is how you select atab, which is why I think the request is just plain daft, you click tab2 to go to tab2 and you go to tab1!

moa
02-20-2007, 04:08 AM
Well I guess we'll find out why he wants to do it but I'm with you on this; sounds like a bad idea.

dansam
02-20-2007, 05:16 AM
Hi,
I am trying to make a tabed browsing
Control , so like you have in IE 7 , I have 2 tabs on that ,the first in tab1 the last one is as “ +” sign so when user clicks on it adds a tab between the tab1 and “+” , I have the following code , but the problem is that the code doesn’t activate the last added tab , so what to for it ?

something is wrong , so help me !


Private Sub TabStrip1_Change()

Dim a As Integer
a = Me.TabStrip1.Tabs.Count
If TabStrip1.Value = a-1 Then
TabStrip1.Tabs.Add "", "page 2", a - 1
End If
End Sub

Bob Phillips
02-20-2007, 05:29 AM
You mean tabbed browsing as in Firefox, as it has been for a long time in FF.



Private Sub TabStrip1_Change()
Dim cTabs As Integer
With Me.TabStrip1
cTabs = .Tabs.Count
If .Tabs(cTabs - 1).Caption = "+" Then
.Tabs.Add "", "page " & cTabs, cTabs - 1
End If
End With
End Sub

dansam
02-20-2007, 06:10 AM
You mean tabbed browsing as in Firefox, as it has been for a long time in FF.


Exactly

But in your example, it inserts tabs both time , when I click on tab 1 and when I click on “+” , I just want to add a tab BETWEEN these two tabs only when user clicks on “+” and then to activate the ADDED Tab .

What’s the way?

Regards,
Dan

dansam
02-20-2007, 06:16 AM
this works fine :
Private Sub TabStrip2_Change()
Dim cTabs As Integer
With Me.TabStrip2
cTabs = .Tabs.Count
If TabStrip2.Value = cTabs - 1 Then
.Tabs.Add "", "page " & cTabs, cTabs - 1
End If
End With

End Sub


but what to do to activate the added tab ?

Bob Phillips
02-20-2007, 06:48 AM
Private Sub TabStrip1_Click(ByVal Index As Long)
Dim cTabs As Integer
With Me.TabStrip1
cTabs = .Tabs.Count
If .Index = cTabs - 1 Then
.Tabs.Add "", "page " & cTabs, cTabs - 1
.Value = cTabs - 1
End If
End With

End Sub

moa
02-20-2007, 07:07 AM
I think you need

If .SelectedItem.Index = cTabs - 1 Then

moa
02-20-2007, 07:42 AM
Or drop the "." in front of Index

dansam
02-20-2007, 08:19 AM
Hi every one,
That code works fine in all the ways. Now I want to stop user if the count of total tabs (cTabs) are greater than 20 (I mean no to let the user to open more than 12 tabs)

Thanks,
Dan

moa
02-20-2007, 08:47 AM
Private Sub TabStrip1_Click(ByVal Index As Long)
Dim cTabs As Integer
With Me.TabStrip1
cTabs = .Tabs.Count

If Index = cTabs - 1 Then
If cTabs > 20 Then
.Value = cTabs - 2
MsgBox Prompt:="You cannot open anymore tabs.", Title:="Maximum number of tabs has been reached", Buttons:=vbExclamation
Else
.Tabs.Add "", "page " & cTabs, cTabs - 1
.Value = cTabs - 1
End If
End If
End With

End Sub

dansam
02-20-2007, 09:08 AM
Hi moa,
Thanks and any idea to add a right click menu to a tab that contains (1) Add tabs and (2) close tab (or close all tabs) ?
and is it possible to add some pictures to the caption of the tab??

Thanks
Dan

Bob Phillips
02-20-2007, 09:29 AM
Why don't you state the whole requirement at the start rather than drip-feeding it, changing everything we have done along the way, and wasting time!




Private Sub Userform_Activate()
'Remove any old instance of MyPopUp
On Error Resume Next
CommandBars("MyPopUp").Delete
On Error GoTo 0

With CommandBars.Add(Name:="MyPopUp", Position:=msoBarPopup)
With .Controls.Add(Type:=msoControlButton)
.OnAction = "AddTab"
.Caption = "Add Tab"
End With
With .Controls.Add(Type:=msoControlButton)
.OnAction = "RemoveTab"
.Caption = "Remove Tab"
End With
End With
End Sub

Private Sub TabStrip1_MouseUp(ByVal Index As Long, ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then
Application.CommandBars("MyPopUp").ShowPopup
End If
End Sub


and in a standard code module



Public Sub AddTab()
Dim cTabs As Long
With UserForm1.TabStrip1
cTabs = .Tabs.Count

If cTabs > 20 Then
.Value = cTabs - 2
MsgBox Prompt:="You cannot open more tabs", Title:="Maximum number of tabs has been reached", Buttons:=vbExclamation
Else
.Tabs.Add "", "page " & cTabs, cTabs - 1
.Value = cTabs - 1
End If
End With
End Sub

Public Sub RemoveTab()
Dim cTabs As Long
With UserForm1.TabStrip1
cTabs = .Tabs.Count

If cTabs = 1 Then
.Value = 0
MsgBox Prompt:="You must have at least one tab", Title:="Minimum number of tabs has been reached", Buttons:=vbExclamation
Else
.Tabs.Remove .Value
' .Value = cTabs - 1
End If
End With
End Sub