user1030181 user1030181 - 1 year ago 80 Question

PictureBox is not showing up on button click event

I am using a PictureBox inside a button click event. When the button is clicked I am enabling the PictureBox and I am running a long database call and at the end of the process, I am trying to disable the PictureBox. Inside the PictureBox I have a loading GIF.

But I don't know what's happening. My PictureBox does not show up..

Please suggest how can I fix this. I tried Thread.Sleep(1000), but it didn't work.

Private Sub btnRetrieve_Click(sender As Object, e As EventArgs) Handles btnRetrieve.Click
Me.PictureBox1.Visible = True
lblSuccess.Text = Nothing
UltraNumberOfConveyance.Value = Nothing
GetData() --Long Running Query
Me.PictureBox1.Visible = False
End Sub

My GetData Function:

Private Function GetData()
dsCheckPointTimes = GetCheckPointTimesByTerminalID()
dtDataTable = dsCheckPointTimes.Tables(0)
chkdtDataTable = dsCheckPointTimes.Tables(1)

If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("ConveyanceName")) Then
lblConveyanceNameText.Text = chkdtDataTable.Rows.Item(0).Item("ConveyanceName").ToString()
End If

If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("NumberOfConveyance")) Then
UltraNumberOfConveyance.Value = chkdtDataTable.Rows.Item(0).Item("NumberOfConveyance")
End If

If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("Dock")) Then
UltratxtChangeLabel1.Value = chkdtDataTable.Rows.Item(0).Item("Dock")
End If

If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("Lines")) Then
UltratxtChangeLabel2.Value = chkdtDataTable.Rows.Item(0).Item("Lines")
End If

If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("CHECKPOINTTYPE")) Then
End If

If dtDataTable.Rows.Count > 0 Then
lblSuccess.Text = "No Records Found! Please check the ordernumber"
lblSuccess.ForeColor = Color.Red
End If
Return Nothing
End Function

Answer Source

Use a BackgroundWorker to do this:

Private WithEvents bgw As New BackgroundWorker

Private Sub btnRetrieve_Click(sender As Object, e As EventArgs) Handles btnRetrieve.Click
  PictureBox1.Visible = True
End Sub

Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork
  GetData() --Long Running Query
End Sub

Private Sub bgw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
  PictureBox1.Visible = False
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download