可以转载,转载请著名作者和出处,谢谢,特别鄙视转载后扣上自己名字的哥们
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Atext As String
Dim blnExist As Boolean
blnExist = False
Atext = TextBox1.Text
Dim i As Integer
For i = 0 To ListBox1.Items.Count() - 1
If ListBox1.Items.Item(i) = Atext Then
MsgBox("error")
blnExist = True
End If
Next i
If blnExist = False Then
ListBox1.Items.Add(Atext)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 1
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK And (saveFileDialog1.FileName.Length) > 0 Then
Dim sw As New StreamWriter(saveFileDialog1.FileName)
For i As Int32 = 0 To ListBox1.Items.Count - 1 Step 1
sw.WriteLine(ListBox1.Items(i))
Next
sw.Close()
MsgBox("保存完毕")
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'Dim strFileName() As String
Dim OpenFileDialog1 As System.Windows.Forms.OpenFileDialog = New System.Windows.Forms.OpenFileDialog()
With OpenFileDialog1
.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
.FilterIndex = 1
.InitialDirectory = "C:\"
.Title = "打开文件"
.Multiselect = False
End With
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
'strFileName = OpenFileDialog1.FileNames
Dim sr As New StreamReader(OpenFileDialog1.FileName)
Dim line As String
Do
line = sr.ReadLine()
If line Is Nothing Then
Exit Do
else
ListBox1.Items.Add(line)
End If
Loop Until line Is Nothing
sr.Close()
MsgBox("读取完毕")
End If
End Sub
End Class
阅读(1682) | 评论(0) | 转发(0) |