Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Label visibility toggling not working 1

Status
Not open for further replies.
Jul 10, 2008
32
US
This is probably going to sound like a really dumb question. In my program, the user selects values from 3 drop-down lists and hits the "Export" button, and the values are passed as parameters to a SQL Server 2005 stored procedure. The results of the query are stored in a dataset, which is then written to an XML file. I'm trying to get a label that says "Please wait..." to show up next to the button when the button is clicked, that will disappear when the operation is complete (that is, everything from opening the database connection to writing the XML file). But for some reason, whenever I hit the "Export" button, the label does not appear until the "File exported to specified directory" message pops up (which is after the operation is already complete). I even had another programmer look at this and even though it seems like such a simple matter, we can't figure out why this is happening. The label should show up when the event handler first starts to run, not right before it finishes.

Here is the code for the "Export" button's event handler. Label8 is my "Please wait..." label, and it's visibility property is set to "False" on the form:

Code:
Public Class mainForm

    Private Sub ExportButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportButton.Click
	Me.Cursor = System.Windows.Forms.Cursors.Default
        Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

	'Here's where I try to make it appear, but it won't actually appear on the GUI until the end of this subroutine is almost reached
        Label8.Visible = True

        ' Prepare the database connection.
        Dim connectionString As String = My.Settings.FileMakerConnectionString
        Dim theDatabase As New SqlConnection(connectionString)
        Try
            theDatabase.Open()
        Catch ex As SqlException
            MessageBox.Show("Error connecting to the database. File not exported.", "Error", MessageBoxButtons.OK)
            Label8.Visible = False
            Exit Sub
        End Try

        ' Check for a valid date/time range
        If EndDatePicker.Value < StartDatePicker.Value Then
            MessageBox.Show("Invalid date/time range.", "Error", MessageBoxButtons.OK)
            Label8.Visible = False
            Exit Sub
        End If

        ' Declare a SqlDataAdapter object.
        Dim objDataAdapter As New SqlDataAdapter()

        ' Assign a new SqlCommand to the SelectCommand property.
        objDataAdapter.SelectCommand = New SqlCommand()

        ' Set the SelectCommand Connection properties and the query to be executed.
        objDataAdapter.SelectCommand.Connection = theDatabase
        objDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
        objDataAdapter.SelectCommand.CommandText = "SP2"

        ' Declare parameters for the query
        objDataAdapter.SelectCommand.Parameters.AddWithValue("@clientname", NamePicker.SelectedValue)
        objDataAdapter.SelectCommand.Parameters.AddWithValue("@startdate", StartDatePicker.Value)
        objDataAdapter.SelectCommand.Parameters.AddWithValue("@enddate", EndDatePicker.Value)

        ' Declare a DataSet object.
        Dim objDataSet As DataSet = New DataSet("TEST")

        ' Fill the DataSet object with data, or
        ' Catch error if user loses network connection/authentication
        Try
            objDataAdapter.Fill(objDataSet, String.Format(NamePicker.SelectedValue + "_Record"))
        Catch ex As SqlException
            MessageBox.Show("Error connecting to the database. File not exported.", "Error", MessageBoxButtons.OK)
            Label8.Visible = False
            Exit Sub
        End Try

        ' If no matching records were found, notify the user.
        Dim objTable As New DataTable
        objTable = objDataSet.Tables(0)

        If objTable.Rows.Count > 0 Then
        Else
            MessageBox.Show("No records were returned.", "Error", MessageBoxButtons.OK)
            Label8.Visible = False
            Exit Sub
        End If

        ' Close the database connection.
        theDatabase.Close()

        ' Set the file directory to which the exported file will be written
        My.Computer.FileSystem.CurrentDirectory = textBox.Text

        ' Use this if you always want to save the file to the desktop
        ' Dim filepath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
        ' My.Computer.FileSystem.CurrentDirectory = filepath

        ' Convert selected DateTimePicker dates to strings
        Dim startDate2 As String = StartDatePicker.Value.ToString("MM-dd-yyyy_HHmmss")
        Dim endDate2 As String = EndDatePicker.Value.ToString("MM-dd-yyyy_HHmmss")
        Dim clientName As String = NamePicker.SelectedValue.ToString

        Dim filename As String = clientName + "_" + startDate2 + "_TO_" + endDate2 + ".xml"

        ' Write query results to XML file.
        Try
            objDataSet.WriteXml(filename)
        Catch se As System.UnauthorizedAccessException
            MessageBox.Show("You do not have the appropriate permission to take this action.", "Error", MessageBoxButtons.OK)
            Label8.Visible = False
            Exit Sub
        Catch ex As Exception
            MessageBox.Show("There was an error. File not created.", "Error", MessageBoxButtons.OK)
            Label8.Visible = False
            Exit Sub
        End Try

	'This is where I finally see the label appear, at the same time this messagebox appears
        If filename.CompareTo(My.Computer.FileSystem.CurrentDirectory & filename) = 1 Then
            MessageBox.Show("File exported to specified directory.", "Success", MessageBoxButtons.OK)
        Else
            MessageBox.Show("There was an error. File not created.", "Error", MessageBoxButtons.OK)
        End If

	'It disappears here, like it should
        Label8.Visible = False

        ' Clean up

        objDataAdapter = Nothing
        objDataSet = Nothing
    End Sub

Anyone know why this is happening and what I can do to solve it?
 
Try putting an Application.DoEvents() after the Label8.Visible = True line.

But to engineer this in a more robust way, I would put the processing in a background worker if it takes any length of time to process.
 
Thank you! I'm a VB newbie so I had never heard of this, but I did some research and it looks like it will work fine for my app. So I guess the problem was that a higher priority event was being processed before a lower-priority one? At first I was concerned because I read about the risks involved with using DoEvents() (re-entering methods), but I did some testing and this doesn't seem to be an issue for my application.

This app won't be processing any huge amounts of data. With the various amounts of data it processes, the wait is usually 2 or 3 seconds and at most 10, so I think I'll stick with what I have instead of a background worker.
 

I would also try:
Code:
Label8.Visible = True
[blue]Label8.Refresh()[/blue]


Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top