I have given step-wise details to create your own Wordpad or Notepad. Read, Learn & Enjoy..!!
When you will open a new windows form application, on the screen you will see Form1.cs [Design] file in your visual studio. This file will show you a basic empty window. This will be the basic template or canvas on which you have to work. You can change its properties like resizing or open the application in Maximized mode by using the property tab on the right side of your screen. If you do not see the property tab on your screen then you can go to View option in the Menu Bar and select Properties Window from there or you can simply press F4 key.
Once you have changed the form properties, now you have to add controls to your form. To do that see the left side of your screen and click on Toolbox. Again if you do not find it then go to View>Toolbox or simply press Ctrl+Alt+X.
Now in Toolbox go to Menus & Toolbars. Click and Drag MenuStrip item from there to the top left of the windows form. This is your MenuBar and you can add MenuItems or SubItems to it by just clicking on the textboxes on the MenuStrip and writing the name of the Menu Item. After this go back to the Toolbox>Common Controls. Drag RichTextBox from there and place it just after your MenuBar. Change the properties of RichTextBox like Dock=Fill etc. accordingly by using Property Window.
After your have added all the MenuItems say File and SubItems say New, Open, Save, Save As, Close & Exit, just click on the File option of your MenuStrip and then double click on New. You will notice that a new tab will open with the name as Form1.cs. This is the file where you actually code or provide functionality to your MenuItems.
Copy and paste the code below to Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int flag_tc = 0;
string filesv = null;
string fileop = null;
public Form1()
{
InitializeComponent();
}
private void rtb1_TextChanged(object sender, EventArgs e)
{
flag_tc = 1;
}
private void NewToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag_tc == 1)
{
DialogResult dr = MessageBox.Show("Do you want to save the file?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
save();
rtb1.Clear();
rtb1.Visible = true;
}
else if (dr == DialogResult.No)
{
rtb1.Clear();
rtb1.Visible = true;
}
}
if (flag_tc == 0)
{
rtb1.Clear();
filesv = null;
fileop = null;
rtb1.Visible = true;
}
}
void save()
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Rich Text File|*.rtf;(*.*)|All Files|(*.*)";
if (sfd.ShowDialog() == DialogResult.OK)
{
rtb1.SaveFile(sfd.FileName);
filesv = sfd.FileName;
flag_tc = 0;
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag_tc == 1)
{
DialogResult dr = MessageBox.Show("Do you want to save the file?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
save();
open();
}
else if (dr == DialogResult.No)
{
open();
}
}
else
{
open();
}
}
void open()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Rich Text File|*.rtf;(*.*)|All Files|(*.*)";
if (ofd.ShowDialog() == DialogResult.OK)
{
rtb1.LoadFile(ofd.FileName);
fileop = ofd.FileName;
flag_tc = 0;
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fileop == null)
{
if (filesv == null)
{
save();
}
else
{
rtb1.SaveFile(filesv);
flag_tc = 0;
}
}
else
{
rtb1.SaveFile(fileop);
flag_tc = 0;
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
save();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag_tc == 1)
{
DialogResult dr = MessageBox.Show("Do you want to save the file?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
save();
filesv = null;
fileop = null;
rtb1.Visible = false;
}
else if (dr == DialogResult.No)
{
filesv = null;
fileop = null;
rtb1.Visible = false;
}
}
else
{
filesv = null;
fileop = null;
rtb1.Visible = false;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag_tc == 1)
{
DialogResult dr = MessageBox.Show("Do you want to save the file?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
save();
Application.Exit();
}
else if (dr == DialogResult.No)
{
Application.Exit();
}
}
else
{
Application.Exit();
}
}
}
}
Once you have pasted the above code in Form1.cs and if you are getting any errors(Red lines below any code line) then check the names of the MenuItems and RichTextBox. Also check that all the MenuItems have events associated to them which you have specified in Form1.cs. To do that make use of PropertyWindow.
After you have changed all the control names and associated all the events properly, you are good to go.
Compile your code and have fun with your very own WordPad. You can add many more features to your application. Just try them yourself and you will able to learn many new things.
Cheers..!!
https://techiesss.blogspot.in/
When you will open a new windows form application, on the screen you will see Form1.cs [Design] file in your visual studio. This file will show you a basic empty window. This will be the basic template or canvas on which you have to work. You can change its properties like resizing or open the application in Maximized mode by using the property tab on the right side of your screen. If you do not see the property tab on your screen then you can go to View option in the Menu Bar and select Properties Window from there or you can simply press F4 key.
Once you have changed the form properties, now you have to add controls to your form. To do that see the left side of your screen and click on Toolbox. Again if you do not find it then go to View>Toolbox or simply press Ctrl+Alt+X.
Now in Toolbox go to Menus & Toolbars. Click and Drag MenuStrip item from there to the top left of the windows form. This is your MenuBar and you can add MenuItems or SubItems to it by just clicking on the textboxes on the MenuStrip and writing the name of the Menu Item. After this go back to the Toolbox>Common Controls. Drag RichTextBox from there and place it just after your MenuBar. Change the properties of RichTextBox like Dock=Fill etc. accordingly by using Property Window.
After your have added all the MenuItems say File and SubItems say New, Open, Save, Save As, Close & Exit, just click on the File option of your MenuStrip and then double click on New. You will notice that a new tab will open with the name as Form1.cs. This is the file where you actually code or provide functionality to your MenuItems.
Copy and paste the code below to Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int flag_tc = 0;
string filesv = null;
string fileop = null;
public Form1()
{
InitializeComponent();
}
private void rtb1_TextChanged(object sender, EventArgs e)
{
flag_tc = 1;
}
private void NewToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag_tc == 1)
{
DialogResult dr = MessageBox.Show("Do you want to save the file?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
save();
rtb1.Clear();
rtb1.Visible = true;
}
else if (dr == DialogResult.No)
{
rtb1.Clear();
rtb1.Visible = true;
}
}
if (flag_tc == 0)
{
rtb1.Clear();
filesv = null;
fileop = null;
rtb1.Visible = true;
}
}
void save()
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Rich Text File|*.rtf;(*.*)|All Files|(*.*)";
if (sfd.ShowDialog() == DialogResult.OK)
{
rtb1.SaveFile(sfd.FileName);
filesv = sfd.FileName;
flag_tc = 0;
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag_tc == 1)
{
DialogResult dr = MessageBox.Show("Do you want to save the file?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
save();
open();
}
else if (dr == DialogResult.No)
{
open();
}
}
else
{
open();
}
}
void open()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Rich Text File|*.rtf;(*.*)|All Files|(*.*)";
if (ofd.ShowDialog() == DialogResult.OK)
{
rtb1.LoadFile(ofd.FileName);
fileop = ofd.FileName;
flag_tc = 0;
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fileop == null)
{
if (filesv == null)
{
save();
}
else
{
rtb1.SaveFile(filesv);
flag_tc = 0;
}
}
else
{
rtb1.SaveFile(fileop);
flag_tc = 0;
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
save();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag_tc == 1)
{
DialogResult dr = MessageBox.Show("Do you want to save the file?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
save();
filesv = null;
fileop = null;
rtb1.Visible = false;
}
else if (dr == DialogResult.No)
{
filesv = null;
fileop = null;
rtb1.Visible = false;
}
}
else
{
filesv = null;
fileop = null;
rtb1.Visible = false;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag_tc == 1)
{
DialogResult dr = MessageBox.Show("Do you want to save the file?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
save();
Application.Exit();
}
else if (dr == DialogResult.No)
{
Application.Exit();
}
}
else
{
Application.Exit();
}
}
}
}
Once you have pasted the above code in Form1.cs and if you are getting any errors(Red lines below any code line) then check the names of the MenuItems and RichTextBox. Also check that all the MenuItems have events associated to them which you have specified in Form1.cs. To do that make use of PropertyWindow.
After you have changed all the control names and associated all the events properly, you are good to go.
Compile your code and have fun with your very own WordPad. You can add many more features to your application. Just try them yourself and you will able to learn many new things.
Cheers..!!
https://techiesss.blogspot.in/
No comments:
Post a Comment