Creating A Text Editor In C# And Visual Basic (Part B Coding)
[ Continued from the previous page ]
TEXT FUNCTIONS
In this section we will create the Cut, Copy and Paste methods for the application. Below the Save&Open region and another region and these methods. The code for this is quite self-explanatory:
private void Undo() { GetCurrentDocument.Undo(); } private void Redo() { GetCurrentDocument.Redo(); } private void Cut() { GetCurrentDocument.Cut(); } private void Copy() { GetCurrentDocument.Copy(); } private void Paste() { GetCurrentDocument.Paste(); } private void SelectAll() { GetCurrentDocument.SelectAll(); }
Private Sub Undo() GetCurrentDocument.Undo() End Sub Private Sub Redo() GetCurrentDocument.Redo() End Sub Private Sub Cut() GetCurrentDocument.Cut() End Sub Private Sub Copy() GetCurrentDocument.Copy() End Sub Private Sub Paste() GetCurrentDocument.Paste() End Sub Private Sub SelectAll() GetCurrentDocument.SelectAll() End Sub
Under the previous region add another one called General and add the following two methods.
GENERAL METHODS
private void GetFontCollection() { InstalledFontCollection InsFonts = new InstalledFontCollection(); foreach (FontFamily item in InsFonts.Families) { toolStripComboBox1.Items.Add(item.Name); } toolStripComboBox1.SelectedIndex = 0; } private void PopulateFontSizes() { for (int i = 1; i <= 75; i++) { toolStripComboBox2.Items.Add(i); } toolStripComboBox2.SelectedIndex = 11; }
Private Sub GetFontCollection() Dim InsFont As New InstalledFontCollection() For Each item As FontFamily In InsFont.Families ToolStripComboBox1.Items.Add(item.Name) Next ToolStripComboBox1.SelectedIndex = 0 End Sub Private Sub PopulateFontSizes() For i = 1 To 75 ToolStripComboBox2.Items.Add(i) Next ToolStripComboBox2.SelectedIndex = 11 End Sub
The first method gets the fonts installed on the system and adds them to the comboBox. If you get an error saying ‘InstalledFontCollection does not exist’, right click on it and click Resolve, and then select System.Drawing.Text.
BINDING METHODS TO EVENTS
Now that we have created methods we need to bind them to the events. We will demonstrate one or two and allow you to do the rest. In design view click Edit and double click Undo.
private void undoToolStripMenuItem_Click(object sender, EventArgs e) { Undo(); }
Private Sub UndoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UndoToolStripMenuItem.Click Undo() End Sub
All we do is call the method, do this for the rest. So you’ll be calling the method a couple of times, for example Save option is available through the File menu, left tool strip and context menu strip.
TOP TOOLSTRIP
There are no methods for the top toolstrip so we’ll directly add the code to the events of each button. Double click the Bold button in design view, and add this code to its oneclick event handler.
Font BoldFont = new Font(GetCurrentDocument.SelectionFont.FontFamily, GetCurrentDocument.SelectionFont.SizeInPoints, FontStyle.Bold); Font RegularFont = new Font(GetCurrentDocument.SelectionFont.FontFamily, GetCurrentDocument.SelectionFont.SizeInPoints, FontStyle.Regular); if (GetCurrentDocument.SelectionFont.Bold) { GetCurrentDocument.SelectionFont = RegularFont; } else { GetCurrentDocument.SelectionFont = BoldFont; }
Dim BoldFont As New Font(GetCurrentDocument.SelectionFont.Name, GetCurrentDocument.SelectionFont.Size, FontStyle.Bold) Dim RegularFont As New Font(GetCurrentDocument.SelectionFont.Name, GetCurrentDocument.SelectionFont.Size, FontStyle.Regular) If GetCurrentDocument.SelectionFont.Bold Then GetCurrentDocument.SelectionFont = RegularFont Else GetCurrentDocument.SelectionFont = BoldFont End If
Here we simply check the font style applied to the highlighted text and change it depending on the style. You can do this for Italic, underline and strikeout, you just need to change the variable name, 3rd argument and If Statement condition.
Upper Case & Lower Case
private void toolStripButton5_Click(object sender, EventArgs e) { GetCurrentDocument.SelectedText = GetCurrentDocument.SelectedText.ToUpper(); } private void toolStripButton6_Click(object sender, EventArgs e) { GetCurrentDocument.SelectedText = GetCurrentDocument.SelectedText.ToLower(); }
Private Sub ToolStripButton5_Click(sender As Object, e As EventArgs) Handles ToolStripButton5.Click GetCurrentDocument.SelectedText = GetCurrentDocument.SelectedText.ToUpper() End Sub Private Sub ToolStripButton6_Click(sender As Object, e As EventArgs) Handles ToolStripButton6.Click GetCurrentDocument.SelectedText = GetCurrentDocument.SelectedText.ToLower() End Sub
This code is rather self-explanatory.
Font Increase and Decrease
private void toolStripButton7_Click(object sender, EventArgs e) { float NewFontSize = GetCurrentDocument.SelectionFont.SizeInPoints + 2; Font NewSize = new Font(GetCurrentDocument.SelectionFont.Name, NewFontSize, GetCurrentDocument.SelectionFont.Style); GetCurrentDocument.SelectionFont = NewSize; } private void toolStripButton8_Click(object sender, EventArgs e) { float NewFontSize = GetCurrentDocument.SelectionFont.SizeInPoints - 2; Font NewSize = new Font(GetCurrentDocument.SelectionFont.Name, NewFontSize, GetCurrentDocument.SelectionFont.Style); GetCurrentDocument.SelectionFont = NewSize; }
Private Sub ToolStripButton7_Click(sender As Object, e As EventArgs) Handles ToolStripButton7.Click Dim NewFontSize As Single = GetCurrentDocument.SelectionFont.Size + 2 Dim NewFont As New Font(GetCurrentDocument.SelectionFont.Name, NewFontSize, GetCurrentDocument.SelectionFont.Style) GetCurrentDocument.SelectionFont = NewFont End Sub Private Sub ToolStripButton8_Click(sender As Object, e As EventArgs) Handles ToolStripButton8.Click Dim NewFontSize As Single = GetCurrentDocument.SelectionFont.Size - 2 Dim NewFont As New Font(GetCurrentDocument.SelectionFont.Name, NewFontSize, GetCurrentDocument.SelectionFont.Style) GetCurrentDocument.SelectionFont = NewFont End Sub
For the font increase we simply get the current font size of the selected text and add 2 to it. For the initialize a new font class and for the 2nd argument we supply the NewFontSize variable. For C# it is data type float and for VB it’s data type single. We do the same for font decrease but in reverse.
Font Forecolor and Highlighted Color
private void toolStripButton9_Click(object sender, EventArgs e) { if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { GetCurrentDocument.SelectionColor = colorDialog1.Color; } } private void HighlighGreen_Click(object sender, EventArgs e) { GetCurrentDocument.SelectionBackColor = Color.LightGreen; }
If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then GetCurrentDocument.SelectionColor = ColorDialog1.Color End If Private Sub HighlightGreen_Click(sender As Object, e As EventArgs) Handles HighlightGreen.Click GetCurrentDocument.SelectionBackColor = Color.LightGreen End Sub
For the font forecolor we simply show the colordialog, and when the users click OK we get their chosen colour and apply it to the highlighted text.
For the text background colour, we simply get the SelectionBackColor to the chosen colour from the drop down menu. For example if it is green we set SelectionBackColor to Color.LightGreen (don’t make it too dark). The same can be done for orange and yellow.
ComboBoxes FontFamily and FontSize
These must be added to the ComboBox SelectedIndexChanged Event.
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) { Font NewFont = new Font(toolStripComboBox1.SelectedItem.ToString(), GetCurrentDocument.SelectionFont.Size, GetCurrentDocument.SelectionFont.Style); GetCurrentDocument.SelectionFont = NewFont; } private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e) { float NewSize; float.TryParse(toolStripComboBox2.SelectedItem.ToString(), out NewSize); Font NewFont = new Font(GetCurrentDocument.SelectionFont.Name, NewSize, GetCurrentDocument.SelectionFont.Style); GetCurrentDocument.SelectionFont = NewFont; }
Private Sub ToolStripComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged Dim NewFont As New Font(ToolStripComboBox1.SelectedItem.ToString(), _ GetCurrentDocument.SelectionFont.Size, GetCurrentDocument.SelectionFont.Style) GetCurrentDocument.SelectionFont = NewFont End Sub Private Sub ToolStripComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ToolStripComboBox2.SelectedIndexChanged Dim NewSize As Single = ToolStripComboBox2.SelectedItem Dim NewFont As New Font(GetCurrentDocument.SelectionFont.Name, NewSize, GetCurrentDocument.SelectionFont.Style) GetCurrentDocument.SelectionFont = NewFont End Sub
For the font family when we initialize the font class, we change the first argument to the SelectedItem in the first comboBox. The same applies to font size but it’s the second argument we change, and we also convert to value to the correct data type.
Timer
Double click the timer in design view and put this code; it will simply count the amount of characters in the current document.
if (GetCurrentDocument.Text.Length > 0) { toolStripStatusLabel1.Text = GetCurrentDocument.Text.Length.ToString(); }
If GetCurrentDocument.Text.Length > 0 Then ToolStripStatusLabel1.Text = GetCurrentDocument.Text.Length End If
Form Load
For the form load event you need to add the following methods (in order).
AddTab(); GetFontCollection(); PopulateFontSizes();
AddTab() GetFontCollection() PopulateFontSizes()
Summary
That was a lot to absorb, and hopefully this tutorial gave you some good insight on creating windows applications. The .NetFramework has a lot more to offer so it is recommended you spend some time exploring it.
Hopefully this tutorial has also given you some pointers on code organisation and structuring your work. If you enjoyed it, please give us a like!
[Download Project Files – Visual Basic – C#]
Philemon Frimpong
December 15, 2015 @ 1:08 pm
hello coding guys,
I
Thanks a lot for the step by step tutorial.
I’ve really enjoyed and still enjoying it.
Just want to give you guys a Hi5 for your
good work.
I’m now into visual basic and mysql, so still following…..
Ben
June 2, 2016 @ 11:19 pm
Great work!
Martynas
November 16, 2016 @ 10:25 am
A few simple improvments could be made here:
1. Font increase and decrease buttons should be dynamic towards font size combo box.
2. Error-checking should be done in both buttons, because trying to decrease size of 1, or increase of 75 throws an exception.