title: Better Way To Build BindingSource Filter From a Group of Controls [WinForms]
body: <p>Right now I am building a filter based on user input (textboxes and comboboxes) by a bunch of if statements.  There must be a better way.  Here is my current code:</p>
  
  <pre><code>    Private Function BuildProductsFilter() As String
  
          Dim RawFilterResults As String = ""
  
          If Not CompanyNameComboBox.SelectedValue Is Nothing AndAlso Not CompanyNameComboBox.SelectedValue.ToString = "[ALL]" Then
              RawFilterResults = "companyname = '" &amp; CompanyNameComboBox.SelectedValue.ToString &amp; "'"
          End If
  
          If QtyTextbox.Text &gt; "" AndAlso IsNumeric(QtyTextbox.Text) Then
              RawFilterResults &amp;= " and stock = " &amp; QtyTextbox.Text
          End If
  
          If KeywordTextbox.Text &gt; "" Then
              RawFilterResults &amp;= " and (description like '%" &amp; KeywordTextbox.Text &amp; "%'"
              RawFilterResults &amp;= "or descriptionlong like '%" &amp; KeywordTextbox.Text &amp; "%'"
              RawFilterResults &amp;= "or details like '%" &amp; KeywordTextbox.Text &amp; "%')"
          End If
  
          If SKUTextbox.Text &gt; "" Then
  
              If SKUTextbox.Text.StartsWith("*") Then
                  RawFilterResults &amp;= " and sku like '%" &amp; SKUTextbox.Text &amp; "'"
              ElseIf SKUTextbox.Text.EndsWith("*") Then
                  RawFilterResults &amp;= " and sku like '" &amp; SKUTextbox.Text &amp; "%'"
              Else
                  RawFilterResults &amp;= " and sku = '" &amp; SKUTextbox.Text &amp; "'"
              End If
  
          End If
  
          If Not AllowPurchaseCombobox.SelectedItem Is Nothing AndAlso Not AllowPurchaseCombobox.SelectedItem.ToString = "[ALL]" Then
              RawFilterResults &amp;= " and allowpurchase = '" &amp; AllowPurchaseCombobox.SelectedValue.ToString &amp; "'"
          End If
  
          If Not ShowPriceCombobox.SelectedItem Is Nothing AndAlso Not ShowPriceCombobox.SelectedItem.ToString = "[ALL]" Then
              RawFilterResults &amp;= " and showprice = '" &amp; ShowPriceCombobox.SelectedValue.ToString &amp; "'"
          End If
  
          If Not VirtualLoupeCombobox.SelectedItem Is Nothing AndAlso Not VirtualLoupeCombobox.SelectedItem.ToString = "[ALL]" Then
              RawFilterResults &amp;= " and VirtualLoupe = '" &amp; VirtualLoupeCombobox.SelectedValue.ToString &amp; "'"
          End If
  
          If ImageTextbox.Text &gt; "" Then
              Dim ImageDir As String = Path.GetDirectoryName(ImageTextbox.Text)
              RawFilterResults &amp;= " and imageurl like '" &amp; ImageDir &amp; "%'"
          End If
  
          If CaratTextbox.Text &gt; "" Then
              RawFilterResults &amp;= " and carat = '" &amp; CaratTextbox.Text &amp; "'"
          End If
  
          If CutTextbox.Text &gt; "" Then
              RawFilterResults &amp;= " and cut = '" &amp; CutTextbox.Text &amp; "'"
          End If
  
          If ColorTextbox.Text &gt; "" Then
              RawFilterResults &amp;= " and color = '" &amp; ColorTextbox.Text &amp; "'"
          End If
  
          If ClarityTextbox.Text &gt; "" Then
              RawFilterResults &amp;= " and Clarity = '" &amp; ClarityTextbox.Text &amp; "'"
          End If
  
          If RawFilterResults.StartsWith(" and ") Then
              RawFilterResults = RawFilterResults.Substring(4)
          End If
  
          Return RawFilterResults
  
      End Function
  </code></pre>