0% found this document useful (0 votes)
41 views15 pages

(GTTH - Lab3) - Lap-Trình-Windows (Cont)

This document provides code samples and instructions for using various Windows controls in C# Windows Forms applications, including: - Adding checkboxes to a ListView and selecting/deselecting all items. - Deleting selected ListView items with a button click. - Changing the color and font of selected ListView items to highlight them. - Changing a ListView item's background color on mouseover. - Increasing the line height of ListView items. - Creating and adding tab pages to a TabControl at design-time and run-time.

Uploaded by

Duy Bùi Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views15 pages

(GTTH - Lab3) - Lap-Trình-Windows (Cont)

This document provides code samples and instructions for using various Windows controls in C# Windows Forms applications, including: - Adding checkboxes to a ListView and selecting/deselecting all items. - Deleting selected ListView items with a button click. - Changing the color and font of selected ListView items to highlight them. - Changing a ListView item's background color on mouseover. - Increasing the line height of ListView items. - Creating and adding tab pages to a TabControl at design-time and run-time.

Uploaded by

Duy Bùi Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

WINDOWS

PROGRAMMING

Abstract
The practical curriculum of Programming on Windows helps students
master the C # programming language, Windows Form programming.
Provides basic knowledge of visual programming on the Windows
operating system. Basic operations with files, folders, databases,
processes, services, and libraries on Windows. At the end of the course,
students are provided with a full range of knowledge to apply to solve
real problems.

FACULTY OF INFORMATION TECHNOLOGY


Internal circulation, 2020
Windows Programming 1

LAB 3: WINDOWS CONTROLS (CONT)

Overview

- In this section, you will find various Windows Controls related source code samples,
articles, tutorials, and tips.

PRACTICE CONTENT

1.2.1 Winform ListView function extension in table

1.2.1.1 How to add checkbox in listview in C# winforms.

You must set the ListView's CheckBoxes and MultiSelect properties to True. Drag
another CheckBox control below ListView.

Open the Properties dialog box(right-click the CheckBox, and select Properties),
and change its "Text" property to "Select All"; select the "Events" tab, and add

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 2

"CbAll_CheckedChanged" to the right of "CheckedChanged". Press "Enter" to add the


selection change event.
Method 1:
private void SelectAllCheckBoxes(ListView lv, bool currVal)
{
for (int i = 0; i < lv.Items.Count; i++)
lv.Items[i].Checked = currVal;
}
Method 2:
private void SelectAllCheckBoxes(ListView lv, bool currVal)
{
foreach (ListViewItem item in lv.Items)
item.Checked = currVal;
}
Call:
private void CbAll_CheckedChanged(object sender, EventArgs e)
{
SelectAllCheckBoxes(listView1, cbAll.Checked);
}

The effect diagram is shown:

1.2.1.2 Listview delete selected items

Drag a Label control to the right of "Select All" under ListView; open the
"Properties" dialog box(Right-click listView1, select "Properties"), select the "Events"

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 3

tab, add the lblDel_MouseClick event to the right of MouseClick, and add the following
code:
private void lblDel_MouseClick(object sender, MouseEventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
if (item.Checked)
item.Remove();
}
}

1.2.1.3 Listview change color and font for selected items to highlight

Open the Properties dialog box(Right-click listView1, select Properties), select the
"Events" tab, add the listView1_ItemSelectionChanged event to the right of
CheckedChanged, and add the following code:
private void listView1_ItemSelectionChanged(object sender,
ListViewItemSelectionChangedEventArgs e)
{
listView1.FullRowSelect = true;
if (this.listView1.SelectedItems.Count > 0)
{
// Clear the original foreground color
foreach (ListViewItem item in listView1.Items)
item.ForeColor = Color.Black;
// Clear original background color and font
foreach (ListViewItem item in listView1.Items)
{
item.BackColor = Color.White;
Font f = new Font(Control.DefaultFont, FontStyle.Regular);
item.Font = f;
}
listView1.SelectedItems[0].SubItems[0].ForeColor
= Color.OrangeRed;
Font newFont = new Font(Control.DefaultFont, FontStyle.Bold); //
Bold selected line f
listView1.SelectedItems[0].SubItems[0].Font = newFont;
listView1.SelectedItems[0].BackColor = Color.FromArgb(5, 206,
249, 221);// Set background color of selected row
listView1.SelectedItems[0].Selected = false;
}
}

The effect diagram is shown:

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 4

1.2.1.4 ListView mouse over the item, its background changes color

Quick response method


Open the "Properties" dialog box(Right-click listView1, select "Properties"), select the
"Events" tab, add the listView1_MouseMove event to the right of MouseMove, and add the
following code:
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
ListView lv = (ListView)sender;
ListViewItem oldItem = null;
if (lv.Tag != null)
oldItem = (ListViewItem)lv.Tag;
ListViewItem curItem = lv.GetItemAt(e.X, e.Y);// Get the item under the
mouse
if (curItem != null)
{
// Restore the background color of the last item which the mouse was
over
if (oldItem != null && oldItem != curItem)
oldItem.BackColor = lv.BackColor;
curItem.BackColor = Color.LightGreen;// Set the background color of the
current item
lv.Tag = curItem;
}
else
{
if (oldItem != null && oldItem.BackColor != lv.BackColor)
oldItem.BackColor = lv.BackColor;
}
}

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 5

The effect diagram is shown:

Slow response method


Add the listView1_ItemMouseHover event to the right of ItemMouseHover,
and add the following code:
ListViewItem lastSelectedItem = null;
private void listView1_ItemMouseHover(object sender,
ListViewItemMouseHoverEventArgs e)
{
if (lastSelectedItem != null)
lastSelectedItem.BackColor = Color.White;
e.Item.BackColor = Color.LightGreen;
lastSelectedItem = e.Item;
}

1.2.1.5 ListView line height

Code:
private void SetLineHeight()
{
ImageList imgList = new ImageList();
imgList.ImageSize = new Size(1, 24);
// Set the width and height of ImageList
listView1.SmallImageList = imgList;
}

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 6

Call:
private void Form1_Load (object sender, EventArgs e)
{
DisplayTableWithListview();
SetLineHeight();
}
The effect diagram is shown:

1.2.2 TabControl Control

The TabControl manages tab pages where each page may host different child
controls. In this article, I will demonstrate how to create and use a TabControl in
Windows Forms.
Design-time
To create a TabControl control at design-time, you simply drag and drop a
TabControl control from Toolbox onto a Form in Visual Studio. After you drag and drop
a TabControl on a Form, the TabControl1 is added to the Form

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 7

A TabControl is just a container and has no value without tab pages. As you can
see from Figure 1, by default two Tab Pages are added to the TabControl. We can add
and remove tab pages by clicking on the Tasks handle and selecting Add and Remove
Tab links:

Add Tab link adds next tab page and Remove Tab removes the current tab page
from a Tab Control. We will discuss tab pages in more detail later in this tutorial.
Run-time
TabControl class represents a tab control. The following code snippet creates a
TabControl and sets its Name, BackColor, ForeColor, Font, Width, and Height
properties.

Once the TabControl control is ready with its properties, we need to add it to a Form
by calling Form.Controls.Add method. The following code snippet adds a TabControl
control to the current Form

Controls.Add(dynamicTabControl);

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 8

TabPage class represents a tab page control in Windows Forms. The following code
snippet creates two TabPage controls, sets their properties, and calls
TabControl.TabPages.Add() method to add tab pages to TabControl.

New TabControl with two tab pages:

1.2.2.1 TabControl Properties

After you place a TabControl control on a Form, the next step is to set properties.
The easiest way to set properties is from the Properties Window. You can open

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 9

Properties window by pressing F4 or right-click on a control and select the Properties


menu item. The Properties window:

 Name

Name property represents a unique name of a TabControl control. It is used to access


the control in the code. The following code snippet sets and gets the name and text of a
TabControl control.
 Positioning a TabControl

The Dock property is used to set the position of a TabControl. It is of type. DockStyle
that can have values Top, Bottom, Left, Right, and Fill. The following code snippet sets
Location, Width, and Height properties of a TabControl control.

dynamicTabControl.Dock = DockStyle.Left;

 Font

Font property represents the font of text of a TabControl control. If you click on the
Font property in the Properties window, you will see Font name, size, and other font
options. The following code snippet sets Font property at run-time.

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 10

dynamicTabControl.Font = new Font("Georgia", 16);

 Background and Foreground

BackColor and ForeColor properties are used to set background and foreground
color of a TabControl respectively. If you click on these properties in Properties
window, the Color Dialog pops up.

Alternatively, you can set background and foreground colors at run-time. The
following code snippet sets BackColor and ForeColor properties.

dynamicTabControl.BackColor = Color.White;

dynamicTabControl.ForeColor = Color.Black;

 Alignment and Appearance

Alignment property gets or sets the area of the control where the tabs are aligned. A
TabAlignment enumeration is used to set Alignment property and have Top, Bottom,
Left, or Right values.
Appearance property gets or sets the visual appearance of the control's tabs. A
TabAppearance enumeration is used to set the Appearance property and has Normal,
Buttons, or FlatButtons values.

dynamicTabControl.Alignment = TabAlignment.Left;

dynamicTabControl.Appearance = TabAppearance.FlatButtons;

1.2.2.2 Tab Pages

TabPages property, a type of TabPageColleciton object is the gateway to access and


add tab pages to a TabControl. Like any other collection, TabPageCollection has all
collection functionality including add, remove, and find.
We can add and access tab pages of TabControl at design-time from Properties
Window by clicking on TabPages Collection

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 11

When you click on the Collections, the Tab Pages Collection Editor window will
pop up where you can add and remove tab pages and you can also set these tab pages
properties and events. I add four tab pages

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 12

As we have seen earlier in this tutorial, we can also add and remove tab pages to a
TabControl using TabPage class.

1.2.2.3 Adding Child Controls to Tab Pages

Adding child controls to tab pages at design-time is pretty straight forward. You
select a tab page and drag child controls from Toolbox onto a TabPage and set their
properties. Accessing these child controls from code is no different than any other
controls.
Adding child controls at run-time is a little tricky. You create child controls and add
them to the TabPage, not to the form.
The following snippet creates a Button control and adds it to a TabPage.

1.2.2.4 Display Images in Tab Control

To display images in the header of tab pages at design-time, follow these steps.

• Drag an ImageList control onto a Form


• Add images to ImageList by clicking on its Images property
• Set TabControl.ImageList property to ImageList1
• Set TabPage.ImageIndex property to the index of the image in ImageList.
Remember, ImageList index starts at 0.

To set an image in the tab pages header at run-time, first, we need to create an
ImageList and add images to it.

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 13

After that we set ImageList property of TabControl.

dynamicTabControl.ImageList = iconsList;

The last step is to set ImageIndex property of TabPages.

tabPage1.ImageIndex = 0;

A TabControl with image icons

TabCount and RowCount

TabCount property gets the number of tab pages in a TabControl. RowCount


property gets the number of rows that are currently being displayed in the control's tab
strip.

MessageBox.Show(dynamicTabControl.TabCount.ToString());

MessageBox.Show(dynamicTabControl.RowCount.ToString());

Selected Tab

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 14

SelectedTab and SelectedIndex properties get the selected tab and index of the
selected tab in a TabControl.

TabPage selectedTab = dynamicTabControl.SelectedTab;

int selectedIndex = dynamicTabControl.SelectedIndex;

dynamicTabControl.SelectedTab = selectedTab;

--- THE END ---

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy