Results 1 to 1 of 1

Thread: Auto Indent Line

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Auto Indent Line

    Hi I I been playing with the codeDOM compiler of an example I seen in here, and decided to make my own little IDE only for console apps.

    anyway I did not like the normal TAB spaces so I replaced them with 4 spaces, I also liked the auto indent feature in the VB editor and wanted some like that for my IDE anyway here what I made.
    Hope you find it useful comments and suggestions welcome.

    Code:
    'Put a TextBox on a form name it txtcode and paste this code in top area of form.
    'Press F5 hit Tab and then press enter you see it indent the new line.
    
    Public Class Form1
    
        Public Function GetWhiteCount(ByVal Source As String) As Integer
            Dim idx As Integer = 0
            'Count White Spaces On Left Of String.
            For Each c As Char In Source
                If c <> " " Then
                    Exit For
                End If
                idx += 1
            Next c
            'Return Count.
            Return idx
        End Function
    
        Public Function FillChar(ByVal c As Char, ByVal Size As Integer) As String
            Return New String(c, Size)
        End Function
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            txtCode.AcceptsTab = True
        End Sub
    
        Private Sub txtCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCode.KeyPress
            Dim sLine As String = vbNullString
            Dim iCount As Integer = 0
            Dim Line As Integer = 0
    
            If (e.KeyChar = vbTab) Then
                e.Handled = True
                e.KeyChar = CChar(vbNullChar)
                'Replace TABs with 4 Spaces.
                txtCode.SelectedText = FillChar(" "c, 4)
            End If
    
            'Exit If TextBox Is Empry
            If String.IsNullOrEmpty(txtCode.Text) Then
                Exit Sub
            End If
    
            If (e.KeyChar = vbCr) Then
                e.Handled = True
                'Get Current Line.
                Line = txtCode.GetLineFromCharIndex(txtCode.SelectionStart)
    
                Try
                    'Get Text Line.
                    sLine = txtCode.Lines(Line)
                    iCount = GetWhiteCount(sLine)
                Catch ex As Exception
                    iCount = 0
                End Try
                 'Add CRLF And Extra Indent
                txtCode.SelectedText = vbCrLf & FillChar(" "c, iCount)
            End If
    
        End Sub
    End Class
    Last edited by BenJones; Jan 27th, 2013 at 05:30 PM. Reason: Bug Fix

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width