Date 9-2-11 Wednesday
Dock Property – Using Dock Property we can define which borders of the control are bound to the container. It can be set with different values like - Left, Right, Top, Bottom and Fill.
Rich TextBox – This control is similar to a textbox but provides all the built in functionalities for Cut, Copy, Past, Save, Open etc.
Procedure of Designing a Notepad in VS:
Open a new form set as title as untitled and place a menu strip control on it.
Add four menus init --> File, Edit, Format, and Help.
Under File menu add the menu items – new, open, save, save as, close.
Under edit menu add the menu items – cut, copy, past, undo and redo.
Under Format menu add – word wrap, fount, color and make word wrap as checked.
Under help add – calculator and about My Notepad.
- Now place a rich TextBox control on the form and set its dock property as fill and change the name as “rtbText”.
- Place an OpenFileDialog, SaveFileDialog, ColorDialog and FontDialog control in form.
using System.Diagnostics; |
class Declaretion string fname; private void CheckForSave() { if (rtbText.Text.Trim().Length > 0) { DialogResult dr = MessageBox.Show("Do you wish to save the constant?", "my notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); if (dr == DialogResult.Yes) { saveFileDialog1.ShowDialog(); fname = saveFileDialog1.FileName; rtbText.SaveFile(fname, RichTextBoxStreamType.PlainText); rtbText.Clear(); } else if (dr == DialogResult.No) rtbText.Clear(); } } |
Under New Menu item: CheckForSave(); |
Under Open Menu item: { CheckForSave(); openFileDialog1.ShowDialog(); fname = openFileDialog1.FileName; rtbText.LoadFile(fname,RichTextBoxStreamType.PlainText); this.Text = fname; } |
Under Save menu Item: if (this.Text == "Untitled") { saveFileDialog1.ShowDialog(); fname = saveFileDialog1.FileName; rtbText.SaveFile(fname, RichTextBoxStreamType.PlainText); rtbText.Clear(); } else rtbText.SaveFile(fname, RichTextBoxStreamType.PlainText); |
Under SaveAs menu item: saveFileDialog1.ShowDialog(); fname = saveFileDialog1.FileName; rtbText.SaveFile(fname, RichTextBoxStreamType.PlainText); this.Text = fname; |
Under Close menu item: CheckForSave(); this.Close(); |
Under Cut menu item: rtbText.Cut(); Under Copy menu item: rtbText.Copy(); Under Paste menu item: rtbText.Paste(); Under Undo menu item: rtbText.Undo(); Under Redo menu item: rtbText.Redo(); |
Under Word Wrap menu item: if (wordWrapToolStripMenuItem.Checked == true) { wordWrapToolStripMenuItem.Checked = false; rtbText.WordWrap = false; } else wordWrapToolStripMenuItem.Checked = true; rtbText.WordWrap = true; |
Under Font menu item: fontDialog1.ShowDialog(); rtbText.Font = fontDialog1.Font; Under Color menu item: colorDialog1.ShowDialog(); rtbText.ForeColor = colorDialog1.Color; |
Under Calculator menu item: Process.Start("calc.exe"); Under About Menu item: Form1 f = new Form1(); f.ShowDialog(); |
0 comments:
Post a Comment