Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: VB.NET: Tiedostolistaus VB 2012

heikkju2 [23.01.2013 21:44:55]

#

Olempa taasen ihan pihalla, VB6 on olio nimeltään Filelist joka näyttää
määrätyn hakemiston sisällön, mikä vb2012 toimisi samallatavalla
kokeiilin filedialogia mutta en saanut sitä pelittämään.

neau33 [24.01.2013 01:47:33]

#

Moi heikkju2!

'Formille:
'1 OpenFiledialog (openFileDialog1)
'1 FolderBrowserDialog (folderBrowserDialog1)
'2 Listboxia (listBox1 & listBox2)
'3 Komentonappia (button1, button2 & button3)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

	openFileDialog1.FileName = String.Empty
	openFileDialog1.InitialDirectory = _
	Environment.GetFolderPath( _
	Environment.SpecialFolder.MyDocuments) 'Omat tiedostot -kansio
	openFileDialog1.Filter = "Kaikki tiedostot (*.*)|*.*"

	If openFileDialog1.ShowDialog = DialogResult.OK Then
		Select Case Microsoft.VisualBasic.Right(openFileDialog1.FileName, 4).ToLower
			Case ".txt"
				Shell("Notepad.exe " & openFileDialog1.FileName, AppWinStyle.NormalFocus)
			Case ".exe"
				Shell(openFileDialog1.FileName, AppWinStyle.NormalFocus)
			'Case jen.
				'jne...
			Case Else
				'Do nothing...
		End Select
	End If

End Sub

'pari muuta tapaa listata tiedostoja

'VB.NET tyyliin
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

	If folderBrowserDialog1.ShowDialog = DialogResult.OK Then

		Dim Hakemisto As New DirectoryInfo(folderBrowserDialog1.SelectedPath)

		Dim tiedostot() As FileInfo = Hakemisto.GetFiles("*.*")

		If tiedostot.Length > 0 Then
			For i As Integer = 0 To tiedostot.GetUpperBound(0)
				listBox1.Items.Add(tiedostot(i).ToString)
			Next
		End If

	End If

End Sub

'VB6 tyyliin
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

	Dim tiedosto As String
	Dim Hakemisto As String = "C:\temp\" 'esim.
	tiedosto = Dir(Hakemisto + "*.*")

	Do Until tiedosto = ""
		listBox2.Items.Add(tiedosto)
		tiedosto = Dir()
	Loop

End Sub

heikkju2 [24.01.2013 18:40:28]

#

Kiitos taas kerran

neau33 [26.01.2013 03:42:44]

#

Moi taas heikkju2!

Kun sulla tuntuu olevan Vb Old (VB6 ja alaspäin) hanskassa ja VB.NET tökkii niin tässä vielä tiedostolistausta VB.NET:llä vanhaan kunnon Vb Old malliin...

'HUOM! esimerkki on väännetty SharpDevelop 4.2:lla
'Formille:
'3 ListBox ohjausobjektia (driveListBox, dirListBox &  fileListBox)
'3 Label ohjausobjektia (driveListLbl, lblPlus & lblMinus)

Imports System.IO
Imports System.Drawing
Imports System.Diagnostics
Imports VB = Microsoft.VisualBasic

Public Partial Class MainForm

	Private driveListReady As Boolean = False
	Private driveListIndex As Integer
	Private curDrive As String = String.Empty
	Private curFolder As String = String.Empty
	Private fullPath As String = String.Empty
	Private MySubDirs As DirectoryInfo = Nothing
	Private dirArray() As String

	Public Sub New()

		Me.InitializeComponent()

	End Sub

	Sub MainFormLoad(sender As Object, e As EventArgs)

		Me.lblPlus.Size = New Size(dirListBox.ItemHeight,dirListBox.ItemHeight)
		Me.lblMinus.Size = Me.lblPlus.Size
		Me.lblPlus.BackColor = Color.FromKnownColor(KnownColor.ControlLight)
		Me.lblMinus.BackColor = Me.lblPlus.BackColor
		Me.lblPlus.Font = New Font("Microsoft Sans Serif", 9!, _
		FontStyle.Bold, GraphicsUnit.Point, CType(0,Byte))
		Me.lblMinus.Font = Me.lblPlus.Font
		Me.lblPlus.BringToFront
		Me.lblMinus.BringToFront
		Me.lblPlus.TextAlign = ContentAlignment.MiddleCenter
		Me.lblMinus.TextAlign = Me.lblPlus.TextAlign

		driveListBox.DataSource = My.Computer.FileSystem.Drives

		For i As Integer = 0 To driveListBox.Items.Count - 1
			If driveListBox.Items(i).ToString = Environ("HOMEDRIVE") + "\" Then
				driveListBox.SelectedIndex = i:Exit For
			End If
		Next

		curFolder = curDrive
		dirListBox_Populate(curFolder)
		driveListIndex = driveListBox.SelectedIndex
		driveListReady = True

	End Sub

	Sub DriveListBoxSelectedIndexChanged(sender As Object, e As EventArgs)

		curDrive = driveListBox.SelectedItem.ToString
		fullPath = String.Empty

		If fileListBox.Items.Count > 0 Then
			fileListBox.Items.Clear
		End If

		If dirListBox.Items.Count > 0 Then
			dirListBox.Items.Clear
		End If

		Dim allDrives() As DriveInfo = DriveInfo.GetDrives()

		For Each d As DriveInfo In allDrives
			If d.Name = driveListBox.SelectedItem.ToString Then
				Select Case d.Name
					Case "A:\", "B:\"
						driveListLbl.Text = "Floppy drive"
					Case Else
						Select Case d.DriveType.ToString
							Case "Removable"
								driveListLbl.Text = "USB drive"
							Case Else
								driveListLbl.Text = d.DriveType.ToString + " drive"
						End Select
				End Select
			End If
		Next

		If driveListReady Then
			dirListBox_Populate(DriveListBox.SelectedItem.ToString)
		End If

	End Sub

	Sub LblPlusMouseClick(sender As Object, e As MouseEventArgs)

		If MySubDirs IsNot Nothing Then
			lblPlus.Visible = False
			DirListBox_Populate(curFolder)
			MySubDirs = Nothing
		End If

	End Sub

	Sub LblMinusMouseClick(sender As Object, e As MouseEventArgs)

		lblMinus.Visible = False

		If VB.InStr(dirArray(0),"\") <> _
		VB.InStrRev(dirArray(0), "\") Then
			curFolder = VB.Left(curFolder, curFolder.Length - 1)
			curFolder = VB.Left(curFolder,InStrRev(curFolder,"\"))
		Else
			curFolder = curDrive
		End If

		DirListBox_Populate(curFolder)

	End Sub

	Sub DirListBoxSelectedIndexChanged(sender As Object, e As EventArgs)

		fullPath = String.Empty
		fileListBox.Items.Clear
		Dim MyDirs As DirectoryInfo = New DirectoryInfo( _
		dirArray(dirListBox.SelectedIndex))

		Try
			Dim MyFiles() As FileInfo = MyDirs.GetFiles("*.*")

			curFolder = dirArray(dirListBox.SelectedIndex)

			If MyFiles.Length > 0 Then
				For i As Integer = 0 To MyFiles.GetUpperBound(0)
					fileListBox.Items.Add(MyFiles(i).ToString)
				Next
			End If

			If fileListBox.Items.Count > 0 Then
				fileListBox.SelectedIndex = 0
			End If
		Catch ex As Exception
		End Try

	End Sub

	Sub DirListBoxMouseMove(sender As Object, e As MouseEventArgs)

		Dim index As Integer = DirListBox.IndexFromPoint(New Point(e.X, e.Y))
		Dim strDbl() As String = (CDbl(e.Y / dirListBox.ItemHeight)).ToString.Split(",".ToCharArray)
		Dim itemTop As Integer = DirListBox.Top + (CInt(strDbl(0)) * DirListBox.ItemHeight)

		If index = DirListBox.SelectedIndex And index = 0 Then

			If DirListBox.SelectedItem.ToString.Length > 3 Then
				lblMinus.Left = dirListBox.Left + _
				dirListBox.ClientRectangle.Width - lblMinus.Width + 2
				lblMinus.Top = DirListBox.Top + 1
				lblMinus.Visible = True
			End If

		ElseIf index = DirListBox.SelectedIndex And index > 0 Then

			lblMinus.Visible = False

			MySubDirs = New DirectoryInfo(curFolder)

			If MySubDirs.GetDirectories().Length > 0 Then
				lblPlus.Left = dirListBox.Left + _
				dirListBox.ClientRectangle.Width - lblPlus.Width + 2
				lblPlus.Top = itemTop
				lblPlus.Visible = True
			Else
				MySubDirs = Nothing
			End If

		Else
			lblMinus.Visible = False
			lblPlus.Visible = False
		End If

	End Sub

	Sub DirListBox_Populate(dInfo As String)

		DirListBox.Items.Clear
		fullPath = String.Empty
		Erase dirArray

		Dim MyDirs = New DirectoryInfo(dInfo)
		dirListBox.Items.Add(curFolder )

		ReDim dirArray(0)
		dirArray(0) = curFolder

		Try
			Dim cnt As Integer
			For Each item In MyDirs.GetDirectories()
				cnt += 1
				ReDim Preserve dirArray(cnt)
				dirArray(cnt) = item.FullName + "\"
				dirListBox.Items.Add(item.Name)
			Next
			driveListIndex = driveListBox.SelectedIndex
			If dirListBox.Items.Count > 0 Then
				dirListBox.SelectedIndex = 0
			End If
		Catch ex As Exception
			MsgBox(ex.Message)
			driveListBox.SelectedIndex = driveListIndex
		End Try

	End Sub

	Sub FileListBoxSelectedIndexChanged(sender As Object, e As EventArgs)
		fullPath = curFolder +  fileListBox.SelectedItem.ToString
	End Sub

	Sub FileListBoxMouseDoubleClick(sender As Object, e As MouseEventArgs)

		If fullPath = String.Empty Then Exit Sub

		Select Case VB.Right(fullPath.ToLower, 4)
			Case ".txt" 'esim.
				Shell("Explorer.exe " + fullPath, AppWinStyle.NormalFocus)
				Me.WindowState = FormWindowState.Minimized
			Case ".exe"
				Me.WindowState = FormWindowState.Minimized
				Process.Start(fullPath)
		End Select

	End Sub

End Class

GUIn asettelu vois näyttää vaikka tältä ja käyttö vois tapahtua esim. tähän malliin...

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta