LboxCommdlg

  VBによるクラス作成



アセンブリ名 : lightbox.tool
ルート名前空間 : lightbox.tool

※ 参照 : Microsoft : コンストラクタとデストラクタの使用方法

  
Imports System.Windows.Forms
Imports System.Text

Public Class LboxCommdlg

	Public path As String = Nothing
	Public title As String
	Public fbd As FolderBrowserDialog
	Public SelectedPath As String = ""

	' ****************************************
	' デフォルトコンストラクタ
	' ****************************************
	Public Sub New()
		MyBase.New()
	End Sub

	' ****************************************
	' コンストラクタ1
	' ****************************************
	Public Sub New(ByVal path As String)
		MyBase.New()
		Me.path = path
	End Sub

	' ****************************************
	' コンストラクタ2
	' ****************************************
	Public Sub New(ByVal path As String, ByVal title As String)
		MyBase.New()
		Me.path = path
		Me.title = title
	End Sub

	' ****************************************
	' ディレクトリ参照
	' ****************************************
	Public Function GetFolderName() As Boolean

		Me.fbd = New FolderBrowserDialog()
		If Me.path <> Nothing Then
			Me.fbd.SelectedPath = Me.path
		End If
		If Me.title <> Nothing Then
			Me.fbd.Description = Me.title
		End If

		If Me.fbd.ShowDialog() = DialogResult.OK Then
			Me.SelectedPath = Me.fbd.SelectedPath
			GetFolderName = True
		Else
			GetFolderName = False
		End If

		Me.fbd.Dispose()

	End Function

	' ****************************************
	' ディレクトリ参照
	' ****************************************
	Public Function GetFolderName2() As String

		Me.fbd = New FolderBrowserDialog()
		If Me.path <> Nothing Then
			Me.fbd.SelectedPath = Me.path
		End If
		If Me.title <> Nothing Then
			Me.fbd.Description = Me.title
		End If

		If Me.fbd.ShowDialog() = DialogResult.OK Then
			GetFolderName2 = Me.fbd.SelectedPath
			Me.SelectedPath = Me.fbd.SelectedPath
		Else
			GetFolderName2 = ""
		End If

		Me.fbd.Dispose()

	End Function

	' ****************************************
	' ディレクトリ参照
	' ****************************************
	Public Function GetFolderName(ByVal path As StringBuilder) As Boolean

		Me.fbd = New FolderBrowserDialog()
		If Me.path <> Nothing Then
			Me.fbd.SelectedPath = Me.path
		End If
		If Me.title <> Nothing Then
			Me.fbd.Description = Me.title
		End If

		If Me.fbd.ShowDialog() = DialogResult.OK Then
			path.Remove(0, path.Length)
			path.Append(Me.fbd.SelectedPath)
			Me.SelectedPath = Me.fbd.SelectedPath
			GetFolderName = True
		Else
			GetFolderName = False
		End If

		Me.fbd.Dispose()

	End Function

End Class
  




  利用サンプルコード



いずれのメソッドを使用しても、プロパティ 「SelectedPath」にはデータがセットされます。

※ 前後のコードの状況や都合によって使い分けます。

  
Imports System.Text
Imports lightbox.tool

Public Class Form1

	' ******************************************************
	' 戻り値で返す
	' ******************************************************
	Private Sub Button1_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles Button1.Click

		Dim dir As New LboxCommdlg("c:\tmp", "テスト")
		Dim path As String

		path = dir.GetFolderName2()
		If path <> "" Then
			MessageBox.Show(path)
		End If

	End Sub

	' ******************************************************
	' 引数で返す
	' ******************************************************
	Private Sub Button2_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles Button2.Click

		Dim dir As New LboxCommdlg("c:\tmp", "テスト")
		Dim path As New StringBuilder(1024)

		If dir.GetFolderName(path) Then
			MessageBox.Show(path.ToString())
		End If

	End Sub

	' ******************************************************
	' プロパティで返す
	' ******************************************************
	Private Sub Button3_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles Button3.Click

		Dim dir As New LboxCommdlg("c:\tmp", "テスト")

		If dir.GetFolderName() Then
			MessageBox.Show(dir.SelectedPath)
		End If

	End Sub
End Class
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ