开发者

Active form in a Windows application?

I am developing a Windows Forms application. I have four forms which is inherited from Baseform in another project. In all four forms I am using a label to show some transaction count based on network status. I have implemented a thread which gets the active form of application and setting up the text. The code works fine if application screen is active. If I minimize and open any other application, I am getting an null error exception.

How do I get the active form of an application?

Private Sub StartThread() 
    pollThread =New Thread(AddressOf PollfileStatus) 
    pollThread.IsBackground =True
    running =True
    pollThread.Start()
End Sub

Private Sub PollfileStatus() 
    While (running)
        Try
            For Each e As Control In Me.ActiveForm.Controls 
                If (e.Name = "pbStatus") Then
                    e.Invoke(New SetTextCallback(AddressOf Settext), 
                        New Object() {e, 10}) 
                End If
            Next
        Catch ex As Exception 
            Throw New ApplicationException(ex.Message) 
        End Try
        Thread.Sleep(6000)
    End Whi开发者_如何学Pythonle
End Sub 


Understandably Me.ActiveForm is empty, since minimizing your form makes it inactive. You have two options:

  • Test if Me.ActiveForm is empty, and if so, do not update the label. This will effectively 'pause' label updates, until the user restores the window again.

  • Create a property on your class that contains your thread, to pass a reference of the form you have to update. This way the label on the given form will update even if it is not the active form. This might be a better option, it will update even if the form is inactive in the background, for example.


You should look into the Application.OpenForms shared property. It contains a collection of all forms in your application.

EDIT: Since you're working with .net 1.1 and don't have access to Application.OpenForms, here are some suggestions:

  • Implement your own shared class (module) that contains an ArrayList of forms. You could then create a base class inheriting from Form, handle the Load event to add the current form to the list of forms and the Closed event to remove it. Once you have that class, have all your real forms derive from it. BTW, it's (almost) how it is done in .Net 2.0.
  • Turn the problem around and have the working thread raise events when the values change that you can handle in each form to update it.


An old question, but I'd thought I'd offer a suggestion as well: you could implement a Public Shared CurrentForm As Form that you set in the Form.Activated event of a base form class all your forms inherit from. From the thread you could then use CurrentForm to access the desired form.


This is very easy like this in vb.net

Dim MYFormName As String = Me.Name.ToString


You have many solutions for this situation:

  • first of all you should consider checking if the current form is null before proceeding in any action.
  • second: if that condition passes, a good approach to store the last active form's reference.


This might work but I cannot test it right now.

For Each frm As Form In Application.OpenForms
    If frm.ContainsFocus Then
        Return frm
    End If
Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜