How to handle form load.

PatelNehal4u

Active member
Joined
Apr 6, 2009
Messages
25
Programming Experience
1-3
I had created a form(MDI Child) which have 3 to 4 dataset on it. They all are loaded from the database at the form load event and therefore takes too much time to load and show some part of form during that time. When it load completely it shows the whole form. Is there any way that user will see the complete form instead of some broken form.:mad:
 
Try putting the dataset filling into a sub routine, after you make an instance of your form, call that sub, then show the form. I wonder if SyncLock would be a good idea here???
 
Hi

I have had this issue myself in the past and i accomplished a great result by adding a timer to each mdi and setting it to 5 seconds, in your form load event after your 'initialize' event set the timer to true. in your timer have a sub routine to call the load data method, be sure to set your timer to false as soon as it enters the timer event.

Zeus
 
Here's how I handle loading a list of SQL Server instances, which takes several seconds to retrieve, into a ComboBox:
VB.NET:
private void DatabaseForm_Load(object sender, EventArgs e)
{
    // Add a dummy item to the server list while it is being populated.
    this.serverComboBox.Items.Add("Please wait...");

    // Load the server list.
    this.serverListWorker.RunWorkerAsync();

    this.LoadSettings();
}

private void serverListWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Get the list of SQL Server instances in the background.
    e.Result = new ServersPresenter().Load();
}

private void serverListWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Remember the current server name.
    string text = this.serverComboBox.Text;

    // Display the list of servers.
    this.serverComboBox.DataSource = e.Result;

    // Restore the current server name.
    this.serverComboBox.Text = text;
}
The code is C# but you should be able to get the idea. In the Load event handler you start a background task, in that background task you get your data, then you update the UI with the data when the task is complete.
 
Here is the VB version

Here is the VB vesrion

VB.NET:
Private Sub DatabaseForm_Load(ByVal sender As Object, ByVal e As EventArgs)
	' Add a dummy item to the server list while it is being populated.
	Me.serverComboBox.Items.Add("Please wait...")

	' Load the server list.
	Me.serverListWorker.RunWorkerAsync()

	Me.LoadSettings()
End Sub

Private Sub serverListWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
	' Get the list of SQL Server instances in the background.
	e.Result = New ServersPresenter().Load()
End Sub

Private Sub serverListWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
	' Remember the current server name.
	Dim text As String = Me.serverComboBox.Text

	' Display the list of servers.
	Me.serverComboBox.DataSource = e.Result

	' Restore the current server name.
	Me.serverComboBox.Text = text
End Sub
 
Back
Top