آموزش ساخت برنامه ی نوت پد در #C
عناوین و مباحثی که در این مطلب آموزشی ، به آن پرداخته می شود:
- ایجاد پروژه جدید (new)
- باز کردن فایل متنی
- ذخیره فایل متنی
- تغییر فونت متن
- عوض کردن رنگ متن
- پرینت گرفتن از متن
- افزودن امکاناتی مااند cut و copy و paste و SelectAll
خب ابتدا کنترل های زیر را به فرم برنامه اضافه کنید تا فرمتان شبیه تصویر بالا شود:
OpenFileDialog
PrintDialog
ColorDialog
SaveFileDialog
FontDialog
MenuStrip
PrintDialog
ColorDialog
SaveFileDialog
FontDialog
MenuStrip
برای اطلاعات بیش تر درباره ی این کنترل ها مطالب OpenFileDialog و … و …. را مشاهده کنید.
سپس کدهای کلی فرم به صورت زیر می شود:(ابتدا تک تک رویداد ها را از قسمت Event ها در منوی Properties ایجاد کنید و سپس کد مربوط به هر رویداد را که در این کدها نوشته شده کپی کنید ):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace NotepadApplicationProgram98
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.Text = "";
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
//Now it show open dialog box.
//Read the filename selected by the user with in open file dialog box.
string fName = openFileDialog1.FileName;
//Read the data by using StreamReader class
StreamReader sr = new StreamReader(fName);
txtContent.Text = sr.ReadToEnd();
sr.Close();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
string fName = saveFileDialog1.FileName;
StreamWriter sw = new StreamWriter(fName);
sw.Write(txtContent.Text);
sw.Flush();
sw.Close();
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDialog1.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.Cut();
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.Copy();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.Paste();
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.SelectAll();
}
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
txtContent.Font = fontDialog1.Font;
}
private void colorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
txtContent.ForeColor = colorDialog1.Color;
}
private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
{
//create a form dynamically which displays the information about the company.
Form f = new Form();
Label ll = new Label();
ll.Text = "Write information about the company";
ll.Font = new Font("Arial", 20);
ll.Dock = DockStyle.Fill;
f.Controls.Add(ll);
f.Show();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace NotepadApplicationProgram98
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.Text = "";
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
//Now it show open dialog box.
//Read the filename selected by the user with in open file dialog box.
string fName = openFileDialog1.FileName;
//Read the data by using StreamReader class
StreamReader sr = new StreamReader(fName);
txtContent.Text = sr.ReadToEnd();
sr.Close();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
string fName = saveFileDialog1.FileName;
StreamWriter sw = new StreamWriter(fName);
sw.Write(txtContent.Text);
sw.Flush();
sw.Close();
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDialog1.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.Cut();
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.Copy();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.Paste();
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.SelectAll();
}
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
txtContent.Font = fontDialog1.Font;
}
private void colorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
txtContent.ForeColor = colorDialog1.Color;
}
private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
{
//create a form dynamically which displays the information about the company.
Form f = new Form();
Label ll = new Label();
ll.Text = "Write information about the company";
ll.Font = new Font("Arial", 20);
ll.Dock = DockStyle.Fill;
f.Controls.Add(ll);
f.Show();
}
}
}
به طور مثال newToolStripMenuItem_Click را می توانید با دابل کلیک کردن روی گزینه ی new که خودتان باید به صورت دستی قبلا ایجاد میکردید ، را رویدادش را درست کنید که به طور مثال کد زیر برای رویداد کلیک روی ToolStripMenuItem می باشد:
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
txtContent.Text = "";
}
{
txtContent.Text = "";
}