Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
’ファイルIO用の変数等の宣言
Dim fname As String Dim ret As DialogResult Dim TextFile As IO.StreamReader Dim Line As String Dim cnt As Integer
’PictureBox1のGraphicsオブジェクトを取得
Dim g As Graphics = PictureBox1.CreateGraphics() Dim ystr As Integer
‘ダイアログボックスの初期設定
With Me.OpenFileDialog1 .Title = “ファイルを開く” .CheckFileExists = True .RestoreDirectory = True .Filter = “テキストファイル|.txt;.dat” .FileName = “test.txt” End With
‘ダイアログボックスの表示
ret = Me.OpenFileDialog1.ShowDialog() If ret = Windows.Forms.DialogResult.OK Then fname = Me.OpenFileDialog1.FileName Else fname = “” End If If IO.File.Exists(fname) = False Then MessageBox.Show(“ファイルが見つかりません。”, “通知”) Exit Sub End If
‘フォントオブジェクトの作成
Dim fnt As New Font(“MS UI Gothic”, 12) Me.ListBox1.Items.Clear() TextFile = New IO.StreamReader(fname, System.Text.Encoding.Default)
cnt = 0 Do While True Line = TextFile.ReadLine() If Line = Nothing Then Exit Do End If Me.ListBox1.Items.Add(Line) cnt = cnt + 1