如何访问另一个类中的主表单标签 [英] How to access main form label in another class

查看:91
本文介绍了如何访问另一个类中的主表单标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Team,



我正在为图书出版业做自动化软件。



I我正在访问另一个类中的主表单标签。我想在ToolStripStatusLabel1中显示状态。



我有一个主窗体和一个带共享方法的外部类。当按钮单击主窗体时,将启动外部类。我想将状态/进度信息显示到主表单ToolStripStatusLabel1。



我使用多线程,表单访问对象,执行正常,但没有更新MainForm Label控件(ToolStripStatusLabel1。)。



我尝试过:



METHOD1:

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:

MainForm.ShowTextLabel("Importing XML...")







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







方法2:




METHOD2:

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







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







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







方法3:




METHOD3:

Dim Cls1 As New 
Cls1.ChangeText()







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



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



否使用这三种方法。



请帮我解决这个问题或提供指导我的任何有效链接。



提前致谢。



Venkadesh R.


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

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.

推荐答案

这在形式协作上被简化为一个非常流行的问题。为了涵盖大部分内容,我写了这篇文章:一次回答的许多问题 - Windows窗体或WPF Windows之间的协作 [ ^ ]。



-SA
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


这篇关于如何访问另一个类中的主表单标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆