vb.netwinformscomboboxbindingbindinglist

why does binding combobox during form load make it slow or delay in VB.NET


I'm trying binding combobox with 4 combobox in form load , I use 1 form Form2 to add and view.

if I comment out the code BindcomboboxJob2() BindcomboboxJob2() BindcomboboxJob3() BindcomboboxJob4() in

Public Sub New() then it runs normally without delay or slowness

is there something wrong with my code please guide me?

Thanks

Code in form1

Public Class Form1
    Dim pservice As New PeopleService()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()
    End Sub
 Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If DataGridView1.Columns(e.ColumnIndex) Is DataGridView1.Columns("edit") Then
            If DataGridView1.SelectedRows.Count > 0 Then
                Dim transno = DirectCast(DataGridView1.SelectedRows(0).DataBoundItem, People)
                Using frm = New Form2(transno)
                    If frm.ShowDialog() = DialogResult.OK Then
                    End If
                End Using
            End If

        End If
    End Sub
 Private Sub BtnADD_Click(sender As Object, e As EventArgs) Handles BtnADD.Click
        Using frm = New Form2()
            If frm.ShowDialog() = DialogResult.OK Then
                LoadData()
            End If
        End Using
    End Sub
End Class

Code in form2

Public Class Form2
    Private bindingSource As BindingSource = Nothing
    Dim pservice As New PeopleService()
 Public Sub New()
        InitializeComponent()
  BindcomboboxJob1()
      BindcomboboxJob1()
        ComboBox1.Text = "Job1"
        BindcomboboxJob2()
        ComboBox2.Text = "Job2"
        BindcomboboxJob3()
        ComboBox3.Text = "Job3"
        BindcomboboxJob4()
        ComboBox4.Text = "Job4"
    End Sub
 Public Sub New(Transno As People)
        Me.New
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of People)(CType(pservice.GetPeopleview(Transno.Transno), IList(Of People)))}
        DataGridView1.DataSource = bindingSource
        TextBox1.Text = Transno.Transno
        TextBox2.Text = Transno.Username
        ComboBox1.Text = Transno.Job1
        ComboBox2.Text = Transno.Job2
        ComboBox3.Text = Transno.Job3
        ComboBox4.Text = Transno.Job4
    End Sub
  Private Sub BindcomboboxJob1()
        ComboBox1.DisplayMember = "Job1"
        ComboBox1.ValueMember = "Job1"
        ComboBox1.DropDownHeight = 80
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of Job1)(CType(pservice.GetByJob1, IList(Of Job1)))}
        ComboBox1.DataSource = bindingSource
    End Sub
    Private Sub BindcomboboxJob2()
        ComboBox2.DisplayMember = "Job2"
        ComboBox2.ValueMember = "Job2"
        ComboBox2.DropDownHeight = 80
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of Job2)(CType(pservice.GetByJob2, IList(Of Job2)))}
        ComboBox2.DataSource = bindingSource
    End Sub
    Private Sub BindcomboboxJob3()
        ComboBox3.DisplayMember = "Job3"
        ComboBox3.ValueMember = "Job3"
        ComboBox3.DropDownHeight = 80
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of Job3)(CType(pservice.GetByJob3, IList(Of Job3)))}
        ComboBox3.DataSource = bindingSource
    End Sub
    Private Sub BindcomboboxJob4()
        ComboBox4.DisplayMember = "Job4"
        ComboBox4.ValueMember = "Job4"
        ComboBox4.DropDownHeight = 80
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of Job4)(CType(pservice.GetByJob4, IList(Of Job4)))}
        ComboBox4.DataSource = bindingSource
    End Sub
End Class
Public Class PeopleService
 Public Function GetOledbConnectionString() As String
        Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\People.accdb;Persist Security Info=False;"
    End Function
    Private ReadOnly _conn As OleDbConnection
    Private _connectionString As String = GetOledbConnectionString()
  Public Function GetByJob1() As IEnumerable(Of Job1)
        Dim sql = $"SELECT Job1 FROM Job1"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of Job1)(sql).ToList()
        End Using
    End Function
    Public Function GetByJob2() As IEnumerable(Of Job2)
        Dim sql = $"SELECT Job2 FROM Job2"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of Job2)(sql).ToList()
        End Using
    End Function
    Public Function GetByJob3() As IEnumerable(Of Job3)
        Dim sql = $"SELECT Job3 FROM Job3"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of Job3)(sql).ToList()
        End Using
    End Function
    Public Function GetByJob4() As IEnumerable(Of Job4)
        Dim sql = $"SELECT Job4 FROM Job4"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of Job4)(sql).ToList()
        End Using
    End Function
End Class

according to @jimi's recommendation

example

Public Async Function GetByJob1() As Task(Of IEnumerable(Of Job1))
        Dim sql = $"SELECT Job1 FROM Job1"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            'Return _conn.a(Of Job1)(sql).ToList()
            Return Await _conn.QueryAsync(Of Job1)(sql)
        End Using
    End Function


Private Async Function BindcomboboxJob1() As Task(Of Task())
        ComboBox1.DisplayMember = "Job1"
        ComboBox1.ValueMember = "Job1"
        ComboBox1.DropDownHeight = 80
        Dim job1Data = Await pservice.GetByJob1()
        bindingSource = New BindingSource(job1Data.ToList(), Nothing)
    End Function

'If the method below is correct, When I call again , I have an error in code `Await BindcomboboxJob1()`

  Public Sub New()
 InitializeComponent()
Await BindcomboboxJob1()
        ComboBox1.Text = "Job1"
    End Sub

according to @dr.null recommendation

example

    Private bindingSource As BindingSource = Nothing
 Private Sub BindcomboboxJob1()
    If ComboBox1.DataSource IsNot Nothing Then Return
        Cursor.Current = Cursors.WaitCursor
        ComboBox1.DisplayMember = "Job1"
        ComboBox1.ValueMember = "Job1"
 bindingSource = New BindingSource With {.DataSource = pservice.GetByJob1}
        ComboBox1.DataSource = bindingSource
        Cursor.Current = Cursors.Default
    End Sub
  Public Sub New()
   InitializeComponent()
        ComboBox1.DropDownHeight = 80
ComboBox1.Text = "Job1"
   End Sub
 Public Sub New(Transno As People)
        Me.New
  ComboBox1.Text = Transno.Job1
    End Sub
Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown

        BindcomboboxJob1()

    End Sub

    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown

        BindcomboboxJob1()
    End Sub


Solution

  • This is due to help and recommendations from @dr.null . I thank you very much

    Private bindingSource As BindingSource = Nothing
        
    Private Sub BindcomboboxJob1()
    If ComboBox1.DataSource IsNot Nothing Then Return
            Cursor.Current = Cursors.WaitCursor
            ComboBox1.DisplayMember = "Job1"
            ComboBox1.ValueMember = "Job1"
      bindingSource = New BindingSource With {.DataSource = pservice.GetByJob1}
            ComboBox1.DataSource = bindingSource
            Cursor.Current = Cursors.Default
        End Sub
     Private Sub BindcomboboxJob2()
    If ComboBox2.DataSource IsNot Nothing Then Return
            Cursor.Current = Cursors.WaitCursor
            ComboBox2.DisplayMember = "Job2"
            ComboBox2.ValueMember = "Job2"
     bindingSource = New BindingSource With {.DataSource = pservice.GetByJob2}
            ComboBox2.DataSource = bindingSource
            Cursor.Current = Cursors.Default
        End Sub
        Private Sub BindcomboboxJob3()
    If ComboBox3.DataSource IsNot Nothing Then Return
            Cursor.Current = Cursors.WaitCursor
            ComboBox3.DisplayMember = "Job3"
            ComboBox3.ValueMember = "Job3"
      bindingSource = New BindingSource With {.DataSource = pservice.GetByJob3}
            ComboBox3.DataSource = bindingSource
            Cursor.Current = Cursors.Default
        End Sub
    Private Sub BindcomboboxJob4()
    If ComboBox4.DataSource IsNot Nothing Then Return
            Cursor.Current = Cursors.WaitCursor
            ComboBox4.DisplayMember = "Job4"
            ComboBox4.ValueMember = "Job4"
      bindingSource = New BindingSource With {.DataSource = pservice.GetByJob4}
            ComboBox4.DataSource = bindingSource
            Cursor.Current = Cursors.Default
        End Sub
    Public Sub New()
    InitializeComponent()
            ComboBox1.DropDownHeight = 80
            ComboBox1.Text = "Job1"
            ComboBox2.DropDownHeight = 80
            ComboBox2.Text = "Job2"
            ComboBox3.DropDownHeight = 80
            ComboBox3.Text = "Job3"
            ComboBox4.DropDownHeight = 80
            ComboBox4.Text = "Job4"
        End Sub
    Public Sub New(Transno As People)
            Me.New
     ComboBox1.Text = Transno.Job1
            ComboBox2.Text = Transno.Job2
            ComboBox3.Text = Transno.Job3
            ComboBox4.Text = Transno.Job4
        End Sub
    Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown
    
            BindcomboboxJob1()
    
        End Sub
    
        Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
    
            BindcomboboxJob1()
        End Sub
        Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
    
            BindcomboboxJob2()
    
        End Sub
    
        Private Sub ComboBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox2.KeyDown
    
            BindcomboboxJob2()
        End Sub
    
        Private Sub ComboBox3_DropDown(sender As Object, e As EventArgs) Handles ComboBox3.DropDown
    
            BindcomboboxJob3()
    
        End Sub
    
        Private Sub ComboBox3_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox3.KeyDown
    
            BindcomboboxJob3()
        End Sub
        Private Sub ComboBox4_DropDown(sender As Object, e As EventArgs) Handles ComboBox4.DropDown
    
            BindcomboboxJob4()
    
        End Sub
    
        Private Sub ComboBox4_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox4.KeyDown
    
            BindcomboboxJob4()
        End Sub