برمجة مرئية (ن) م3
برمجة مرئية (ن) م3
برمجة مرئية (ن) م3
With (C#)
3
Dr. Waleed Dahea 2024-2025
E-Mail: dahea.waleed@gmail.com
• ComboBox
• TreeView,
• ListView, and
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.
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.
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.
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);
• 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.
• 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");
treeViewControl.Nodes.RemoveByKey("dummy-node");
lblDisplay.Text = e.Node.Text;
21
Hands-On
22
WindowsExplorer
• Controls (Tools) -Libraries
System.IO
• Button زر System.Diagnostics
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();
}
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
35
Thank You !
36