Malo si zakomplikovao sve, ali evo jedne prostije verzije. Program prilikom pokretanja učitava sve .txt fajlove iz tekućeg foldera u ListView1. Sadržaj se prikazuje u RichTextBox1, i dodano je nekoliko butona za upravljanje. Fajlovi se čitaju kao PlainText stream type, tako da ako hoćeš da zadržiš i formatiranje, onda moraš da staviš "RichTextBoxStreamType.PlainText" za stream type.
Na formi se nalaze:
ListView1 (tu se nalazi spisak fajlova)
RichTextBox1 (sadržaj učitanog fajla)
Buttoni:
btnRefresh (osvežava listu fajlova)
btnLoad (učitava izabrani fajl iz ListView1 u RichTextBox1)
btnNew (prazni RichTextBox1)
btnSave (poziva save diajlog i snima fajl)
A ovo je kod:
Code:
Public Class Form1
'' MOJE PRIVATNE FUNKCIJE
Private Function UpdateFileList()
ListView1.Items.Clear()
Dim folderInfo As New IO.DirectoryInfo(Environment.CurrentDirectory())
Dim arrFilesInFolder() As IO.FileInfo
Dim fileInFolder As IO.FileInfo
arrFilesInFolder = folderInfo.GetFiles("*.txt")
For Each fileInFolder In arrFilesInFolder
ListView1.Items.Add(fileInFolder.Name)
Next
End Function
Private Function LoadFile()
Dim FileName As String = ListView1.SelectedItems(0).Text
Try
RichTextBox1.LoadFile(FileName, RichTextBoxStreamType.PlainText)
Catch ex As Exception
MsgBox("Nisam uspeo učitati fajl, format nije odgovarajući.")
End Try
End Function
'' KONTROLE SA FORME
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
UpdateFileList()
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
UpdateFileList()
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
LoadFile()
End Sub
Private Sub ListView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
LoadFile()
End Sub
Private Sub btnNewFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewFile.Click
RichTextBox1.Text = ""
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
SaveFileDialog1.DefaultExt = ".txt"
SaveFileDialog1.Filter = "Text Files|*.txt"
SaveFileDialog1.InitialDirectory = Environment.CurrentDirectory()
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> "" Then
RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
End If
UpdateFileList()
End Sub
End Class
Dole ti je prikačen ceo projekat, Visual Studio 2005.