editable combobox?

Netnoobie

Regular
Joined
Feb 6, 2003
Messages
94
Location
Philadelphia
Hello all-

I have a ComboBox in a DataGrid pulling items from another DataSet. How can I create the ComboBox to allow text? Like if you don't se the item you'd like in the list, you can type in another. It seems like a simple thing, so maybe I'm just lacking enough coffee to recall.

Many thanks.
 
I still can't seem to get this resolved. I have the combobox being created with the ropdown style, that allows text. But this method does not save the text to the dropdown. I'm just curiousl if anyone else has come across this before and has ever found a solution. Thanks.
 
What I need is for this to hapen dynamically. If I give you 4 options and you need to add a fifth, you can just type that value in the combo and when the control loses focus, the value is then saved in the list. I'n not sure how to do this in .NET.
 
Okay,

First add the new value to the combo box list with an event handler...such as the mouse leave or leave event - also check that its not a duplication.
Visual Basic:
    Private Sub ComboBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
    ComboBox1.Leave, ComboBox1.MouseLeave
        If Not ComboBox1.Items.Contains(ComboBox1.Text) Then
            ComboBox1.Items.Add(ComboBox1.Text)
        End If
    End Sub

Then save it to disk when the form closes in a serialized array list

At the top of you form, above the Public Class line import the following:

Visual Basic:
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

then in the form closing event save the contents of ComboBox1
Visual Basic:
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Try
            Dim formatter As New BinaryFormatter()
            Dim stream As New FileStream(Application.StartupPath & "/myItems.tmp", FileMode.Create, FileAccess.Write, FileShare.None)
            Dim ItemList As New ArrayList()
            Dim itm As Integer
            For itm = 0 To ComboBox1.Items.Count - 1
                ItemList.Add(ComboBox1.Items(itm))
            Next
            formatter.Serialize(stream, ItemList)
            stream.Close()
        Catch
        End Try
        Me.Dispose()
    End Sub

And finally load myItems.tmp into ComboBox1
Visual Basic:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try
            Dim formatter As New BinaryFormatter()
            Dim stream As New FileStream(Application.StartupPath & "/myItems.tmp", FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim ItemList As New ArrayList()
            ItemList = CType(formatter.Deserialize(stream), ArrayList)
            stream.Close()
            ComboBox1.Items.Clear()
            Dim itm As Integer
            For itm = 0 To ItemList.Count - 1
                ComboBox1.Items.Add(ItemList(itm))
            Next
        Catch
        End Try
    End Sub

I hope that helps you.
 
OPPS...the form load event will produce an error on loading the firat time as myItems.tmp doesn't exist yet. So use this instead in the form load event.
Visual Basic:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            If File.Exists(Application.StartupPath & "/myItems.tmp") Then
                Dim formatter As New BinaryFormatter()
                Dim stream As New FileStream(Application.StartupPath & "/myItems.tmp", FileMode.Open, FileAccess.Read, FileShare.Read)
                Dim ItemList As New ArrayList()
                ItemList = CType(formatter.Deserialize(stream), ArrayList)
                stream.Close()
                ComboBox1.Items.Clear()
                Dim itm As Integer
                For itm = 0 To ItemList.Count - 1
                    ComboBox1.Items.Add(ItemList(itm))
                Next
                ComboBox1.SelectedIndex = 0
            Else
                'do something here if the file doesn't exist
                'like load ComboBox1 with your default values
                ComboBox1.Items.Add("1st Item")
                ComboBox1.Items.Add("2nd Item")
                ComboBox1.Items.Add("3rd Item")
                ComboBox1.SelectedIndex = 0
            End If
        Catch
        End Try
    End Sub
 
Back
Top