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,
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. namespace Auto_Complete_Brackets
  8. {
  9. public partial class Form1 : Form
  10. {
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. //declare isCurslyBracesKeyPressed variable as Boolean and assign false value
  16. //to check { key is pressed or not
  17. public static Boolean isCurslyBracesKeyPressed = false;
  18. //richTextBox1 KeyPress events
  19. // if key (,{,<,",',[ is pressed then insert opposite key to richTextBox1 at Position SelectionStart+1
  20. // add one line after inserting, e.Handled=true;
  21. //finally set SelectionStart to specified position
  22. private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
  23. {
  24. String s = e.KeyChar.ToString();
  25. int sel = richTextBox1.SelectionStart;
  26. if (checkBox1.Checked == true)
  27. {
  28. switch (s)
  29. {
  30. case "(": richTextBox1.Text = richTextBox1.Text.Insert(sel, "()");
  31. e.Handled = true;
  32. richTextBox1.SelectionStart = sel + 1;
  33. break;
  34. case "{":
  35. String t = "{}";
  36. richTextBox1.Text = richTextBox1.Text.Insert(sel, t);
  37. e.Handled = true;
  38. richTextBox1.SelectionStart = sel + t.Length - 1;
  39. isCurslyBracesKeyPressed = true;
  40. break;
  41. case "[": richTextBox1.Text = richTextBox1.Text.Insert(sel, "[]");
  42. e.Handled = true;
  43. richTextBox1.SelectionStart = sel + 1;
  44. break;
  45. case "<": richTextBox1.Text = richTextBox1.Text.Insert(sel, "<>");
  46. e.Handled = true;
  47. richTextBox1.SelectionStart = sel + 1;
  48. break;
  49. case "\"": richTextBox1.Text = richTextBox1.Text.Insert(sel, "\"\"");
  50. e.Handled = true;
  51. richTextBox1.SelectionStart = sel + 1;
  52. break;
  53. case "'": richTextBox1.Text = richTextBox1.Text.Insert(sel, "''");
  54. e.Handled = true;
  55. richTextBox1.SelectionStart = sel + 1;
  56. break;
  57. }
  58. }
  59. }
  60. // richTextBox1 Key Down event
  61. /*
  62. * when key { is pressed and {} is inserted in richTextBox
  63. * and isCurslyBracesKeyPressed is true then insert some blank text to richTextBox1
  64. * when Enter key is down
  65. * it will look like this when Enter key is down
  66. {
  67. |
  68. }
  69. * */
  70. private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
  71. {
  72. int sel = richTextBox1.SelectionStart;
  73. if (e.KeyCode == Keys.Enter)
  74. {
  75. if(isCurslyBracesKeyPressed==true)
  76. {
  77. richTextBox1.Text = richTextBox1.Text.Insert(sel, "\n \n");
  78. e.Handled = true;
  79. richTextBox1.SelectionStart = sel + " ".Length;
  80. isCurslyBracesKeyPressed = false;
  81. }
  82. }
  83. }
  84. }
  85. }
VB.Net Code for Auto Complete Brackets
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System.Windows.Forms
  7. Public Class Form1
  8. 'declare isCurslyBracesKeyPressed variable as Boolean and assign false value
  9. 'to check { key is pressed or not
  10. Public Shared isCurslyBracesKeyPressed As [Boolean] = False
  11. 'RichTextBox1 KeyPress events
  12. ' if key (,{,<,",',[ is pressed then insert opposite key to RichTextBox1 at Position SelectionStart+1
  13. ' add one line after inserting, e.Handled=true;
  14. 'finally set SelectionStart to specified position
  15. Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
  16. Dim s As [String] = e.KeyChar.ToString()
  17. Dim sel As Integer = RichTextBox1.SelectionStart
  18. If checkBox1.Checked = True Then
  19. Select Case s
  20. Case "("
  21. RichTextBox1.Text = RichTextBox1.Text.Insert(sel, "()")
  22. e.Handled = True
  23. RichTextBox1.SelectionStart = sel + 1
  24. Exit Select
  25. Case "{"
  26. Dim t As [String] = "{}"
  27. RichTextBox1.Text = RichTextBox1.Text.Insert(sel, t)
  28. e.Handled = True
  29. RichTextBox1.SelectionStart = sel + t.Length - 1
  30. isCurslyBracesKeyPressed = True
  31. Exit Select
  32. Case "["
  33. RichTextBox1.Text = RichTextBox1.Text.Insert(sel, "[]")
  34. e.Handled = True
  35. RichTextBox1.SelectionStart = sel + 1
  36. Exit Select
  37. Case "<"
  38. RichTextBox1.Text = RichTextBox1.Text.Insert(sel, "<>")
  39. e.Handled = True
  40. RichTextBox1.SelectionStart = sel + 1
  41. Exit Select
  42. Case """"
  43. RichTextBox1.Text = RichTextBox1.Text.Insert(sel, """""")
  44. e.Handled = True
  45. RichTextBox1.SelectionStart = sel + 1
  46. Exit Select
  47. Case "'"
  48. RichTextBox1.Text = RichTextBox1.Text.Insert(sel, "''")
  49. e.Handled = True
  50. RichTextBox1.SelectionStart = sel + 1
  51. Exit Select
  52. End Select
  53. End If
  54. End Sub
  55. ' RichTextBox1 Key Down event
  56. '
  57. ' * when key { is pressed and {} is inserted in richTextBox
  58. ' * and isCurslyBracesKeyPressed is true then insert some blank text to RichTextBox1
  59. ' * when Enter key is down
  60. ' * it will look like this when Enter key is down
  61. '
  62. ' {
  63. ' |
  64. ' }
  65. '
  66. ' *
  67. Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
  68. Dim sel As Integer = RichTextBox1.SelectionStart
  69. If e.KeyCode = Keys.Enter Then
  70. If isCurslyBracesKeyPressed = True Then
  71. RichTextBox1.Text = RichTextBox1.Text.Insert(sel, vbLf & " " & vbLf)
  72. e.Handled = True
  73. RichTextBox1.SelectionStart = sel + " ".Length
  74. isCurslyBracesKeyPressed = False
  75. End If
  76. End If
  77. End Sub
  78. End Class