Question Changing ToolStripItem Text dynamically

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

I have a ContextMenuStrip attached to a TextBox

VB.NET:
 Dim TelTBMenu As New ContextMenuStrip
        With TelTBMenu
            .Name = "TelTBMenu"
        End With
        Dim TelTBItem As ToolStripItem
        Dim TelTBItemText As String = ""
        If My.Settings.EnableIntTelNumbers = True Then
            TelTBItemText = "Disable International Formatting"
        Else
            TelTBItemText = "Enable International Formatting"
        End If
        TelTBItem = TelTBMenu.Items.Add(TelTBItemText)
        AddHandler TelTBItem.Click, AddressOf SuppliersTelephoneNumberReformat


        With TelTB
            .Name = "TelTB"
            .Size = New Point(100, 20)
            .Location = New Point(130, 230)
            .ContextMenuStrip = TelTBMenu
        End With
        vBDPanel.Controls.Add(TelTB)

..and the click event

VB.NET:
Private Sub SuppliersTelephoneNumberReformat(ByVal sender As Object, ByVal e As EventArgs)
        Dim TelTB As TextBox = RFC(SuppliersMainForm, "TelTB")
        Dim TelTBmenu As ContextMenuStrip = TelTB.ContextMenuStrip
        Dim TelTBItem As ToolStripItem '= TelTBmenu.Items("TelTBItem")
        TelTBmenu.Items.Clear()
        Dim TelTBItemText As String = ""
        If My.Settings.EnableIntTelNumbers = False Then
            TelTBItemText = "Disable International Formatting"
            My.Settings.EnableIntTelNumbers = True
        Else
            TelTBItemText = "Enable International Formatting"
            My.Settings.EnableIntTelNumbers = False
        End If
        TelTBItem = TelTBmenu.Items.Add(TelTBItemText)
        My.Settings.Save()
    End Sub

Whilst the form is loaded this runs fine twice, but then although the Setting boolean is reset the ToolStripItem text remains unchanged unless the form is re-loaded.

Any ideas why?

Thanks
 
Where is this code? Is it in the form Load event? That would explain it. Then you would need to move it out to it's own sub procedure and make the calls to "reload" it.

Hi there

I have a ContextMenuStrip attached to a TextBox

VB.NET:
 Dim TelTBMenu As New ContextMenuStrip
        With TelTBMenu
            .Name = "TelTBMenu"
        End With
        Dim TelTBItem As ToolStripItem
        Dim TelTBItemText As String = ""
        If My.Settings.EnableIntTelNumbers = True Then
            TelTBItemText = "Disable International Formatting"
        Else
            TelTBItemText = "Enable International Formatting"
        End If
        TelTBItem = TelTBMenu.Items.Add(TelTBItemText)
        AddHandler TelTBItem.Click, AddressOf SuppliersTelephoneNumberReformat


        With TelTB
            .Name = "TelTB"
            .Size = New Point(100, 20)
            .Location = New Point(130, 230)
            .ContextMenuStrip = TelTBMenu
        End With
        vBDPanel.Controls.Add(TelTB)
 
Where is this code?

First 50 lines
VB.NET:
Public Sub Suppliers_Special_New_Supplier_Form(ByVal NewRecord As Boolean, Optional ByVal reload As Boolean = False)
        If reload = False Then
            SuppliersMainForm = New Form
        End If
        With SuppliersMainForm
            .Size = New Point(700, 650)
            If NewRecord = True Then
                .Text = "Add new supplier record"
            Else
                .Text = "Supplier record for " & SupplierNameText
            End If
        End With
        Dim vBDPanel As New Panel
        With vBDPanel
            .Dock = DockStyle.Fill
            .AutoScroll = True
        End With

        SuppliersMainForm.Controls.Add(vBDPanel)



        Dim AMW_MainTab As New TabControl
        With AMW_MainTab
            .Name = "AMW_MainTab"
            .SelectedIndex = 0
            .ShowToolTips = True
            .Dock = DockStyle.Fill
        End With      
        SuppliersMainForm.Controls.Add(AMW_MainTab)

Lines 165 - 180 - the code in question
VB.NET:
Dim TelTBMenu As New ContextMenuStrip
        With TelTBMenu
            .Name = "TelTBMenu"
        End With
        Dim TelTBItem As ToolStripItem
        Dim TelTBItemText As String = ""
        If My.Settings.EnableIntTelNumbers = True Then
            TelTBItemText = "Disable International Formatting"
        Else
            TelTBItemText = "Enable International Formatting"
        End If
        TelTBItem = TelTBMenu.Items.Add(TelTBItemText)
        AddHandler TelTBItem.Click, AddressOf SuppliersTelephoneNumberReformat


        With TelTB
            .Name = "TelTB"
            .Size = New Point(100, 20)
            .Location = New Point(130, 230)
            .ContextMenuStrip = TelTBMenu
        End With
        vBDPanel.Controls.Add(TelTB)


Lines 603 - 620
VB.NET:
SSMStatusStrip = New StatusStrip
        SSMStatusLabel = New ToolStripStatusLabel
        SSMProgressBar = New ToolStripProgressBar
        SSMStatusStrip.Items.Add(SSMProgressBar)
        SSMStatusStrip.Items.Add(SSMStatusLabel)

        SuppliersMainForm.Controls.Add(SSMStatusStrip)




        If reload = False Then
            SuppliersMainForm.StartPosition = FormStartPosition.CenterParent
            SuppliersMainForm.ShowDialog()
        End If


    End Sub
 
Back
Top