Results 1 to 9 of 9

Thread: [2005] Adding a list item in a combo box programmatically

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    49

    [2005] Adding a list item in a combo box programmatically

    Hi everyone!

    Can anyone help how on to add an item in a combox box programmatically in addition to the default values that are retrieved from a certain table in the database.

    For example I have a table named "tblDocuments", which lists all the documents related to a particular contract. What I wanted to see in my combo box is an item, let say "<Select>", before it lists all the related documents.

    Thanks in advance!

    Radic

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Adding a list item in a combo box programmatically

    Firstly, if a ComboBox is bound to a data source then you cannot add items directly. You'd have to add an item to the data source.

    Secondly, what you're intending to do is not uncommon but it is pointless. It's like painting the word "Drive" on the side of a car. If you don't want an item selected after you bind the data then set the SelectedIndex property to -1.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    49

    Re: [2005] Adding a list item in a combo box programmatically

    To expound further on the scenario given above. I have ten (10) Questions pertaining to a contract. Each contract may contain one or more related document reference. For example, Contract A has three related documents, namely Document 1, Document 2 & Document 3. In those ten questions, there are some that needs a reference to a particular document, while some dont.

    How can i then accommodate those questions that may not need any reference? Because what happens is, since my combobox is bound to a data source, the first item that is shown is Document 1, instead of a temporary item before it.

    Please help on how to deal with this. Any suggestions...
    Thanks again! I really appreciate your response.

    Radic

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    49

    Re: [2005] Adding a list item in a combo box programmatically

    To expound further on the scenario given above. I have ten (10) Questions pertaining to a contract. Each contract may contain one or more related document reference. For example, Contract A has three related documents, namely Document 1, Document 2 & Document 3. In those ten questions, there are some that needs a reference to a particular document, while some dont.

    How can i then accommodate those questions that may not need any reference? Because what happens is, since my combobox is bound to a data source, in those 10 Questions, the first item that is shown is Document 1, instead of a temporary item before it.

    Please help on how to deal with this. Any suggestions...
    Thanks again! I really appreciate your response.

    Radic

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Adding a list item in a combo box programmatically

    Quote Originally Posted by radic
    what happens is, since my combobox is bound to a data source, the first item that is shown is Document 1, instead of a temporary item before it.
    As I said:
    Quote Originally Posted by jmcilhinney
    If you don't want an item selected after you bind the data then set the SelectedIndex property to -1.
    If some fields are mandatory and some or not then the most appropriate way to show that is with an ErrorProvider. Add one to your form and then for a mandatory ComboBox field you'd handle the SelectedIndexChanged event and do this:
    vb Code:
    1. Dim errorText As String = Nothing
    2.  
    3. If Me.ComboBox1.SelectedIndex = -1 Then
    4.     errorText = "You must select a value for this field."
    5. End If
    6.  
    7. Me.ErrorProvider1.SetError(Me.ComboBox1, errorText)
    Now a little icon will appear next to the ComboBox when an item is not selected and it will display that text in a tool tip when the user mouses over it. You can control the properties of that icon in the Properties window for each individual control and the ErrorProvider component itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    49

    Re: [2005] Adding a list item in a combo box programmatically

    Thanks for the prompt response! =)

    If you don't want an item selected after you bind the data then set the SelectedIndex property to -1.

    But I found a drawback though on the above statement. What if for example the user have chosen an item then suddenly changed his mind and rather set it to blank. Is there a way to get back to Index -1?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Adding a list item in a combo box programmatically

    Quote Originally Posted by radic
    Is there a way to get back to Index -1?
    If you don't want an item selected in a ComboBox then you set the SelectedIndex to -1, plain and simple. If you want the user to be able to clear their selection then you simply handle some appropriate event and do it there. I usually do this:
    vb Code:
    1. Private Sub ComboBox1_KeyDown(ByVal sender As Object, _
    2.                               ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    3.     'Ctrl+Del or Ctrl+0 clears the selection.
    4.     Select Case e.KeyCode
    5.         Case Keys.Delete, _
    6.              Keys.D0, _
    7.              Keys.NumPad0
    8.             If e.Control AndAlso _
    9.                Not e.Shift AndAlso _
    10.                Not e.Alt Then
    11.                 Me.ComboBox1.SelectedIndex = -1
    12.             End If
    13.     End Select
    14. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    49

    Re: [2005] Adding a list item in a combo box programmatically

    Nice idea ha! I haven't thought of that...Thanks thanks! =)

  9. #9
    Registered User RaviIntegra's Avatar
    Join Date
    Mar 2007
    Location
    Pondicherry, India
    Posts
    125

    Re: [2005] Adding a list item in a combo box programmatically

    vb Code:
    1. 'write this code in form load event
    2.         'this code is to load the fields from your database
    3.         Dim con As New OleDb.OleDbConnection("type your connection string")
    4.         Dim dt As New DataTable
    5.         Dim qry As String = "select * from tablename"
    6.         Dim da As New OleDb.OleDbDataAdapter(qry, con)
    7.         Dim ret As Integer = da.Fill(dt)
    8.         ComboBox1.Items.Clear()
    9.         If ret > 0 Then
    10.             For i As Integer = 0 To ret - 1
    11.                 ComboBox1.Items.Add(dt.Rows(i).Item("fieldname")
    12.             Next
    13.         End If
    14.  
    15.         'write this code to add new items in your combobox
    16.         ComboBox1.Items.Add("Item1")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width