• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

Add values to unbound Combo Box

I just getting started with VB.Net.  Been doing VB, VBA (MS Access) for several years.

Building an app with several combo boxes.  The app isn't heavy weight enough to warrant using MS SQL to bind tables to the cbo's.  I can add values to the cbo's at design time no problem.  I've also written code that allows me to add values to the cbo's at run time (both through the IDE and as a finished exe).  

But in both of the later cases the added values are only available as long as the app is running.  Closing it and re-opening it, the values are gone.  What am I not doing?
0
brownpeterg
Asked:
brownpeterg
  • 8
  • 6
1 Solution
 
davidrichardsonCommented:
iWhen are you adding the items ? (What Event)

if you use something like this in the form load event it should work

        With ComboBox1
            ComboBox1.Items.Add("David")
            ComboBox1.Items.Add("Paul")
            ComboBox1.Items.Add("Sarah")
        End With
0
 
brownpetergAuthor Commented:
I re-read my question and I thought I might have been a bit unclear...

Thanks for you suggestion, and the 'add' method is the one I use, but I can not know what new values I'll add.  I have some values in the "Items" propery (collection) that I start with and I can add to them when the program is loaded and running.  

The problem is that any values that I add when after the program is running are not permanently saved in the app.  This is what I want to do, add values to the "Items" collection when the program is running and have the pre-existing and added values both be available after I close and re-open the app.  Hopefully this is a bit clearer.
0
 
davidrichardsonCommented:
you will need to store them in a database or text file and bind your combo to them
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
brownpetergAuthor Commented:
Roger that.  Forgive my newness to VB.Net.  Does the binding to a text file involve the use of a 'dataset'.  Any advise on the trickery involved?  Increased point value, thanks, their yours after this.
0
 
davidrichardsonCommented:
something like this should work

'create a tabtext file and and save it to c:\text.txt
'button 2 opens the file
'bitton 1 saves


Imports System
Imports System.IO
Imports System.Collections
Imports System.Data
Imports System.Text


Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim oDS As New DataSet
    Dim strFields As String
    Dim oTable As New DataTable
    Dim oRows As DataRow
    Dim intCounter As Int32 = 0
    Dim oRow As DataRow()


    Function delimitedDataSet(ByVal strDelimiter As String, ByVal strFilePath As String) As String

        oDS.Tables.Add("ComboItems")

        Dim oSR As New StreamReader(strFilePath)
        'Go to the top of the file
        oSR.BaseStream.Seek(0, SeekOrigin.Begin)
        'Add in the Header Columns
        For Each strFields In oSR.ReadLine().Split(strDelimiter)
            oDS.Tables(0).Columns.Add()
        Next
        'Now add in the Rows

        oTable = oDS.Tables(0)
        While (oSR.Peek() > -1)
            oRows = oTable.NewRow()
            For Each strFields In oSR.ReadLine().Split(strDelimiter)
                oRows(intCounter) = strFields
                intCounter = intCounter + 1
            Next
            intCounter = 0
            oTable.Rows.Add(oRows)
        End While
        dg.DataSource = oDS
        dg.DataMember = "comboitems"

        With ComboBox1
            .DataSource = oDS
            .DisplayMember = "column1"
            .ValueMember = "column1"
            .SelectedIndex = 0
        End With


        Return oDS.GetXml()

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Scorpion As New ArrayList
        For i As Integer = 0 To oDS.Tables("Product_Details").Rows.Count - 1
            Dim row As New System.Text.StringBuilder
            Scorpion.Add(row)
            For y As Integer = 0 To oDS.Tables("Product_Details").Columns.Count - 1
                row.Append(oDS.Tables("Product_Details").Rows(i)(y).tostring)
                If y <> oDS.Tables("Product_Details").Columns.Count - 1 Then
                    row.Append(vbTab)
                End If
            Next
        Next
        Dim sw As New IO.StreamWriter("C:\Test.txt")
        For i As Integer = 0 To Scorpion.Count - 1
            sw.WriteLine(Scorpion(i).ToString)
        Next
        sw.Flush()
        sw.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        delimitedDataSet(vbTab, "c:\test.txt")
    End Sub
0
 
davidrichardsonCommented:
try that again

'add the folloing at the verry top of the form

Imports System
Imports System.IO
Imports System.Collections
Imports System.Data
Imports System.Text

'declaire the following

    Inherits System.Windows.Forms.Form
    Dim oDS As New DataSet
    Dim strFields As String
    Dim oTable As New DataTable
    Dim oRows As DataRow
    Dim intCounter As Int32 = 0
    Dim oRow As DataRow()


'add the following function

    Function delimitedDataSet(ByVal strDelimiter As String, ByVal strFilePath As String) As String

        oDS.Tables.Add("ComboItems")

        Dim oSR As New StreamReader(strFilePath)
        'Go to the top of the file
        oSR.BaseStream.Seek(0, SeekOrigin.Begin)
        'Add in the Header Columns
        For Each strFields In oSR.ReadLine().Split(strDelimiter)
            oDS.Tables(0).Columns.Add()
        Next
        'Now add in the Rows

        oTable = oDS.Tables(0)
        While (oSR.Peek() > -1)
            oRows = oTable.NewRow()
            For Each strFields In oSR.ReadLine().Split(strDelimiter)
                oRows(intCounter) = strFields
                intCounter = intCounter + 1
            Next
            intCounter = 0
            oTable.Rows.Add(oRows)
        End While

        With ComboBox1
            .DataSource = oDS.Tables("ComboItems")
            .DisplayMember = "Column1"
            .ValueMember = "Column1"
        End With


        Return oDS.GetXml()

    End Function


'use this to save the file

        Dim Scorpion As New ArrayList
        For i As Integer = 0 To oDS.Tables("ComboItems").Rows.Count - 1
            Dim row As New System.Text.StringBuilder
            Scorpion.Add(row)
            For y As Integer = 0 To oDS.Tables("ComboItems").Columns.Count - 1
                row.Append(oDS.Tables("ComboItems").Rows(i)(y).tostring)
                If y <> oDS.Tables("ComboItems").Columns.Count - 1 Then
                    row.Append(vbTab)
                End If
            Next
        Next
        Dim sw As New IO.StreamWriter("C:\Test.txt")
        For i As Integer = 0 To Scorpion.Count - 1
            sw.WriteLine(Scorpion(i).ToString)
        Next
        sw.Flush()
        sw.Close()


'use this to open

delimitedDataSet(vbTab, "c:\test.txt")



0
 
brownpetergAuthor Commented:
Thanks again, you've really jumpstarted me :-).  

Just to be sure, "oSR" wasn't in your list of declarations, I assume oSR is meant to be oDS?

And BTW, you got me thinking and I found a simpler way, without using a dataset.  Not all the Kinks are resolved, like "generalizing" the "path" to the text file for deployment, and the delimiter doesn't work as a vbCrLf for some reason (these are lists of URLs if you have a better idea how to delimit them that'd be real helpful).  Here's the function for loading the values (the 'saving' part will simply take the values in the collection and overwrite the values in file.

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Fill_getURL_Names()

    End Sub

    Private Sub Fill_getURL_Names()
        Dim collectionItem As String
        Dim oSR As New StreamReader("GeneralizedPath...\txtfil_getURL_Names.txt")

        For Each collectionItem In getURL_Name.Items
            getURL_Name.Items.Remove(getURL_Name.Text)
        Next

        'Go to the top of the file
        oSR.BaseStream.Seek(0, SeekOrigin.Begin)
        'Add in the Header Columns
        For Each collectionItem In oSR.ReadLine.Split(">")  ' using the '>' character
            getURL_Name.Items.Add(collectionItem)
        Next

    End Sub
0
 
davidrichardsonCommented:
no osr is the StreamReader, from the function
Dim oSR As New StreamReader("GeneralizedPath...\txtfil_getURL_Names.txt")


0
 
brownpetergAuthor Commented:
oops
0
 
davidrichardsonCommented:
if this is a single user app, you could add the text file to your install directory
0
 
brownpetergAuthor Commented:
Right now I have it in the 'Solution Explorer' so I can fool around with it while developing the code.  The file is at the 'root' level in the SE, is there a way to represent the install directory into the SE and add it there?  And thanks for all your help.  I did approve your points, you sure are being helpful!!
0
 
davidrichardsonCommented:
when you create your setup project add it to the 'application folder'
0
 
brownpetergAuthor Commented:
Thanks!
0
 
davidrichardsonCommented:
no problem
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now