Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team,

I am doing a automation software for book publishing industry.

I am struck on accessing main form label in another class. I want to display the status in the ToolStripStatusLabel1.

I have one main form and one external class with shared method. While button clicks on the main form, the external class is initiated. And i would like to display the status/progress information to the main form ToolStripStatusLabel1.

I used multithreading, form accessing objects, its executed fine, but no updation in the MainForm Label control (ToolStripStatusLabel1.).

What I have tried:

METHOD1:
VB.NET
MainForm.ShowTextLabel("Importing XML...")


VB.NET
Public Sub ShowTextLabel(ByVal Str As String)
        'Dim t1 As New Threading.Thread(AddressOf SetLabelText)
        't1.IsBackground = True
        't1.Start(Str)
        'Thread.Sleep(2000)

        For Each f As Form In My.Application.OpenForms
            If f.InvokeRequired Then
                If f.Tag = "main" Then
                    Dim fcast As New MainForm '<< whatever your form name
                    fcast = f
                    fcast.lblStatus.Text = Str
                End If
            End If
        Next
    End Sub



METHOD2:
VB.NET
Dim F1 = New MainForm
                F1.ToolStripStatusLabel1.Text = "Importing XML..."
                F1.lblStatus.Text = "Importing XML..."
                Application.DoEvents()
                F1.ShowAlert()


VB.NET
Public Sub ShowAlert()
        Dim objNewThread1 As New Thread(AddressOf StartShowAlertText)
        objNewThread1.IsBackground = True
        objNewThread1.Start()
    End Sub


C#
Public Delegate Sub SetText1(text As String)
   Public Sub StartShowAlertText()
       While True
           ToolStripStatusLabel1.Text = "Importing XML..."
           If lblStatus.InvokeRequired Then
               lblStatus.Invoke(New MethodInvoker(Sub() lblStatus.Text = "Importing..."))
           Else
               lblStatus.Text = "Importing..."
               lblStatus.Refresh()
           End If
           Application.DoEvents()
           Thread.Sleep(2000)
       End While
   End Sub



METHOD3:
VB.NET
Dim Cls1 As New 
Cls1.ChangeText()


VB.NET
Public Class VBControls
    Dim callingForm As CS6_SPinDesignXMLAutoflow
    'Public Sub New(ByVal caller As CS6_SPinDesignXMLAutoflow)
    '    callingForm = caller
    'End Sub
    Public Sub ChangeText()
        callingForm.lblStatus.Text = "1"
        callingForm.lblStatus.Text = "KJjhdfjdsjfhkjds"
        Application.DoEvents()
    End Sub
End Class

'----------------------------

No Use on the three methods.

Please help me on this issue or provide any valid links on guiding me.

Thanks in advance.

Venkadesh R.
Posted
Updated 25-Feb-16 20:40pm

1 solution

This is reduced to a pretty popular question on form collaboration. To cover most of them, I wrote this article: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900