0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TreeViewで選択したファイルを拡張子ごとに(画像、テキストを表示を切り替える)方法

Posted at

下記のselectfile関数を作成し、PictureBoxとListBoxのプロパティVisibleを変更して切り替えることにしました。

Private Sub selectfile(ByVal filename As System.Windows.Forms.TreeViewEventArgs)
	Dim ext2 As String
	'拡張子取得
	ext2 = Path.GetExtension(filename.Node.FullPath)
	'MsgBox(ext2)

	Select Case ext2
		Case ".bmp"
			ListBox1.Visible = False
			PictureBox1.Visible = True
			PictureBox1.ImageLocation = filename.Node.FullPath
		Case ".txt"
			ListBox1.Visible = True
			PictureBox1.Visible = False

			'テキストファイルを読込みリストボックスに表示
			'リストボックスに水平スクロールバーを表示 縦スクロールバーは自動表示される
			ListBox1.HorizontalScrollbar = True
			ListBox1.Items.Clear()  'リストボックスアイテム初期化(一旦消去)
			Dim st As New System.IO.StreamReader(filename.Node.FullPath, System.Text.Encoding.UTF8)
			'ファイルの最後までループ
			Do Until st.Peek = -1
				'1行づつ読込む
				ListBox1.Items.Add(st.ReadLine)
			Loop
			st.Close()     'ファイルを閉じる
	End Select

各ノードを選択したときのイベントにselectfile関数を呼び出すようにしました。

Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
	'MsgBox(e.Node.FullPath)
	selectfile(e)
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?