Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is because in winforms a ListBox object collection is different from a ComboBox object collection. The simplest way I can think of to tidy this is to make a helper class like</p> <pre><code>Public Class ListHelper Public Shared Sub Clear(ByRef lst As ListControl) If TypeOf lst Is ListBox Then CType(lst, ListBox).Items.Clear() Else CType(lst, ComboBox).Items.Clear() End If End Sub Public Shared Sub Add(ByRef lst As ListControl, ByVal itm As Object) If TypeOf lst Is ListBox Then CType(lst, ListBox).Items.Add(itm) Else CType(lst, ComboBox).Items.Add(itm) End If End Sub End Class </code></pre> <p>Then in your code you can just do :</p> <pre><code>Sub loadList(ByVal db As SqlDatabase, ByVal strCommandText As String, _ ByVal lstHost As ListControl, Optional ByVal bClearList As Boolean = True) If bClearList Then ListHelper.Clear(lstHost) End If Dim dt As DataTable = _ db.ExecuteDataSet(db.GetSqlStringCommand(strCommandText)).Tables(0) For i = 0 To dt.Rows.Count - 1 ListHelper.Add(lstHost, dt.Rows(i)(0).ToString) Next End Sub </code></pre> <p><strong>EDIT :</strong></p> <p>Another (probably better) way to do this is using extension methods (add a new module and ) :</p> <pre><code>Imports System.Runtime.CompilerServices Module ListExtensions &lt;Extension()&gt; _ Public Sub AddToItems(ByRef lc As ListControl, ByVal itm As Object) If TypeOf lc Is ListBox Then CType(lc, ListBox).Items.Add(itm) ElseIf TypeOf lc is ComboBox then CType(lc, ComboBox).Items.Add(itm) Else 'handle abuse End If End Sub &lt;Extension()&gt; _ Public Sub ClearItems(ByRef lc As ListControl) If TypeOf lc Is ListBox Then CType(lc, ListBox).Items.Clear() ElseIf TypeOf lc is ComboBox Then CType(lc, ComboBox).Items.Clear() Else 'handle abuse End If End Sub End Module </code></pre> <p>Which ends up being even a bit neater in your code :</p> <pre><code>Sub loadList(ByVal db As SqlDatabase, ByVal strCommandText As String, _ ByVal lstHost As ListControl, Optional ByVal bClearList As Boolean = True) If bClearList Then lstHost.ClearItems() End If Dim dt As DataTable = _ db.ExecuteDataSet(db.GetSqlStringCommand(strCommandText)).Tables(0) For i = 0 To dt.Rows.Count - 1 lstHost.AddToItems(dt.Rows(i)(0).ToString) Next End Sub </code></pre> <p>Here I've called these <code>ClearItems</code> and <code>AddToItems</code> to avoid ambiguity with instance methods. ListControl doesn't have <code>.Clear()</code> or <code>.Add()</code> itself but for the sake of being explicit it's probably best to have a unique nomenclature for extensions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload