1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter 2 Windows Programming

103 488 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 103
Dung lượng 1,68 MB

Nội dung

Chapter Windows Programming Contents • • • • • • Introduction to Windows Form Application Introduction to Form Introduction to Control Events Some common Controls Some advanced Controls Slide Create a Windows Form Application Slide Programming interface • Review – Solution Explorer – Toolbox – Properties Window Slide Contents • • • • • • Introduction to Windows Form Application Introduction to Form Introduction to Control Events Some common Controls Some advanced Controls Slide Introduction to Form • Form (also called Windows Form) – is a container for controls and components – belongs to System.Windows.Forms namespace • Some actions with Form – To add a new Form: right-click to the project shown in Solution Explorer, select Add\Windows Form – To add a existing Form: right-click to the project shown in Solution Explorer, select Add\Existing Item (choose file cs) – To exclude a Form: right-click to the form shown in Solution Explorer, select Exclude From Project • In the Windows Forms Designer, a Windows form can be seen in two views: the Design View and the Code View • Exploring the Generated Code Slide Common Form properties and methods Slide Contents • • • • • • Introduction to Windows Form Application Introduction to Form Introduction to Control Events Some common Controls Some advanced Controls Slide Introduction to Control • Control – is a component with graphical part, such as button, label… – is visible • Controls belong to System.Windows.Forms namespace • Most controls derive from the System.Windows.Forms.Control class  many properties and events in the controls are identical – See next slide Slide Common properties and methods of Controls (p.499) PROPERTY DESCRIPTION Anchor Specifies how the control behaves when its container is resized BackColor The background color of a control Dock Docks a control to the edges of its container Enabled Specifies whether the control receive input from the user ForeColor The foreground color of the control Name The name of the control TabIndex The number the control has in the tab order of its container TabStop If true, user can use the Tab key to select the control Text Holds the text that is associated with this control Visible Specifies whether the control is visible at runtime METHOD DESCRIPTION Hide Hides the control Show Shows the control Focus Transfers the focus to a control Slide 10 Example: Build TreeView at run-time 89 Delete selected node private void DeleteSelectedNode(TreeView tree) { if (tree.SelectedNode != null) tree.Nodes.Remove(tree.SelectedNode); } Slide 90 Display selected node private void treeview1_AfterSelect(…) { if (treeview1.SelectedNode != null) { TextBox1.Text = treeview1.SelectedNode.Text; } } Slide 91 Visit direct children of the node • You can visit direct children of selected node, by: foreach (TreeNode node in tree.SelectedNode.Nodes) { // process node } • You can visit direct children of any node 92 Example TreeView Slide 93 Creating Controls • User (composite) controls – Build on the functionality of existing controls to create a new control – Generally made to encapsulate functionality with the user interface of the control, or to enhance the interface of a control by combining several controls into one unit • Custom controls – Create these controls when no existing control fits your needs—that is, you start from scratch Slide 94 Creating user control • Example: Try it out from p.523 to p.530 – Create a user-control combines two existing controls (label, textbox) to create a single one • With the capability to position the text box either to the right of the label or below it • Availability of the usual properties and events of the text box and label – Write an Windows Application using that user-control Slide 95 Prevent a Form from closing • To prevent a Form from closing when the user click x button or press Alt+F4 – Using of the Form’s FormClosing event: private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("Có thật khơng?","Cảnh báo", MessageBoxButtons.OKCancel) == DialogResult.Cancel) e.Cancel = true; } Slide 96 Display a new form • How to display a form from another one? Form2 f = new Form2(); f.Show(); // or f.ShowDialog(); • Difference between Show and ShowDialog method? –Show is used to show a modeless form, ex Find dialog box • When a form is displayed as modeless, the code following the Show method is executed immediately after the form is displayed –ShowDialog is is used to show a modal form, ex About dialog in VS, Message box form • When a form is displayed as modal, the code following the ShowDialog method is not executed until the form is closed Slide 97 Passing data between forms • Some methods: – – – – Using constructor Using objects Using properties Using delegates Slide 98 Using Constructor • On Form2: public Form2( string s ) { InitializeComponent(); label1.Text = s; } • On Form1: private void button1_Click(object sender, System.EventArgs e) { Form2 frm = new Form2(textBox1.Text); frm.ShowDialog(); } Slide 99 Using objects [1] • Change the access modifier for textbox in Form1 to public • In the button click event-handler, add the following code: Form2 frm = new Form2(); frm.frm1 = this; frm.ShowDialog(); • In Form2, create an object of Form1 class: public Form1 frm1; • In Form2’s Load method, write a statement: label1.Text = frm1.textBox1.Text; Slide 100 Using objects [2] • Change the access modifier for label in Form2 to public • In the button click event-handler, add the following code: Form2 frm = new Form2(); frm.label1.Text = textBox1.Text; frm.ShowDialog(); Slide 101 Using properties • Add a property in Form2 to set the labels’ text: public string LabelText { get { return label1.Text; } set { label1.Text = value; } } • In Form1’s button click event handler, add the following code: Form2 frm = new Form2(); frm.LabelText = textBox1.Text; frm.ShowDialog(); • Note: You can use public variable Slide 102 Using Delegates • Add a delegate signature to Form1: public delegate void delPassData(TextBox text); • In Form2, add a function to which the delegate should point to: public void funData(TextBox txtForm1) { label1.Text = txtForm1.Text; } • In Form1’s button click event handler: Form2 frm = new Form2(); delPassData del = new delPassData(frm.funData); del(this.textBox1); frm.ShowDialog(); Slide 103 ... Introduction to Windows Form Application Introduction to Form Introduction to Control Events Some common Controls Some advanced Controls Slide Create a Windows Form Application Slide Programming. .. Slide 21 Some common Controls Button Label, LinkLabel TextBox, RichTextBox GroupBox, Panel CheckBox, RadioButton PictureBox ListBox, CheckedListBox ComboBox ListView 10 TabControl Slide 22 Button... components – belongs to System .Windows. Forms namespace • Some actions with Form – To add a new Form: right-click to the project shown in Solution Explorer, select Add \Windows Form – To add a existing

Ngày đăng: 13/05/2014, 11:30

TỪ KHÓA LIÊN QUAN