winforms 如何更新继承窗体的单个示例而不更新其他打开的示例?

js5cn81o  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(131)

编辑3/23以包括建议的更改和新信息。
我有一个vb.netWindows窗体rptBase,它带有一个statusstrip和一个ToolStripStatusLabel tssl 1。
在frmMain上,我有ListBox 1,其中:

Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
    Dim frmRpt As New frmRptBase

    Select Case ListBox1.SelectedIndex
        Case 1
            frmRpt = New frmRpt1()
            frmRpt.Text = "Report 1"
            frmRpt.Show()
        Case 2
            frmRpt = New frmRpt2()
            frmRpt.Text = "Report 2"
            frmRpt.Show()
    End Select
    AddForm(frmRpt)
End Sub

我想报告报告的进度,但当我更新报告1的tssl1.text时,它也会更新报告2,反之亦然。我如何只更新其中一个?
我有一个字典和一个方法来保存对表单的引用

Public Sub AddForm(frm As Object)
    m_dctFrms.Add(frm, Str(frm.FormID))
End Sub

以及更新状态条的方法

Public Sub UpdateStatus(strMsg As String)
    tssl1.Text = strMsg
End Sub

我运行程序,打开report 1,然后打开report 2,然后运行report 1。
新信息:
报表在后台工作进程中运行。当我从DoWork子进程调用UpdateStatus时,它按预期工作。当我从ProgressChanged子进程调用UpdateStatus时,它不按预期工作。
Private Sub bw_DoWork(sender As Object,e As DoWorkEventArgs)处理bw.DoWork
UpdateStatus(“此操作有效,仅更新了一个表单。”)
末端子组件
私有子进程bw_ProgressChanged(sender As Object,e As ProgressChangedEventArgs)处理bw.ProgressChanged
UpdateStstus(“这不起作用。两个表单都已更新。”)

End Sub
xfyts7mz

xfyts7mz1#

也许是一个简单的数组(+简化你的代码):

Dim arrForm(5) As Form 'set max forms count
   
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If IsNothing(arrForm(ListBox1.SelectedIndex)) Then
            Dim frmRpt As New frmRptBase
            frmRpt.Text = "Report " & ListBox2.SelectedIndex
            arrForm(ListBox1.SelectedIndex) = frmRpt
            frmRpt.Show()
        End If
    End Sub
 
    Public Sub UpdateStatus(intKey As Integer, strMsg As String)
        Dim frm As frmRptBase = arrForm(intKey)
        frm.tssl1.Text = strMsg
    End Sub

相关问题