Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15539

VS 2010 Using multiple Backgroundworker controls on one WinForm

$
0
0
Hello,
My developing environment is :
Win 10 64
Visual Basic 2010
WinForm application Front-End, MS-Access 2016 Database

My Issue :
I have a Form that contains 4 ComboBox Controls, each ComboBox is supposed to be filled with Data From a different table within the Database. I'm using BackgroundWorker Control to fill one ComboBox control on Form_Load() event.

I'm wondering, how to do the same with the rest 3 ComboBox controls? and if there are a better way to achieve such task !!

Here is my code for filling one of the combobox controls:

Code:

Imports System.Data.OleDb
Public Class ThisForm

    Private ComboItems As New Dictionary(Of String, String)()

Private Sub PurchaseFrm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If BackgroundWorker1.IsBusy Then
            BackgroundWorker1.CancelAsync()
        End If
        BackgroundWorker1.RunWorkerAsync()
    End Sub

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim SqlStr As String =
            ("SELECT * FROM Types;")
        Using CN As New OleDbConnection With {.ConnectionString = My.Settings.MainConnection},
                              M_Cmd As New OleDbCommand(SqlStr, CN)
            CN.Open()
            Using ComboReader As OleDbDataReader = M_Cmd.ExecuteReader
                While ComboReader.Read
                    If ComboReader.HasRows Then
                        'Almost 29.8k records of string.
                        ComboItems.Add(ComboReader!TypeID.ToString, ComboReader!TypeNm)
                    Else
                        TypCbo.DataSource = Nothing
                    End If
                End While
            End Using
        End Using
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        With TypCbo
            .DataSource = Nothing
            .Items.Clear()
            .BeginUpdate()
            .DataSource = New BindingSource(ComboItems, Nothing)
            .DisplayMember = "Value"
            .ValueMember = "key"
            .SelectedIndex = -1
            .EndUpdate()
        End With
    End Sub

End Class


Viewing all articles
Browse latest Browse all 15539

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>