(GTTH - Lab3) - Lap-Trình-Windows (Cont)
(GTTH - Lab3) - Lap-Trình-Windows (Cont)
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.
Overview
- In this section, you will find various Windows Controls related source code samples,
articles, tutorials, and tips.
PRACTICE CONTENT
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
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"
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;
}
}
1.2.1.4 ListView mouse over the item, its background changes color
Code:
private void SetLineHeight()
{
ImageList imgList = new ImageList();
imgList.ImageSize = new Size(1, 24);
// Set the width and height of ImageList
listView1.SmallImageList = imgList;
}
Call:
private void Form1_Load (object sender, EventArgs e)
{
DisplayTableWithListview();
SetLineHeight();
}
The effect diagram is shown:
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
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);
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.
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
Name
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.
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 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;
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
As we have seen earlier in this tutorial, we can also add and remove tab pages to a
TabControl using TabPage class.
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.
To display images in the header of tab pages at design-time, follow these steps.
To set an image in the tab pages header at run-time, first, we need to create an
ImageList and add images to it.
dynamicTabControl.ImageList = iconsList;
tabPage1.ImageIndex = 0;
MessageBox.Show(dynamicTabControl.TabCount.ToString());
MessageBox.Show(dynamicTabControl.RowCount.ToString());
Selected Tab
SelectedTab and SelectedIndex properties get the selected tab and index of the
selected tab in a TabControl.
dynamicTabControl.SelectedTab = selectedTab;