2 Replies - 487 Views - Last Post: 01 November 2016 - 07:05 PM Rate Topic: -----

#1 Damein  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 29-March 14

Loading a Form from another Form results in TextBox now being visible

Posted 29 October 2016 - 07:45 PM

Dim x As Form3 = New Form3
            x.Show()


Public Class Form3
    Private WithEvents m_tmrGame As New System.Timers.Timer(1000)
    Private m_intShouldBeAnEnum As Integer = 1

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        m_tmrGame.Start()
    End Sub

    Private Sub m_tmrGame_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_tmrGame.Elapsed

        Dim strCurrentObject As String = String.Empty

        Select Case m_intShouldBeAnEnum
            Case 1
                strCurrentObject = "Data is loading..."
                m_intShouldBeAnEnum = 2
            Case 2
                strCurrentObject = "Data is loading...."
                m_intShouldBeAnEnum = 3
            Case 3
                strCurrentObject = "Data is loading....."
                m_intShouldBeAnEnum = 4
            Case 4
                strCurrentObject = "Data is loading......"
                m_intShouldBeAnEnum = 1
        End Select

        If Application.OpenForms().OfType(Of Form3).Any Then
            LoadingTextBox.Invoke(Sub(lblTarget As Label) lblTarget.Text = strCurrentObject, New Object() {LoadingTextBox})
        Else
            m_tmrGame.Stop()
        End If
    End Sub
End Class



The above form loads but when it does the TextBox LoaderTextBox is just a white block, not the text. When the data is done loading and right before the other form loads it shows the boxes data but only for a split second.

Any thoughts on this?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Loading a Form from another Form results in TextBox now being visible

#2 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1458
  • View blog
  • Posts: 3,700
  • Joined: 01-February 13

Re: Loading a Form from another Form results in TextBox now being visible

Posted 30 October 2016 - 06:52 AM

I don`t see why you are using a Timers.Timer instead of a Windows.Forms.Timer. A Windows.Forms.Timer`s Tick event would be raised in the same thread as Form3 allowing you to access the TextBox directly from the Tick event without worrying about cross thread calls.

If you really think you need to use a Timers.Timer, you can simply set the SynchronizingObject property of the timer to the Form. That will marshal the Elapsed event to be raised in the same thread as the Form and then you can access the TextBox directly from the Elapsed event. Like this...
Public Class Form3
    Private WithEvents m_tmrGame As New System.Timers.Timer(1000)
    Private m_intShouldBeAnEnum As Integer = 1

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        m_tmrGame.SynchronizingObject = Me
        m_tmrGame.Start()
    End Sub

    Private Sub m_tmrGame_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_tmrGame.Elapsed
        Dim strCurrentObject As String = String.Empty

        Select Case m_intShouldBeAnEnum
            Case 1
                strCurrentObject = "Data is loading..."
                m_intShouldBeAnEnum = 2
            Case 2
                strCurrentObject = "Data is loading...."
                m_intShouldBeAnEnum = 3
            Case 3
                strCurrentObject = "Data is loading....."
                m_intShouldBeAnEnum = 4
            Case 4
                strCurrentObject = "Data is loading......"
                m_intShouldBeAnEnum = 1
        End Select

        LoadingTextBox.Text = strCurrentObject
    End Sub
End Class


As a side note, your TextBox.Invoke code really does not make sense and i am not sure how it even runs without throwing an exception. The Sub is set up to take a Label type control as the parameter but, you are passing a TextBox type to it. That should throw an exception. It may have something to do with the problem.
LoadingTextBox.Invoke(Sub(lblTarget As Label) lblTarget.Text = strCurrentObject, New Object() {LoadingTextBox})


This line could also cause problems if there is more than 1 instance of Form3 opened at the same time. If you closed one instance, the Elapsed event could still be raised if Any instance of Form3 are still opened.
If Application.OpenForms().OfType(Of Form3).Any Then


Without seeing and understanding the full context of your code that you are actually using, another guess of why the TextBox is not updated would be that You are running a long updating process on the same thread which would cause the form and its controls to not receive any messages until it is stopped. That could be fixed by running the updating code in a separate thread which would allow the Form and its controls to still receive messages and update themselves while the updating thread is running in the background.
Was This Post Helpful? 0
  • +
  • -

#3 Damein  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 29-March 14

Re: Loading a Form from another Form results in TextBox now being visible

Posted 01 November 2016 - 07:05 PM

View PostIronRazer, on 30 October 2016 - 06:52 AM, said:

I don`t see why you are using a Timers.Timer instead of a Windows.Forms.Timer. A Windows.Forms.Timer`s Tick event would be raised in the same thread as Form3 allowing you to access the TextBox directly from the Tick event without worrying about cross thread calls.


Thanks for all the info IronRazer, I'll look into the section pertaining to timers.

Also, I think you're right about the thread idea. I'll look into that as well.

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1