copyright 1999 - 2014 by heinz prelle - hannover  - lenbachstraße 42 - www.visual-basic5.de | impressum
'Beispiel: VB .Net - Datei gesplittet in ComboBox einfuegen
'
Option Explicit On
Option Strict On

Imports System.IO
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles Button1.Click
        Me.ReadFileAndSetToComboBox(Path.Combine(Helper.AppPath, "test.txt"), ComboBox1)
    End Sub

    Private Sub ReadFileAndSetToComboBox(ByVal Filename As String, ByVal cb As System.Windows.Forms.ComboBox)
        Try
            Using sr As StreamReader = New StreamReader(Filename, Encoding.Default)
                Dim item As String = ""
                Dim items() As String
                Do
                    item = sr.ReadLine()
                    If item <> "" Then
                        items = item.Split(" "c)
                        cb.Items.AddRange(items)
                    End If
                Loop Until item = Nothing
            End Using
            If cb.Items.Count > 0 Then cb.SelectedIndex = 0
        Catch ex As IOException
            MessageBox.Show(ex.Message.ToString(), "IOException")
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString(), "Exception")
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles MyBase.Load
        CenterToScreen()
    End Sub
End Class

Public Class Helper

    Public Shared ReadOnly Property AppPath() As String
        Get
            Dim Path As String = Application.StartupPath
            If Path.EndsWith("\") = False Then Path &= "\"
            Return Path
        End Get
    End Property

End Class