Change Form Text When TabControl is Clicked.

emi_sastra
emi_sastra used Ask the Experts™
on
Hi,

I create a form with tabcontrol.

I load tabpage form below code.

Let's say we have 2 tabpage:

1. frmTMSUPPLIER.
2. frmTMCUSTOMER.

At the first load the form text =FormName & "View Mode"

When user click btnAdd then the parent form text =FormName & "Add Mode".

The problem is form 1 in "Add Mode" and form 2 in FormName & "View Mode".

When we switch the tabpage the form text does not change base on its form state.

How to solve this problem?

Thank you.







With frmTMSUPPLIER
            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
            .Dock = DockStyle.Fill
            .TopLevel = False
            .frmParentForm = Me    --> Change form text inside
            tbpSupplier.Controls.Add(frmTMSUPPLIER)
            .Show()
        End With

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Whenever you change the "mode" on one of your forms, save that value in the .Tag() Property:

    ' ...from within each "embedded" form (frmTMSUPPLIER/frmTMCUSTOMER)...
    Me.Tag = "Add Mode" ' <-- Set the Tag() prop whenever appropriate to keep track of the current mode

Then, when the Tab changes, grab the current TabPage, get the Form out of its Controls() Collection, and use the Tag() Property:

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Dim tp As TabPage = TabControl1.TabPages(TabControl1.SelectedIndex)
        Dim frm As Form = tp.Controls(0)
        Me.Text = frm.Name & " " & frm.Tag
    End Sub

Commented:
You can create a setting resource (currentmode as a string type) file under the project.  (by doubling clicking on the my project and selecting setting)

in the button click event when defining the mode use
My.Settings.Item("currentmodee") = "Add Mode" etc

under the event for the tab control use
Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Me.Text = ctype(TabControl1.SelectedTab.Controls(0),form).name  + " " +  My.Settings.currentmode
    End Sub



Author

Commented:
Hi miketayo,

I have not tried it yet.

Just more info :

At tabpage Supplier the parent form text will be : "MASTER - SUPPLIER (Mode)"
At tabpage Customer the parent form text will be : "MASTER - CUSTOMER (Mode)"

Using form.tag to save the state definitely work.

How about using setting as comment ? I still don't get it yet.

Thank you.

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
Hi Idle_Mind,

Let me try first.

Thank you.

Author

Commented:
Hi Idle_Mind,

  Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Dim tp As TabPage = TabControl1.TabPages(TabControl1.SelectedIndex)
        Dim frm As Form = tp.Controls(0)
        Me.Text = frm.Name & " " & frm.Tag
    End Sub

Index 0 is out of range.
Parameter name: index

Why always 0 index?

Thank you.

Author

Commented:
Please see my entire form code.

Thank you.
Option Explicit On
 
Public Class frmTabMasterSupplier
 
    Dim blnfrmMasterSupplier As Boolean = False
    Dim blnfrmMasterGroupSupplier As Boolean = False
    Dim strFormText As String = ""
 
    Dim strMainMenuId As String = ""
    Dim strMainMenuName As String = ""
    Dim strAppNo As String = ""
 
 
    Public Sub New(ByVal strMainMenuId As String, _
                  ByVal strMainMenuName As String, _
                  ByVal strAppNo As String)
 
        Me.InitializeComponent()
 
        Me.strMainMenuId = strMainMenuId
        Me.strMainMenuName = strMainMenuName
        Me.strAppNo = strAppNo
 
    End Sub
 
    Private Sub LoadMasterSupplier()
 
        Dim clsFormAuthority As New clsFormAuthority(ERV_Global.strLoginUserID, _
                                                  strAppId, _
                                                  strMainMenuId, _
                                                  strMainMenuName, _
                                                  strAppNo, _
                                                  "TMSUPPLIER", _
                                                  "MASTER - SUPPLIER")
 
        Dim frmTMSUPPLIER As New frmTMSUPPLIER(clsFormAuthority)
 
        With frmTMSUPPLIER
            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
            .Dock = DockStyle.Fill
            .TopLevel = False
            .frmParentForm = Me
            tbpSupplier.Controls.Add(frmTMSUPPLIER)
            .Show()
        End With
    End Sub
 
    Private Sub LoadGroupSupplier()
        Dim clsFormAuthority As New clsFormAuthority(ERV_Global.strLoginUserID, _
                                                 strAppId, _
                                                 strMainMenuId, _
                                                 strMainMenuName, _
                                                 strAppNo, _
                                                 "TMGROUPSUPPLIER", _
                                                 "MASTER - GROUP SUPPLIER")
 
        Dim frmTMGROUPSUPPLIER As New frmTMGROUPSUPPLIER(clsFormAuthority)
 
        With frmTMGROUPSUPPLIER
            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
            .Dock = DockStyle.Fill
            .TopLevel = False
            .frmParentForm = Me
            tbpGroupSupplier.Controls.Add(frmTMGROUPSUPPLIER)
            .Show()
 
        End With
    End Sub
 
 
    Private Sub TabControl()
 
        Select Case tbcSupplier.SelectedTab.Name.ToUpper
 
            Case tbpSupplier.Name.ToUpper
                If Not blnfrmMasterSupplier Then
                    LoadMasterSupplier()
                    blnfrmMasterSupplier = True
                End If
            Case tbpGroupSupplier.Name.ToUpper
                If Not blnfrmMasterGroupSupplier Then
                    LoadGroupSupplier()
                    blnfrmMasterGroupSupplier = True
                End If
        End Select
 
    End Sub
 
    Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TabControl()
    End Sub
 
    Private Sub tbcSupplier_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbcSupplier.Click
        TabControl()
    End Sub
 
    Private Sub tbcSupplier_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbcSupplier.SelectedIndexChanged
        Dim tp As TabPage = tbcSupplier.TabPages(tbcSupplier.SelectedIndex)
        Dim frm As Form = tp.Controls(tp.Name)
        Me.Text = frm.Tag
    End Sub
End Class

Open in new window

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
I assume that the Form is the first and only control on each TabPage:

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Dim index As Integer = TabControl1.SelectedIndex
        If index <> -1 Then
            Dim tp As TabPage = TabControl1.TabPages(index)
            If tp.Controls.Count > 0 Then
                If TypeOf tp.Controls(0) Is Form Then
                    Dim frm As Form = tp.Controls(0)
                    Me.Text = frm.Name & " " & frm.Tag
                End If
            End If
        End If
    End Sub

Author

Commented:
Hi Idle_Mind,

Yes, it works.

Thank you very much for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial