'以下程式碼為 VB2010編寫,不確定VB6.0 是否適用...
'如果要用的話,直接將以下程式碼完整複製
'不用另外創立元件,已在程式碼內創立
Imports System.IO
Public Class Form1
Dim aks As String
Dim bu1 As New Button
Dim com1 As New ComboBox
Dim lis1, lis2 As New ListBox
Dim lab1 As New Label
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Me
.Controls.Add(bu1)
.Controls.Add(com1)
.Controls.Add(lis1)
.Controls.Add(lis2)
.Controls.Add(lab1)
End With
With bu1
.Width = 200
.Height = 50
.Location = New Point(Me.Width - 250, 10)
.Text = "創立+寫入" & Chr(10) & "讀取+List列出"
End With
With lab1
.AutoSize = False
.Location = New Point(bu1.Location.X, (bu1.Location.Y + bu1.Height) + 10)
.Width = bu1.Width
.Height = bu1.Height
.BackColor = Color.Salmon
.BorderStyle = BorderStyle.Fixed3D
End With
With com1
.Width = 150
.Height = 50
.Location = New Point(10, 10)
.Visible = False
End With
With lis1
.Location = New Point((com1.Width + com1.Location.X) + 10, 10)
.Height = Me.Height - 100
.Width = 200
End With
With lis2
.Location = New Point((lis1.Width + lis1.Location.X) + 10, 10)
.Height = Me.Height - 100
.Width = 250
End With
AddHandler bu1.Click, AddressOf Button1_Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'若讀寫功能要分開 讀區域要另外+
'dim kk1 as string
'寫區域
Dim w As StreamWriter
Dim kk1 As String
For akk = 1 To 500
aks = "此數值為:" & akk
com1.Items.Add(aks)
kk1 = aks
w = File.CreateText("C:\tp\" & aks & ".txt")
w.WriteLine(kk1)
w.Close()
Next
'讀區域
Dim r As StreamReader
For akk = 1 To 500
aks = "此數值為:" & akk
kk1 = aks
r = New StreamReader("C:\tp\" & aks & ".txt")
Do While Not r.EndOfStream
lis1.Items.Add(r.ReadToEnd)
Loop
r.Close()
Next
AddHandler lis1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
'讀區域
Dim kee As String
Dim r As StreamReader
MsgBox(lis1.Text)
Dim FileExists As Boolean
'Dim filePath = "C:\tp\" & "此數值為:1" & ".txt"
Dim filePath = "C:\tp\" & lis1.Text & ".txt"