Create notepad using C# Part-4

0

In the part1, part2 and part3 articles of this series explain the design and code behind the working of some menu items of File and Edit menu item. Now moving forward in this article we discuss about the C# code behind the next menu items. We discuss about the Format and View Menu in this series.

Part 1, Part 2, Part 3, Part 4, Part 5 , Part 6 of the article.

Items in Format Menupart-4_picture-1

Items In View Menu

part-4_picture-2

Format Menu :In Format menu the items are Word wrap, Font, color which are generally used to forma the text, so we discuss each item separately .

1. Word wrap :Line wrap is feature of continuing new line when a line is full , such that each line fits into a viewable window, allows text to be read without horizontal scrolling .Word wrap is additional feature provide by some text editor , web browser and word processor of breaking line between and not within the words, except when a single word is longer than a line.

So to provide this feature in our notepad the following code is written in the handler of format menu item click event.

private void wordWrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.WordWrap = !richTextBox1.WordWrap;// Word wrap is a property of rich textbox which return true and false. 
            statusBarToolStripMenuItem.Enabled = !richTextBox1.WordWrap;
            if(richTextBox1.WordWrap==true)
            {
                wordWrapToolStripMenuItem.Checked = true;
                statusBarToolStripMenuItem.Checked = false;
                statusStrip1.Visible = false;
            }
            else
            {
                statusBarToolStripMenuItem.Checked = true;
                statusStrip1.Visible = true;
                wordWrapToolStripMenuItem.Checked = false;
            }
        }

 

By using the property word wrap of rich textbox we can enable and disable the some other items like statusbartool strip menu item and status strip menu item. The

status bar menu item is disabled when the word wrap feature is enabled.

2. Font: The next item in the format menu is font selection, the Visual studio provide us a build in control FontDialog for the selection of the fonts.

FontDialog:

part-4_picture-3

We use this control directly in our project. To use this control drag and drop it from the tool window to your design window, like other dialog control this control also resides at the bottom of the main form. To invoke it at the button click make the object of it and hold the reference in the DialogResult type variable as given in following code segment

private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ft;
            ft = fontDialog1.ShowDialog();
            if (ft == DialogResult.OK)// If we click on the ok button oh font dialog 
            {
                richTextBox1.SelectionFont = fontDialog1.Font;// set the font of rich textbox text same as selected in the Fnt dialog. 
            }
        } 

 

3. Color: The next item in the formatting of text is the color of the text, The color dialog box contain some basic colours and provide feature

for the custom colours, Microsoft Visual studio provide the build in control for color dialog box also , this control is named as colordialog in the toolbox and can be

used by simply drag and drop, the dialog box look like as :

part-4_picture-4

The method to invoke this control is same as Font dialog , the code for this is shown as :

      private void colorToolStripMenuItem_Click(object sender, EventArgs e)
        { 
            DialogResult cl;

            cl = colorDialog1.ShowDialog();
            if(cl==DialogResult.OK)
            {
                richTextBox1.SelectionColor = colorDialog1.Color;
            }


  }

 

1. Status Bar:The status bar is small horizontal strip generally placed at the bottom of the notepad and shows line no and column number , and a

progress bar when we delete some item and paste some item. The status strip is enable only when the word wrap feature is disabled. The status bar shown as :
part-4_picture-5

The code behind the handler of the click event is given as:

private void statusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!wordWrapToolStripMenuItem.Checked)
            {
                statusBarToolStripMenuItem.Checked = !statusBarToolStripMenuItem.Checked;
                if (statusBarToolStripMenuItem.Checked)
                {

                    statusStrip1.Visible = true;
                    
                }
                else
                {
                    
                    statusStrip1.Visible = false;
                                    }
            }
            
            
        }

This code is only for hide and display the status bar. Now to show the line number and column number we insert a label in the status strip, and progress bar to show the paste and delete action, as shown :

part-4_picture-6

 

Now to represent a line and column number following code have to write in the handler of the cursor change event of the rich text box (to make the handler of cursor change event select the richtext box and from property window find the event cursor change and double click on it , the visual studio automatically form a handler for it )

private void richTextBox1_CursorChanged(object sender, EventArgs e)
        {


            line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
            column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
            
            StatusLabel3.Text = string.Format("ln {0} cl {1}", line, column);
           
        }

The code behind the working of progress bar will be discussed in the next article with some other also define the timer control and its event.

2. Zoom: The next option in in the view menu is zoom, which further have three options Zoom In, Zoom Out, Reset to default zoom. For this a very simple logic is used. In the Zoom In case we add the some specific value in the font size of the text on the click on this Item , and in Zoom Out the specific value is subtracted. The code for each is given as :

Zoom In:

float m;
private void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (m < 73)
            {
                m = richTextBox1.Font.Size;
                richTextBox1.Font = new Font(richTextBox1.Font.Name, m + 3);// add 3 on every click until the font size is less than 73 (you can use any value for 

increment and limit ) 
            }
            else if(m<=70)
            {
                zoomInToolStripMenuItem.Enabled = false;// when the 
            }
        }

 

Zoom Out:

 private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (m > 7)
            {
                m = richTextBox1.Font.Size;
                richTextBox1.Font = new Font(richTextBox1.Font.Name, m - 3);
            }
            else if(m>=8)
            {
                zoomOutToolStripMenuItem.Enabled = false;
            }
        }

Reset to default :

         private void resetDefaultZoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Font = new Font(richTextBox1.Font.Name, 13);
        }

 

Here Font is class and its constructor takes 2 parameters, font name and font size .

With this we complete our View menu item. In the next articles we will discuss Run Menu item (not present in the Microsoft Notepad but useful for a web developer), we will also discuss the process class in C# and open the web browser and send mail form our notepad. So next articles will be more interesting and informative.

Please give your valuable feedback and suggestion.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge