Create notepad using C# Part-5

0

notepad by Inder

In the part1part2 ,part3 and part4  articles of this series explain the design and code behind the working of some menu items of File and Edit menu item and C# code behind the next menu items. This is next part in our series in which we discuss some advance feature that are not present in the Microsoft Notepad. These advance features are very helpful for the web designer to check their HTML code in different browser by a single click. These function makes our notepad different from MS notepad. In this part we also discuss about a Process class In c# and also discuss that how we can send the written text to others using email. So let’s start with the next Menu item Run and discuss the C# code.

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

part-5_picture-1

In the Run menu item we have 6 further choices , that are Launch in IE, Launch in Chrome, Launch in Firefox, Google Search, Wikipedia search and send via . We discuss all these one by one.

But before start discussion, we have a little knowledge about the Process class in C# and some of its function.

Process Class: The class process lies in the System.Diagnostics  namespace  so you have use this namespace in your project to access this class. This class provide you to access local and global process and enables you to start and stop local system process. This class has many functions and properties out of which some important functions and properties are given bellow:

Function:

Start():This function available with 5 different overloads with different argument , this function start a process resource by passing the name of exe file of the process and the set command line argument these two parameters are necessary and in our project we use this function with these two arguments. The return type of this function is of process type so we hold the outcome of this function in a process type variable.

Kill():This function take no arguments and its return type Is void. The main task of this function is immediately stop all the process associated with the object.

There are some more functions are presents in the class but we here we are not going to discuss all in detail .

Properties:

MachineName: This property return the name of computer on which the process is run , the return type of this property is string.

ProcessName: This property is used to get the name of the process, only get property, we cant set the name at run time or compile time.

TotalProcessTime: This is also only get type property and using this we can get the total time of the process.

VirtualMemorySize: Using this process we get the total memory space allocated to the process.

Like thses many other useful properties are present in the process class , and we can check it in detail from MSDN.


 

Now come to the C# code and logic behind the menu item launch in IE,Chrome,Firefox.

To launch the file in the web browser the file must be saved with html extension some where in the local computer, so we make a function in which we check that either the file saved or not and if it is saved then the extension should be .html or .htm, then we move forward. So the code behind that function is given bellow:

private void launchinweb()
        {

            if (string.IsNullOrEmpty(fname))
            {
                DialogResult res;
                saveFileDialog1.Filter = "Html file|*.htm";
                res = saveFileDialog1.ShowDialog();
                if (res == DialogResult.OK)
                {
                    richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                    richTextBox1.Modified = false;
                }
                fname = saveFileDialog1.FileName;
                string name = Path.GetFileNameWithoutExtension(fname);

                this.Text = name + "-My Notepad";
                isfilesave = true;
            }
            else
            {
                richTextBox1.SaveFile(fname, RichTextBoxStreamType.PlainText);
                isfilesave = true;
            }
        }

 

Now we discuss the code behind each options to launch the file in browser:

  1. Launch In IE(ctrl+alt+shift+I): The code behind the handler of click event of this menu item is given bellow:
private void launchInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            launchinweb();// call the function defined above to check that file is saved or not.if not than open the save file dialog. 
            if (fname!="")
            {
                string link = Path.GetFullPath(fname);
                Process mypro;
                mypro = Process.Start("iexplore.exe", link);// the name of exe file for Internet Explorer is “iexplore.exe” you can check this using run option in window. 
            }

       }

 

2.Launch in Google Chrome(ctrl+alt+shift+R): The code for this same as  for IE but a little change in this we pass the name of exe file of chorme as given bellow:

private void launchInChromeToolStripMenuItem_Click(object sender, EventArgs e)
        {
             launchinweb();
            if (fname != "")
            {
                string link = Path.GetFullPath(fname);
                Process mypro;
                mypro = Process.Start("chrome.exe", link);
               
            }
        }

3.Launch in Firefox(ctrl+alt+shift+X): The name of firefox exe is “firefox.exe”

 

private void launchInChromeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            launchinweb();
            if (fname != "")
            {
                string link = Path.GetFullPath(fname);
                Process mypro;
                mypro = Process.Start("firefox.exe", link);
               
            }
     }

 

part-5_picture-2

part-5_picture-4

Because the above file is not saved so file save dialog box open and the save type is already defined as HTML file and after saving the file corresponding output is shown in the mozila firefox.

part-5_picture-3

Now we discuss the next two option, these two are not so important and but for a web designer and developer it may be helpful  because they always search their queries from google or wikkipedia (especially while making  college assignment )  so we provide a shortcut to open the google and Wikipedia directly from our notepad.

To perform this task we again use the process class as described bellow

4.Google Search(Alt+F1) : The code behind handler of click event of this tool strip menu item is give bellow:

private void googleSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {


            Process proc = new Process();
            proc.StartInfo.FileName = "http://www.google.com";
            proc.Start();

        }

5.Wikipedia Search: The code behind this is:

private void wikiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process proc = new Process();
            proc.StartInfo.FileName = "http://en.wikipedia.org/wiki/Main_Page";
            proc.Start();

        }

 

6.Sendvia:  The last option in this menu item sendvia  this option is useful when you want to send your document to some else via email , so this provide you list of choices from which you can choose the way to send the file .

After click on the send via button the pop ask you to choose the way to send the mail :

part-5_picture-5

The above window will open if you not choose default option to send mail by choosing the appropriate choice the text of our document is inserted in to the body of email and from their you can send email directly.

The C# code behind this is given bellow:

private void sendViaOutlookToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
         
            if (richTextBox1.Text.Length > 0)
            {
                Process.Start(string.Format(@"mailto:?body={0}", richTextBox1.Text,richTextBox1.Text));
            }
           
         

        }

With this we  complete our Run menu item and the C# code behind each option in item with end of this part we complete our 90% notepad and in the next section we discuss about the “About form” and the create set up of our notepad with that we complete this serise  .

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