簡易行番号エディタコントロール

  仕様




現時点で、RichTextBox に行番号表示する PictureBox を組み合わせている。

※ Editor プロパティで、RichTextBox のインスタンスを取得できる
※ TAB および、SHIFT+TAB で一括インデントを実装




  コード



  
Imports System.Windows.Forms
Imports System.Drawing

Public Class LboxEditor

	Private rowAreaBackColor As System.Drawing.Color = Color.White
	Private lineHeight As Single
	Private lineX As Integer

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

		myText.AcceptsTab = True
		myText.LanguageOption = RichTextBoxLanguageOptions.UIFonts
		myText.Text = " "
		myText.Text = ""
		lineHeight = myText.Font.GetHeight()

	End Sub

	' *******************************************************************
	' 行の再描画
	' *******************************************************************
	Public Sub ResetRowArea(ByVal graphicHandle As Graphics)

		Dim rows As Integer
		Dim leftTop As Integer
		Dim topLine As Integer
		Dim endrow As Integer
		Dim loc As Point

		' 現在の最終行
		endrow = myText.GetLineFromCharIndex(myText.Text.Length)
		' 表示エリアに表示可能な行数
		rows = (myText.ClientRectangle.Height) / myText.Font.Height + 2
		' 少ないほうを採用
		If endrow < rows Then
			rows = endrow
		End If

		' 表示エリアの開始行
		leftTop = myText.GetCharIndexFromPosition(New Point(0, 0))
		topLine = myText.GetLineFromCharIndex(leftTop)

		' 行番号を描画する X 座標
		lineX = Me.rowArea.Width - graphicHandle.MeasureString("0000000", myText.Font).Width
		loc = New Point( _
		   lineX, _
		   myText.GetPositionFromCharIndex(myText.GetFirstCharIndexFromLine(topLine)).Y _
		  )

		' 行番号エリアの背景色
		graphicHandle.Clear(Me.rowAreaBackColor)

		' 行番号を描画
		For i As Integer = topLine To topLine + rows
			graphicHandle.DrawString( _
			 String.Format("{0,7}", i + 1), _
			 myText.Font, _
			 Brushes.Black, _
			 loc)
			loc.Y += lineHeight
		Next

	End Sub

	' *******************************************************************
	' RichTextBox からのイベント
	' *******************************************************************
	Public Shadows Event KeyDown As EventHandler
	Public Shadows Event KeyPress As EventHandler
	Public Shadows Event KeyUp As EventHandler
	Public Shadows Event TextChanged As EventHandler
	Public Shadows Event VScroll As EventHandler
	Public Shadows Event HScroll As EventHandler

	' *******************************************************************
	' 特殊キーの処理
	' *******************************************************************
	Private Sub myText_KeyDown(ByVal sender As System.Object, _
	  ByVal e As System.Windows.Forms.KeyEventArgs) Handles myText.KeyDown

		If e.Control And e.KeyCode = Keys.V Then
			myText.SelectedText = Clipboard.GetText
			e.Handled = True
		End If

		If e.Shift And e.KeyCode = Keys.Insert Then
			myText.SelectedText = Clipboard.GetText
			e.Handled = True
		End If

		If e.KeyCode = Keys.Tab Then
		   If myText.SelectedText.Length <> 0 Then
		      Dim start As Integer = myText.SelectionStart
		      Dim str As String = myText.SelectedText
		      Dim last As String = str.Substring(str.Length - 1, 1)
		      Dim last2 As String = str.Substring(str.Length - 2, 1)

		      If e.Shift Then
		         If str.Substring(0, 1) = ControlChars.Tab Then
		            str = str.Substring(1, str.Length - 1)
		         End If
		         str = str.Replace(ControlChars.Lf + ControlChars.Tab, ControlChars.Lf)
		         myText.SelectedText = str
		         Dim length As Integer = str.Length
		         myText.Select(start, length)
		      Else
		         If last = ControlChars.Lf And last2 = ControlChars.Lf Then
		            str = str.Substring(0, str.Length - 2)
		            str = str.Replace(ControlChars.Lf, ControlChars.Lf + ControlChars.Tab)
		            str = str + ControlChars.Lf + ControlChars.Lf
		            str = ControlChars.Tab + str
		            str = str.Replace(ControlChars.Tab + ControlChars.Lf, ControlChars.Lf)
		            Dim length As Integer = str.Length
		            myText.SelectedText = str
		            myText.Select(start, length)
		         Else
		            str = str.Substring(0, str.Length - 1)
		            str = str.Replace(ControlChars.Lf, ControlChars.Lf + ControlChars.Tab)
		            str = str + last
		            str = ControlChars.Tab + str
		            str = str.Replace(ControlChars.Tab + ControlChars.Lf, ControlChars.Lf)
		            Dim length As Integer = str.Length
		            myText.SelectedText = str
		            myText.Select(start, length)
		         End If

		      End If
		      e.SuppressKeyPress = True
		   End If
		End If

		RaiseEvent KeyDown(sender, e)

	End Sub

	' *******************************************************************
	' キーの処理
	' *******************************************************************
	Private Sub myText_KeyPress(ByVal sender As System.Object, _
	  ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles myText.KeyPress

		RaiseEvent KeyPress(sender, e)

	End Sub

	Private Sub myText_KeyUp(ByVal sender As System.Object, _
	  ByVal e As System.Windows.Forms.KeyEventArgs) Handles myText.KeyUp

		RaiseEvent KeyUp(sender, e)

	End Sub

	' *******************************************************************
	' データが変更された時の処理
	' *******************************************************************
	Private Sub myText_TextChanged(ByVal sender As System.Object, _
	  ByVal e As System.EventArgs) Handles myText.TextChanged

		Me.rowArea.Refresh()
		RaiseEvent TextChanged(sender, e)

	End Sub

	' *******************************************************************
	' フォントが変更された時の処理
	' *******************************************************************
	Private Sub myText_FontChanged(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles myText.FontChanged

		lineHeight = myText.Font.GetHeight()
		Me.rowArea.Refresh()

	End Sub

	' *******************************************************************
	' 縦スクロール
	' *******************************************************************
	Private Sub myText_VScroll(ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) Handles myText.VScroll

		Me.rowArea.Refresh()
		RaiseEvent VScroll(sender, e)

	End Sub

	' *******************************************************************
	' 横スクロール
	' *******************************************************************
	Private Sub myText_HScroll(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles myText.HScroll

		RaiseEvent HScroll(sender, e)

	End Sub

	<System.ComponentModel.Browsable(False)> _
	 Public Overrides Property Font() As System.Drawing.Font
		Get
			Return myText.Font
		End Get
		Set(ByVal value As System.Drawing.Font)
			myText.Font = value
		End Set
	End Property

	' *******************************************************************
	' フォントプロパティ
	' *******************************************************************
	Public Property TextFont() As System.Drawing.Font
		Get
			Return myText.Font
		End Get
		Set(ByVal value As System.Drawing.Font)
			myText.Font = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overrides Property BackColor() As System.Drawing.Color
		Get
			Return myText.BackColor
		End Get
		Set(ByVal value As System.Drawing.Color)
			myText.BackColor = value
		End Set
	End Property

	' *******************************************************************
	' 背景色プロパティ
	' *******************************************************************
	Public Property TextBackColor() As System.Drawing.Color
		Get
			Return myText.BackColor
		End Get
		Set(ByVal value As System.Drawing.Color)
			myText.BackColor = value
		End Set
	End Property

	' *******************************************************************
	' 行番号の背景色プロパティ
	' *******************************************************************
	Public Property RowBackColor() As System.Drawing.Color
		Get
			Return rowAreaBackColor
		End Get
		Set(ByVal value As System.Drawing.Color)
			rowAreaBackColor = value
			Me.rowArea.Refresh()
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overrides Property Text() As String
		Get
			Return myText.Text
		End Get
		Set(ByVal value As String)
			myText.Text = value
		End Set
	End Property

	Public Property [ReadOnly]() As Boolean
		Get
			Return myText.ReadOnly
		End Get
		Set(ByVal value As Boolean)
			myText.ReadOnly = value
		End Set
	End Property

	' *******************************************************************
	' RichTextBox のインスタンス
	' *******************************************************************
	Public ReadOnly Property Editor() As RichTextBox
		Get
			Return myText
		End Get
	End Property

	' *******************************************************************
	' 行番号部分の幅
	' *******************************************************************
	Public Property RowAreaWidth() As Integer
		Get
			Return rowArea.Width
		End Get
		Set(ByVal value As Integer)
			rowArea.Width = value
			myText.Location = New System.Drawing.Point(value + 1, 0)
			myText.Width = Me.Width - value - 5
			Me.rowArea.Refresh()
		End Set
	End Property

	' *******************************************************************
	' このコントロールのサイズ変更
	' *******************************************************************
	Private Sub LboxEditor_SizeChanged(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles MyBase.SizeChanged

		Me.rowArea.Refresh()

	End Sub

	' *******************************************************************
	' 内部 PictureBox( 高速描画用 )
	' *******************************************************************
	Private Class LboxEditorPicture
		Inherits System.Windows.Forms.PictureBox

		Protected Overrides Sub OnCreateControl()

			MyBase.OnCreateControl()
		End Sub

		Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)

			If Not Me.DesignMode Then
				CType(Me.Parent, Object).ResetRowArea(pe.Graphics)
			End If

		End Sub

	End Class

End Class
  










  infoboard   管理者用   





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

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ