Step 1: Start Visual Studio and create Windows Form Application in C# or VB as you wish.
Step 2: Add CheckBox and RichTextBox to your form.
Step 3: Add KeyPress and KeyDown events to RichTextBox.
Step 4: Now add the following code defined in KeyPress and KeyDown event into your code.
How it Works
It's simple, just identify which key is pressed in richTextBox, if it is ( open round bracket then read SelectionStart from richTextBox and insert opposite character to richTextBox at position SelectionStart+1.
Finally add one line once opposite character is inserted and that is e.Handled=true;
C# Code for Auto Complete Brackets,
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
-
- namespace Auto_Complete_Brackets
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
-
-
- public static Boolean isCurslyBracesKeyPressed = false;
-
-
-
-
-
-
-
- private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- String s = e.KeyChar.ToString();
- int sel = richTextBox1.SelectionStart;
- if (checkBox1.Checked == true)
- {
- switch (s)
- {
- case "(": richTextBox1.Text = richTextBox1.Text.Insert(sel, "()");
- e.Handled = true;
- richTextBox1.SelectionStart = sel + 1;
- break;
-
- case "{":
- String t = "{}";
- richTextBox1.Text = richTextBox1.Text.Insert(sel, t);
- e.Handled = true;
- richTextBox1.SelectionStart = sel + t.Length - 1;
- isCurslyBracesKeyPressed = true;
- break;
-
- case "[": richTextBox1.Text = richTextBox1.Text.Insert(sel, "[]");
- e.Handled = true;
- richTextBox1.SelectionStart = sel + 1;
- break;
-
- case "<": richTextBox1.Text = richTextBox1.Text.Insert(sel, "<>");
- e.Handled = true;
- richTextBox1.SelectionStart = sel + 1;
- break;
-
- case "\"": richTextBox1.Text = richTextBox1.Text.Insert(sel, "\"\"");
- e.Handled = true;
- richTextBox1.SelectionStart = sel + 1;
- break;
-
- case "'": richTextBox1.Text = richTextBox1.Text.Insert(sel, "''");
- e.Handled = true;
- richTextBox1.SelectionStart = sel + 1;
- break;
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
- {
- int sel = richTextBox1.SelectionStart;
- if (e.KeyCode == Keys.Enter)
- {
- if(isCurslyBracesKeyPressed==true)
- {
- richTextBox1.Text = richTextBox1.Text.Insert(sel, "\n \n");
- e.Handled = true;
- richTextBox1.SelectionStart = sel + " ".Length;
- isCurslyBracesKeyPressed = false;
- }
- }
- }
- }
- }
VB.Net Code for Auto Complete Brackets
- Imports System
- Imports System.Collections.Generic
- Imports System.ComponentModel
- Imports System.Data
- Imports System.Drawing
- Imports System.Windows.Forms
- Public Class Form1
-
-
-
- Public Shared isCurslyBracesKeyPressed As [Boolean] = False
-
-
-
-
-
-
-
- Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
- Dim s As [String] = e.KeyChar.ToString()
- Dim sel As Integer = RichTextBox1.SelectionStart
- If checkBox1.Checked = True Then
- Select Case s
- Case "("
- RichTextBox1.Text = RichTextBox1.Text.Insert(sel, "()")
- e.Handled = True
- RichTextBox1.SelectionStart = sel + 1
- Exit Select
-
- Case "{"
- Dim t As [String] = "{}"
- RichTextBox1.Text = RichTextBox1.Text.Insert(sel, t)
- e.Handled = True
- RichTextBox1.SelectionStart = sel + t.Length - 1
- isCurslyBracesKeyPressed = True
- Exit Select
-
- Case "["
- RichTextBox1.Text = RichTextBox1.Text.Insert(sel, "[]")
- e.Handled = True
- RichTextBox1.SelectionStart = sel + 1
- Exit Select
-
- Case "<"
- RichTextBox1.Text = RichTextBox1.Text.Insert(sel, "<>")
- e.Handled = True
- RichTextBox1.SelectionStart = sel + 1
- Exit Select
-
- Case """"
- RichTextBox1.Text = RichTextBox1.Text.Insert(sel, """""")
- e.Handled = True
- RichTextBox1.SelectionStart = sel + 1
- Exit Select
-
- Case "'"
- RichTextBox1.Text = RichTextBox1.Text.Insert(sel, "''")
- e.Handled = True
- RichTextBox1.SelectionStart = sel + 1
- Exit Select
- End Select
- End If
- End Sub
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
- Dim sel As Integer = RichTextBox1.SelectionStart
- If e.KeyCode = Keys.Enter Then
- If isCurslyBracesKeyPressed = True Then
- RichTextBox1.Text = RichTextBox1.Text.Insert(sel, vbLf & " " & vbLf)
- e.Handled = True
- RichTextBox1.SelectionStart = sel + " ".Length
- isCurslyBracesKeyPressed = False
- End If
- End If
- End Sub
- End Class