CS-404 Visual Programming
Lecture 11 (Adding Menus and Menu Items)
Course Teacher: Awais Mehmood, Department of Computer Science, UET, Taxila (Pakistan)
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 1
Goals for today
Adding
Panel Cut-Paste Copy-Paste
menu
Set clipboard Open File
Undo
text Dialogue Box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 2
Adding menus to Windows Form in C#
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 3
Adding menus to Windows Form in C#
4
Adding menus to
Windows Form in C#
Adding menus to Windows Form in C#
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 6
Adding menus to Windows Form in C#
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 7
The Edit Menu
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 8
The Edit Menu: Cut
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 9
The Edit Menu: Cut
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 10
The Edit Menu: Cut
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 11
The Edit Menu:
Cut
12
The Edit Menu: Undo
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 13
The Edit Menu: Undo
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 14
The Edit Menu: Copy
15
The Edit Menu: Copy
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 16
The Edit Menu: Copy
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 17
The Edit
Menu: Copy
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 18
The Edit Menu: Paste
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 19
The Edit Menu: Paste
20
The Edit Menu: Paste
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 21
The Edit Menu: Paste
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 22
The Edit Menu: Paste
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 23
Set Clipboard text
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 24
Open File Dialogue Box in C#
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 25
Open File Dialogue Box in C#
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 26
Open File dialogue box in C#
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 27
Open File dialogue box in C#
28
Open File dialogue box in C#
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 29
Open File dialogue box in C#
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 30
Open File Dialogue Box on C#
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 31
Inserting an image in Dialogue box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 32
Inserting an image in Dialogue box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 33
Inserting an image in Dialogue box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 34
Inserting an image in Dialogue box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 35
Inserting an image in Dialogue box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 36
Inserting an image in Dialogue box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 37
Inserting an image in Dialogue box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 38
Inserting an image in
Dialogue box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 39
ـرك ُٓـوا ا َ ْن يَّقُ ْولُ ٓوا ما ََنَّا ََ ُُـ ْْ ََ يُ ْْتَنُ ْـو َن ()2 ب النَّ ُ
اس ا َ ْن يُّتْ َ ا َ َح ِ
س َ
کیا لوگ خیال کرتے ہیں يہ کہنے سے کہ ہْ ايمان َئے ہیں چھوڑ ديے
جائیں گے اَر ان کی آزَائش نہیں کی جائے گی۔
سورۃ العنکبوت ()2
)DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN 40
Assignment 2
▪ Create an application named Lecture09_10 using all the code provided in the
said lecture slides.
▪ Create another application named Lecture11 using all the code provided in
lecture slides (cut, copy, paste, undo, quit, OpenFileDialogue, Image, etc.) .
▪ Add another menu named ‘Process’ to the above application which
implements the following sub menus:
▪Batch Copy: to concatenate the values of textbox1 and textbox 2 and copy them
into clipboard.
▪Display: to display the copied values as a message in a message box.
41
CS-404 Visual Programming
Lecture 12 (RichText, Open & Save Dialog)
Course Teacher: Awais Mehmood, Department of Computer Science, UET, Taxila (Pakistan)
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 42
Goals for today
Rich Textbox Open file dialogue Save file dialogue box
box
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 43
RichTextBox
▪ A textbox which gives you rich text editing controls and advanced
formatting features
▪ Includes loading rich text format (RTF) files.
▪ Allows you to display or edit flow content, including paragraphs, images,
tables, etc.
▪ Mainly used to display more than one line input.
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 44
Textbox vs Rich Textbox
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 45
Adding RichTextBox
(Design Time)
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 46
Adding RichTextBox (Run Time)
▪ It is a little bit trickier than the above method.
▪ In this method, you can create a RichTextBox control programmatically with
the help of syntax provided by the RichTextBox class.
Step 1: Create a RichTextBox control using the RichTextBox() constructor
provided by the RichTextBox class.
// Creating a RichTextBox control
RichTextBox box = new RichTextBox();
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 47
•Step 2: After creating a RichTextBox control, set the property of the
RichTextBox control provided by the RichTextBox class.
// Setting the location of the RichTextBox
box.Location = new Point(236, 97);
// Setting the background color of the RichTextBox
box.BackColor = Color.Aqua;
// Setting the text in the RichTextBox
box.Text = "!..Welcome..!";
48
Step 3: Finally, add this RichTextBox control to the form using the
following statement:
// Adding this RichTextBox /
/ in the form
this.Controls.Add(box);
49
Example
using System; namespace WindowsFormsApp30 {
using System.Collections.Generic; public partial class Form1 :
using System.ComponentModel; Form {
using System.Data; public Form1()
using System.Drawing; {
using System.Linq;
using System.Text; InitializeComponent();
using System.Threading.Tasks; }
using System.Windows.Forms;
50
Example
private void Form1_Load(object sender, EventArgs e)
{
// Creating and setting the properties of the label
Label lb = new Label();
lb.Location = new Point(251, 70);
lb.Text = "Enter Text";
// Adding this label in the form
this.Controls.Add(lb);
51
Example
// Creating and setting the properties of the RichTextBox
RichTextBox box = new RichTextBox();
box.Location = new Point(236, 97);
box.BackColor = Color.Aqua;
box.Text = “This is a sample rich text. ";
// Adding this RichTextBox in the form
this.Controls.Add(box);
}
52
Constructor
53
Properties of Rich textbox
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 54
Properties of Rich textbox
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 55
Properties of Rich textbox
56
Loading File in RichTextBox
57
Insert Text File Data
if
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 58
Saving to Text File
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 59
SaveFileDialogue
▪ Allows users to launch Windows Save File
Dialog and let them save files.
▪ A typical SaveFileDialog looks like the figure
where you can see the Windows Explorer
type features to navigate through folders
and save a file in a folder.
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 60
Save File Dialogue Control-Design Time
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 61
Save File Dialogue Control
62
Save File Dialogue Control
63
Save Rich Text Data to Text File
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 64
Save Rich Text Data to Text File
if
65
Save File Dialogue-Run time
▪ The first step to create a dynamic SaveFileDialog is to create an instance
of SaveFileDialog class.
▪ The following code snippet creates a SaveFileDialog control object:
SaveFileDialog SaveFD = new SaveFileDialog();
SaveFD.ShowDialog();
▪ Once the ShowDialog method is called, you can browse and select a file.
DEPARTMENT OF COMPUTER SCIENCE, UET TAXILA (PAKISTAN) 66
Setting properties at run time
▪ Initial and Restore Directories
InitialDirectory property represents the directory to be displayed when the
open file dialog appears the first time.
SaveFD.InitialDirectory = @"C:\";
▪ If RestoreDirectory property is set to true, that means the open file dialog box
restores the current directory before closing.
SaveFD.RestoreDirectory = true;
67
Setting properties at run time (2)
▪ Title
Title property is used to set or get the title of the open file dialog.
SaveFD.Title = "Browse Text Files";
▪ Default Extension
DefaultExt property represents the default file name extension.
SaveFD.DefaultExt = "txt";
68
Setting properties at run time (3)
▪ Filter and Filter Index
Filter property represents the filter on a file dialog that is used to filter the
type of files to be loaded during the browse option in an open file dialog.
For example, if you need users to restrict to image files only, we can set
Filter property to load image files only.
SaveFD.Filter = "txt files (*.txt)|*.txt|All
files (*.*)|*.*";
▪ FilterIndex property represents the index of the filter currently selected in the
file dialog box.
SaveFD.FilterIndex = 2;
69
Setting properties at run time (4)
▪ Check File Exists and Check Path Exists
CheckFileExists property indicates whether the dialog box displays a
warning if the user specifies a file name that does not exist.
CheckPathExists property indicates whether the dialog box displays a
warning if the user specifies a path that does not exist.
SaveFD.CheckFileExists = true;
SaveFD.CheckPathExists = true;
70
Setting properties at run time (5)
File Name and File Names
▪ FileName property represents the file name selected in the open file dialog.
textBox1.Text = SaveFD.FileName;
▪ If MultiSelect property is set to true that means the dialog box allows multiple file
selection. The FileNames property works like an array and represents all the files
selected in the selection.
this.SaveFD.Multiselect = true;
foreach(String file in SaveFD.FileNames)
MessageBox.Show(file);
71
Example
private void SaveButton_Click(object sender, EventArgs e) {
SaveFileDialog SaveFD = new SaveFileDialog();
SaveFD.InitialDirectory = @ "C:\";
SaveFD.Title = "Save text Files";
SaveFD.CheckFileExists = true;
SaveFD.CheckPathExists = true;
72
Example
SaveFD.DefaultExt = "txt";
SaveFD.Filter = "Text files (*.txt)|*.txt|All
files (*.*)|*.*";
SaveFD.FilterIndex = 2;
SaveFD.RestoreDirectory = true;
if (SaveFD.ShowDialog() == DialogResult.OK) {
textBox1.Text = SaveFD.FileName;
}
}
73
Questions?