0% found this document useful (0 votes)
2 views36 pages

برمجة مرئية (ن) م3

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 36

Visual Programming

With (C#)

3
Dr. Waleed Dahea 2024-2025
E-Mail: dahea.waleed@gmail.com

C# Programming: From Problem Analysis to Program Design 1


Chapter Objectives
• Use C# and Visual Studio to create Windows-based applications.

• Add Control objects such as :-

• ComboBox

• TreeView,

• ListView, and

• ImageList ……to a form

• Work through a programming example that illustrates the


lecture’s concepts

2
ComboBox
• ComboBox control is one of the most useful control in
Windows Forms. It provides an interface to display the list
of items, and allows to select the items. To save the space
on Windows Form, it lists the items as a drop-down list.
That means, initially the control displays a text box & a
button. When click on the button, it displays the list of
items and the selected item will be displayed in a text box.

• ComboBox control is a combination of text box,


button & list box controls.

3
4
Standard Controls

5
Properties
• We need to add the entries to ComboBox control, in order to
display them when needed. As mentioned above, ComboBox
control displays it’s items in a list box. It holds the list of
items in it’s Items collection, which is an important property
of this control. That said, Items property is of collection type.
To add or remove the items to & from the ComboBox
control, we use this Items property.
• When Sorted property is set to True , it’s items will be
sorted.
• Text property returns the text displayed in the text box. That
means, it returns the selected item’s text. This is editable, but
it will be disabled depends on the value set in
DropDownStyle property. 6
Properties
• DropDownStyle property controls the appearance and functionality of
the ComboBox control. Depending on your program requirement, you
can set this property to proper value. When set this property to;
1. Simple, it will display it’s items in the list box and a text box will be
displayed to display the selected item from it’s list box. Text box is
allowed to edit the text it displays.

7
Properties
2. DropDown, it displays a text box and a button control next
to it. When click on the button control, it shows the list of
items the control has and allows to select the item(s). The
selected item will be displayed in the text box and it is
editable.

8
Properties
3. DropDownList, it behaves like when we set this property
to DropDown; except, the text box is NOT editable. That
means, when we select the item from the ComboxBox
control, it displays the selected item in the text box and but it
doesn’t allow to edit the text. When set this; Text property
will behave like read-only.

• Another useful property is, MaxDropDownItems


property. This allows to set the number of items to be
visible at a time, when drop-down list is displayed. This
you set to the proper value, depending on your application
window size.
9
Methods
• Adding and removing items to & from the ComboBox
control can be done through the methods of Items property.
Items.Add method will add an item to the ComboBox
control. And, Items.Remove method will be used to
remove an item from the ComboBox control. To remove
the item from the specific position index, we can use
Items.RemoveAt method.

• To remove all items from the ComboBox control, we can


use Items.Clear() method..

10
Events
• SelectedIndexChanged event will be triggered whenever
we select an item from the ComboBox control. Actually
this will be triggered whenever SelectedIndex property
value is changed.

• DropDown event will be raised when the drop-down


portion of the ComboBox control is visible. Usually this
will be visible when we click on the button to select an
item from the ComboBox control. Once the selection is
done, the drop-down portion will be hidden. Then,
DropDownClosed event will be triggered. These two
events are very useful to place the code to update or reset
11
the changes for the ComboBox control.
Example

12
if (comboBox1.Text.Length > 0)
comboBox1.Items.Add(comboBox1.Text);

if (comboBox1.SelectedIndex != -1)
comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);

comboBox1.Items.Clear();

switch (comboBox1.DropDownStyle)
{
case ComboBoxStyle.Simple:
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
break;

case ComboBoxStyle.DropDown:
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
break;

case ComboBoxStyle.DropDownList:
comboBox1.DropDownStyle = ComboBoxStyle.Simple;
comboBox1.Height = comboBox1.ItemHeight * 15;
break;
13
}
TreeView control
• TreeView control in C# is used to display the items in hierarchical
form. Each item in TreeView control is called a node. A node can
have sub-nodes too; and each sub-node has it’s own nodes, and so on.
All the nodes in the TreeView control are displayed in an hierarchical
form, for better readability and control.
• Add TreeView control to the Form
• Create a C# Windows Forms Application in Visual Studio IDE. In
Form’s design view, add the TreeView control from the Toolbox.
Visual Studio automatically, creates a variable for this control and by
using this we can use the features of the control.

• You can add the nodes to the control in design view also, by selecting
Nodes property from the Properties dialog
14
15
Adding nodes to the TreeView control
• A Treeview control is the collection of nodes; each node is in the same
level or in different hierarchical position in the tree structure. These
collection of nodes are managed through it’s Nodes property.
• Nodes is of TreeNodeCollection type, that represents the collection of
tree nodes assigned to the TreeView control.
• TreeNodeCollection has Add() method, which allows to add a new
tree node to the end of the Nodes collection. Add() method has
number of variances; these are used to add a node with label text, with
key, with image index etc,.
• Usually, we use the simple Add() method variance to add a new tree
node with labeled text. Here is the code in C#;
• TreeNode node = ctrlTreeView.Nodes.Add("Parent Node");
• The above statement adds a new tree node, and returns the tree node
that has added to the tree Nodes collection. The node is of TreeNode
type. 16
Removing a node from TreeView control
• We can also remove the node from tree nodes collection, if we have
the reference to TreeNode. As we have the reference of “Sub Node” in
sub_node variable, below statement can directly removes the node
from the collection.

• node.Nodes.Remove(sub_node);

• These methods will remove the node which is in current nodes


collection.

• We can also remove the node by calling it’s Remove() method. This is
possible, only when we have the node reference.

• sub_node.Remove();
17
Find a node in TreeView control
• To find a node by it’s key, we use TreeNodeCollection‘s
Find() method. We need to specify the key value and
whether to look into it’s sub-nodes also to search for the
node.

• Find() method returns the list of nodes which matches the


given key value. Below is an example, which removes the
first node which matches with the given key value.

• node.Nodes.Find("key-value", true)[0].Remove();

18
Example

19
Example
private void Form1_Load(object sender, EventArgs e)
{
TreeNode node = treeViewControl.Nodes.Add("root", "Government Space Agencies");

TreeNode sub_node = node.Nodes.Add("country", "Country");

sub_node.Nodes.Add("usa", "United States").Nodes.Add("nasa", "National Aeronautics and Space


Administration (NASA)");
sub_node.Nodes.Add("chn", "China").Nodes.Add("cnsa", "China National Space Administration
(CNSA)");
sub_node.Nodes.Add("jpn", "Japan").Nodes.Add("jaxa", "Japan Aerospace Exploration Agency
(JAXA)");
sub_node.Nodes.Add("ind", "India").Nodes.Add("isro", "Indian Space Research Organization (ISRO)");
sub_node.Nodes.Add("rus", "Russia").Nodes.Add("rfsa", "Russian Federal Space Agency (RFSA)");

sub_node.Nodes.Add("dummy-node", "Dummy Node");

treeViewControl.Nodes.RemoveByKey("dummy-node");

// Find for the dummy node & remove it


treeViewControl.Nodes.Find("dummy-node", true)[0].Remove();

// Expand all nodes


treeViewControl.ExpandAll();
} 20
}}
Example

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)


{

lblDisplay.Text = e.Node.Text;

21
Hands-On

22
WindowsExplorer
• Controls (Tools) -Libraries
System.IO
• Button ‫زر‬ System.Diagnostics

• TextBox ‫صندوق نص‬

• GroupBox ‫صندوق تجميع‬

• ComboBox ‫صندوق االختيار‬

• TreeView ‫شجرة العرض‬

• ListView ‫قائمة العرض‬

• ImageList ‫قائمة الصور‬


23
Windows-Based Applications

24
Windows Form Properties

25
Windows Form Properties

26
Windows Form Properties

27
Windows Form Properties

28
Coding
public partial class Form1 : Form
{
string SelectedDrive, SelectedPath;
public Form1()
{
InitializeComponent();
comboBox1.Text = "C:\\";
comboBox2.Text = "Details";
getDrives();
comboView();
}
void comboView()
{
comboBox2.Items.Add("LargeIcon");
comboBox2.Items.Add("Details");
comboBox2.Items.Add("SmallIcon");
comboBox2.Items.Add("List");
comboBox2.Items.Add("Tile");
}
void getDrives()
{
foreach (string drive in Directory.GetLogicalDrives())
{
comboBox1.Items.Add(drive);
}
}
29
Coding

void getFolders()
{
SelectedDrive = comboBox1.Text;
DirectoryInfo Dir = new DirectoryInfo(SelectedDrive);
treeView1.Nodes.Clear();
foreach (DirectoryInfo Folder in Dir.GetDirectories())
{
treeView1.Nodes.Add("", Folder.Name, 0, 0);
}
}

30
Coding
void getFiles(string strPath)
{
ListViewItem lvi;
DirectoryInfo Dir = new DirectoryInfo(SelectedDrive + strPath);
listView1.Items.Clear();
try
{
foreach (FileInfo files in Dir.GetFiles())
{
lvi = listView1.Items.Add(files.Name, 1);
double FileLength = files.Length;
lvi.SubItems.Add(Math.Round(FileLength / 1000, 3) + "
KB");
lvi.SubItems.Add(files.LastAccessTime.ToString());
lvi.SubItems.Add(files.Extension);
lvi.SubItems.Add(files.CreationTime.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
31
Coding
private void comboBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
getFolders();
}

private void comboBox2_SelectedIndexChanged(object sender,


EventArgs e)
{
if (comboBox2.Text == "LargeIcon")
listView1.View = View.LargeIcon;
else if (comboBox2.Text == "Details")
listView1.View = View.Details;
else if (comboBox2.Text == "SmallIcon")
listView1.View = View.SmallIcon;
else if (comboBox2.Text == "List")
listView1.View = View.List;
else
listView1.View = View.Tile; 32
}
Coding

private void listView1_Enter(object sender, EventArgs e)


{
button1.Enabled = true;
}

private void treeView1_Enter(object sender, EventArgs e)


{
button1.Enabled = false;
}

private void textBox1_TextChanged(object sender, EventArgs e)


{
if (File.Exists(SelectedPath + "\\" + textBox1.Text))
MessageBox.Show("Exists!");
}

33
Coding
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
SelectedPath = e.Node.FullPath;
getFiles(SelectedPath);
TreeNode node;
DirectoryInfo Dir = new DirectoryInfo(SelectedDrive +
SelectedPath);
try
{
foreach (DirectoryInfo folder in Dir.GetDirectories())
{
node = new TreeNode(folder.Name, 0, 0);
e.Node.Nodes.Add(node);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SelectedPath = SelectedDrive + SelectedPath;
}
34
Coding

private void button1_Click(object sender, EventArgs e)


{
try
{
Process.Start(SelectedPath + "\\" +
listView1.FocusedItem.Text);
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}

35
Thank You !

• Dr. Waleed 2024-2025


• Mobile: 780768488
• E-Mail: dahea.waleed@gmail.com

36

You might also like