Компьютерный форум NoWa.cc

Компьютерный форум NoWa.cc (https://nowa.cc/index.php)
-   .NET (https://nowa.cc/forumdisplay.php?f=298)
-   -   ListBox и VB.net (https://nowa.cc/showthread.php?t=219456)

Skiminok06 14.05.2009 13:28

ListBox и VB.net
 
Подскажите как сохранить и загрузить содержимое listbox в файл?

Vagrod 22.05.2009 21:30

Re: ListBox и VB.net
 
Можно просто в текстовый файл с разделителем. В XML чуть посложнее... но только чуть)

Код:

Imports System.IO

Public Class Form1

    Private Const DATA_SEPARATOR As Char = ";"c

    '>>> Сохраняем список
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim sData As String = String.Empty

        Using stream As New FileStream(Application.StartupPath & "\listbox.data", FileMode.OpenOrCreate)
            Using writer As New StreamWriter(stream)
                For Each Item As Object In lstData.Items
                    sData &= TryCast(Item, String) & DATA_SEPARATOR
                Next

                sData = Mid(sData, 1, Len(sData) - 1)

                writer.Write(sData)
            End Using
        End Using
    End Sub

    '>>> Загружаем список
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sData As String = String.Empty

        Using stream As New FileStream(Application.StartupPath & "\listbox.data", FileMode.OpenOrCreate)
            Using reader As New StreamReader(stream)
                sData = reader.ReadToEnd
            End Using
        End Using

        If sData = String.Empty Then Return

        Dim ListItems As String() = sData.Split(DATA_SEPARATOR)

        For i As Short = 0 To ListItems.Length - 1
            lstData.Items.Add(ListItems(i))
        Next
    End Sub

    '>>> Добавляем новый Item
    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        Dim s As String = InputBox("Enter here new list box item text:", "Add New Item", "New Item " & String.Format(CStr(lstData.Items.Count + 1), "00"))

        If s = String.Empty Then Return

        lstData.Items.Add(s)
    End Sub

End Class



Текущее время: 13:30. Часовой пояс GMT +3.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2026, vBulletin Solutions, Inc. Перевод: zCarot
Copyright ©2004 - 2026 NoWa.cc

Время генерации страницы 0.01892 секунды с 9 запросами