Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSharp.NET Tutorial – Day 17

Tags: menu
In previous article, we have discussed what are Default Events, what are default events for controls, what is form, what is Button, what is Label, what is TextBox, what are different kind of properties  of TextBox, how to set TextArea, how to set password, how set multiline, how to validate the controls, how to set Accept Button and Control Button of Form, how to Create a Menu to Windows Form, how to set Alt keys in Menu, how to set shortcut keys in Menu, how to set checkmarks in Menu, what are Dialog Controls, what is Color Dialog, what is Font Dialog, what is Folder Browser Dialog, what is Open File Dialog, what is Save File Dialog etc, what is Dock Property, what is RichTextBox along with syntaxes and examples, etc

Please find below the link for accessing the article

CSharp.NET Tutorial - Day 16

Now, in this article we will discuss How to Create a Notepad application in Visual Studio, how to use Menustrip control, how to add menu items to Menustrip control, how to create dialog controls and how to use them, how to create About Box control and how to use it, how to set different kind of actions in Menu strip control etc along with syntaxes and examples.

How to Create a Notepad application in Visual Studio

Now, we will learn how to create a Notepad application in Visual Studio easily and quickly.  In this article, we are going to use Menustrip control, adding menu items to Menustrip control, dialog controls, etc.


Please follow below steps to create a Notepad application.

Step 1:  Take a new Windows Form and add a 'Menu Strip' control to it.

Step 2:  For Form set its Text as "Untitled - My Notepad"

Step 3:  On the Menu strip add 4 Menus

·         File
·         Edit
·         Format
·         Help

Step 4:  Under 'File' Menu add the following Menu items

·         New
·         Open
·         Save
·         Save As
·         Close

Step 5:  Under the 'Edit' Menu add the following items

·         Cut
·         Copy
·         Paste

Step 6:  Under 'Format' Menu add the following items

·         Word Warp (Checked)
·         Color

Step 7:  Under 'Help' Menu add the Menu item

·         Calculator
·         About My Notepad

Step 8:  Place a 'RichTextBox' control and set its 'Dock' Property as 'Fill' and also change the name of it as "rtbData".

Step 9:  Add a 'OpenFileDialog', 'SaveFileDialog', 'ColorDialog', and 'FontDialog' to the form.

Step 10:  Now write the below code.

Using System.Diagnostics;

Declarations:
bool close = true;

private void SaveNewFile()
{
if(rtbData.Text.Trim().Length > 0)
{
DialogResult dr=MessageBox.Show("The text in the Untitled file has changed.\
n\Do you want to save the Changes?", "My Notepad",
MessageBoxButtons.YesNoCancel,MessageBoxIcon.Information);

if(dr==DialogResult.Yes)
{
DialogResult d=SaveFileDialog1.ShowDialog();
if(d!=DialogResult.Cancel)
{
string fpath=SaveFileDialog1.FileName;
rtbData.SaveFile(fpath,RichTextBoxStreamType.PlainText);
rtbData.Clear();
}
}

else if(dr==Dialog.Result.No)
rtbData.Clear();

else if(dr==DialogResult.Cancel)
Close=false;
}
}

Under 'New' Menu item:
SaveNewFile();

Under 'Open' Menu Item:
SaveNewFile();
OpenFileDialog1.ShowDialog();
string fpath=OpenFileDialog1.FileName;
rtbData.LoadFile(fpath,RichTextBoxStreamType.PlainText);
this.Text=fpath;

private void SaveFile()
{
SaveFileDialog1.ShowDialog();
string fpath=SaveFileDialog1.FileName;
rtbData.SaveFile(fpath,RichTextBoxStreamType.PlainText);
this.Text=fpath;
}

Under 'Save' Menu Item:
if(this.Text=="Untitled-MyNotepad")
SaveFile();
else
rtbData.SaveFile(this.Text,RichTextBoxStreamType.PlainText);

Under 'SaveAs' Menu Item:
SaveFile();

Under 'Close' Menu Item:
SaveNewFile();
if(Close==true)
this.Close();
close=true;

Under 'Cut' Menu Item:
rtbData.Cut();

Under 'Copy' Menu Item:
rtbData.Copy();

Under 'Paste' Menu Item:
rtbData.Paste();

Under 'WordWrap' Menu Item:
if(WordWrapToolStringMenuItem.Checked==true)
{
WordWrapToolStringMenuItem.Checked==false;
rtbData.WordWrap=false;
}
else
{
WordWrapToolStringMenuItem.Checked=true;
rtbData.WordWrap=true;
}

Under 'Font' Menu Item:
FontDialog1.ShowDialog();
rtbData.Font=fontDialog1.Font;

Under 'Color' Menu Item:
colorDialog1.ShowDialog();
rtbData.ForeColor=colorDialog1.color;

Under 'Calculator' Menu Item:
Process. Start ("calc.exe");

Now, open the Add New Item window and add a New Item choosing the option as "About Box" which will add a New Form as "AboutBox1.cs".  By default the Form designed to be used as an About Box so make required changes and then write the below code under "About MyNotepad" Menu Item:

AboutBox1 f=new AboutBox1 ();
f.ShowDialog ();


Now we done with sample Notepad application which is same as Notepad application there in Windows Operating System.  We can easily develop this kind of applications with ease and quick with Visual Studio.

Happy Learning….!!!!!


This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here

Share the post

CSharp.NET Tutorial – Day 17

×

Subscribe to Dot Net Programming (c#, Asp.net, Ado.net, Wcf, Wpf, Ajax, Linq)

Get updates delivered right to your inbox!

Thank you for your subscription

×