Need the first line of a combo box to be blank enterable

Posted on 2006-04-25
Last Modified: 2010-04-23
I load CboAgency with the code below. How can I enter a blank row before ALLl  so user can enter his/her desireable value?

TheAgenyIdTable = GetAgencyId()
            cboAgencyId.DataSource = TheAgenyIdTable
            cboAgencyId.DataTextField = "AGENCY_Id"
            cboAgencyId.DataValueField = "AGENCY_Id"
            cboAgencyId.Items.Insert(0, New ListItem("--ALL--", ""))
Question by:sraymond101
    LVL 44

    Expert Comment

    Hello sraymond101,

    you could look at
    1. include it in the datasource by changing the SQL to inlcude one blank row
    2. add it to the datasource as showed in that PAQ > Ds.Tables("mytable").Rows.Add(Ds.Tables("mytable").NewRow)

    hope this helps a bit

    Author Comment

    Adding this line cboAgencyId.Items.Insert(0, New ListItem("", "")) under   cboAgencyId.Items.Insert(0, New ListItem("--ALL--", "")) creates the blanks row but it reads only, and the user can not  enter data there. I would like for the user to be able to start typing into the blank row and the blank is populated with a number from the combo box

    Author Comment

    Resolve: My comment above fix it. I just to remove isposback from the cboAgencyId property
    LVL 1

    Accepted Solution

    Here is a control that inherits from the combobox. It doesn't add a blank row but will handle a null/empty value. You'll need to bind to "SelectedValueEx".

    On press of backspace/delete it will clear the value also.

    Hope its some use:

     Public Class TheDropDownList
        Inherits ComboBox

    #Region " Windows Form Designer generated code "

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
          If disposing Then
            If Not (components Is Nothing) Then
            End If
          End If
        End Sub
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

          Me.Name = "TheDropDownList"
          Me.Size = New System.Drawing.Size(543, 96)

        End Sub

    #End Region

        Const _NullText As String = "-ALL-" ' "(Not Selected)"

        Event SelectedValueExChanged As EventHandler

        Private m_ValueEx As Object
        Property SelectedValueEx() As Object
            Return m_ValueEx
          End Get
          Set(ByVal value As Object)
          End Set
        End Property

        Private Sub SetSelectedValueEx(ByVal value As Object)
          Static Running As Boolean
          If Running Then Exit Sub
            Running = True
            If value Is Nothing OrElse value Is DBNull.Value OrElse CStr(value) = "" OrElse CStr(value) = "0" Then
              'Raises the SelectedIndexChanged and SelectedValueChanged events
              Me.SelectedIndex = -1
              Me.Text = _NullText
              m_ValueEx = DBNull.Value
              m_ValueEx = value
              Me.SelectedValue = m_ValueEx
            End If
            RaiseEvent SelectedValueExChanged(Me, New EventArgs)
            Running = False
          End Try
        End Sub

        Private Sub DropDownList_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SelectedValueChanged
        End Sub

        Private Sub DropDownList_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            e.Handled = True
            e.SuppressKeyPress = True
            Select Case e.KeyCode
              Case Keys.Delete, Keys.Back
                'Clear the Value
            End Select
        End Sub

        Public Sub New()

          'This call is required by the Windows Form Designer.

        End Sub
    End Class

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now