PL/SQL ビルダーの作成(6) : ini ファイルの処理 と エディタのポップアップメニュー : 【VB.NET】

↓ソースコードのダウンロード

ブラウザでダウンロード

DBの接続情報と終了時のウインドウの位置
ini ファイルの処理は、WIN32API を使用する必要があるので、
DllImport による、DLL に対する直接アクセスの為の定義を
クラス内で行っています。
INI.vb
#Region "仕様"
REM ***********************************************************
REM INI ファイルの処理
REM コンストラクタで名前を渡し、プログラムが存在するディレクトリ
REM に存在する 名前.ini にアクセスする
REM ***********************************************************
#End Region
#Region "Imports"

' ● StringBuilder
Imports System.Text
' ● DllImport
Imports System.Runtime.InteropServices

#End Region

Public Class INI

	' ******************************************************
	' ini ファイル読み込み
	' ******************************************************
	<DllImport("Kernel32.dll", CharSet:=CharSet.Auto)> _
	Private Shared Function GetPrivateProfileString( _
	   ByVal lpAppName As String, _
	   ByVal lpKeyName As String, _
	   ByVal lpDefault As String, _
	   ByVal lpReturnedString As StringBuilder, _
	   ByVal nSize As Integer, _
	   ByVal lpFileName As String) As Integer
	End Function

	' ******************************************************
	' ini ファイル書き込み
	' ******************************************************
	<DllImport("Kernel32.dll", CharSet:=CharSet.Auto)> _
	Private Shared Function WritePrivateProfileString( _
	   ByVal lpAppName As String, _
	   ByVal lpKeyName As String, _
	   ByVal lpString As String, _
	   ByVal lpFileName As String) As Integer
	End Function

	Public _path As String

	' ********************************************************
	' (コンストラクタの定義)( Sub で定義する )
	' ********************************************************
	Public Sub New(ByVal name As String)

		_path = Application.StartupPath + "\" + name + ".ini"

	End Sub

	' ******************************************************
	' 取得
	' ******************************************************
	Public Function GetValue(ByVal Section As String, ByVal Entry As String) As String

		Dim str As StringBuilder = New StringBuilder(512)

		GetPrivateProfileString(Section, Entry, "", str, 512, _path)

		Return str.ToString()

	End Function

	' ******************************************************
	' 書き込み
	' ******************************************************
	Public Sub SetValue(ByVal Section As String, ByVal Entry As String, ByVal Value As String)

		WritePrivateProfileString(Section, Entry, Value, _path)

	End Sub

End Class
Form1.vb

		・
		・

	Public ini As New INI("plsql_builder")

		・
		・

	' ******************************************************
	' 接続ダイアログ
	' ******************************************************
	Private Sub 接続_Click(ByVal sender As System.Object, _
	 ByVal e As System.EventArgs) Handles 接続.Click

		・
		・

		' ini ファイルに書き込み
		ini.SetValue("Connect", "Server", Server)
		ini.SetValue("Connect", "Service", Service)
		ini.SetValue("Connect", "User", User)
		ini.SetValue("Connect", "Pass", Pass)

	End Sub

	' ******************************************************
	' 初期処理
	' ******************************************************
	Private Sub Form1_Load(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles MyBase.Load

		・
		・

		' ini ファイルの読み込み
		Server = ini.GetValue("Connect", "Server")
		Service = ini.GetValue("Connect", "Service")
		User = ini.GetValue("Connect", "User")
		Pass = ini.GetValue("Connect", "Pass")

		Dim pos As String = ini.GetValue("Window", "Main")
		If pos <> "" Then
			Dim value As String() = pos.Split(",")
			Me.Left = Integer.Parse(value(0))
			Me.Top = Integer.Parse(value(1))
			Me.Width = Integer.Parse(value(2))
			Me.Height = Integer.Parse(value(3))
		End If

		pos = ini.GetValue("Window", "List")
		If pos <> "" Then
			Dim value As String() = pos.Split(",")
			Me.LboxGrid2.Width = Integer.Parse(value(0))
			Me.Panel2.Height = Integer.Parse(value(1))
		End If

		・
		・

	End Sub

	' ******************************************************
	' フォームが閉じた後の処理
	' ******************************************************
	Private Sub Form1_FormClosed(ByVal sender As System.Object, _
	ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed

		If Me.Left >= 0 Then
			ini.SetValue("Window", "Main", _
			 Me.Left.ToString() + "," + _
			 Me.Top.ToString() + "," + _
			 Me.Width.ToString() + "," + _
			 Me.Height.ToString())
		Else
			ini.SetValue("Window", "Main", _
			 "0,0," + _
			 (Me.Width - 16).ToString() + "," + _
			 (Me.Height - 16).ToString())
		End If

		ini.SetValue("Window", "List", _
		  Me.LboxGrid2.Width.ToString() + "," + _
		  Me.Panel2.Height.ToString())

	End Sub

plsql_builder.ini
[Window]
Main=0,0,1272,988
List=497,517
[Connect]
Server=night
Service=XE
User=lightbox
Pass=lightbox



エディタのポップアップメニュー
LboxEditor は、ユーザーコントロールの中に リッチテキストボックス
を配置していますので、そちらの ContextMenuStrip プロパティに設定します

	' ******************************************************
	' 初期処理
	' ******************************************************
	Private Sub Form1_Load(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles MyBase.Load

		・
		・

		' エディタの編集部分にポップアップメニューを実装
		Me.LboxEditor1.Editor.ContextMenuStrip = Me.エディタメニュー

	End Sub

	' ******************************************************
	' エディタメニューの処理
	' ******************************************************
	Private Sub コピー_Click(ByVal sender As System.Object, _
	 ByVal e As System.EventArgs) Handles コピー.Click

		Me.LboxEditor1.Editor.Copy()

	End Sub

	' ******************************************************
	' コメントタイトルを挿入する
	' ******************************************************
	Private Sub コメント_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles コメント.Click

		' クリップボードのデータを保存
		Dim returnValue As IDataObject
		returnValue = Clipboard.GetDataObject()

		' 貼り付けるデータ
		Dim str As String = _
		"/**********************************************************/" + _
		ControlChars.CrLf + _
		"/* */" + _
		ControlChars.CrLf + _
		"/**********************************************************/" + _
		ControlChars.CrLf
		' クリップボードにコピー
		Clipboard.SetText(str)

		' エディタに貼り付け
		Me.LboxEditor1.Editor.Paste()

		' 保存していたデータを戻す
		Clipboard.SetDataObject(returnValue, True)

	End Sub