Bài giảng Lập trình C# 2010: Chương 2 trình bày các kiến thức về Windows Forms and Windows Forms Controls trình bày cách tạo form, các thuộc tính phổ biến, các thao tác cơ bản và một số nội dung khác.
DONG NAI UNIVERSITY OF TECHNOLOGY Object MarshalByRefObject Component Control Label Textbox Button LinkLabel … ScrollableControl ContainerControl Form DONG NAI UNIVERSITY OF TECHNOLOGY Create Form Represents a window or dialog box that makes up an application’s user interface Popular Properties Popular Events Tab order DONG NAI UNIVERSITY OF TECHNOLOGY Create Form 1.Right Click on Project Name 2.Choose Add Item 3.Choose Windows Form… DONG NAI UNIVERSITY OF TECHNOLOGY 1.Choose Windows Form Enter name frmLogin.cs 3.Click “Add” button to create DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY Popular Properties DONG NAI UNIVERSITY OF TECHNOLOGY Name Text Description Gets or sets the text associated with this control Gets or sets the Name Gets or sets the size of the form Name Size WindowState Gets or sets the form's window state Normal; Minimized; Maximized Font Gets or sets the font of the text displayed by the control DONG NAI UNIVERSITY OF TECHNOLOGY Name Description StartPosition Gets or sets the starting position of the form at run time Manual CenterScreen WindowsDefaultLocation WindowsDefaultBounds CenterParent TopMost Gets or sets a value indicating whether the form should be displayed as a topmost form DONG NAI UNIVERSITY OF TECHNOLOGY Name FormBorderStyle Description Gets or sets the border style of the form None FixedSingle Fixed3D FixedDialog Sizable FixedToolWindow SizableToolWindow DONG NAI UNIVERSITY OF TECHNOLOGY TextBox – Properties - Events Name Anchor Description Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent Top Lef Right Bottom DONG NAI UNIVERSITY OF TECHNOLOGY Name Description Dock Gets or sets which control borders are docked to its parent control and determines how a control is resized with its parent None Top Lef Right Bottom Fill DONG NAI UNIVERSITY OF TECHNOLOGY Name Multiline Description Gets or sets a value indicating whether this is a PasswordChar Gets or sets the character used to mask characters of a password in a single-line TextBox control ReadOnly Gets or sets a value indicating whether text in the text box is read-only DONG NAI UNIVERSITY OF TECHNOLOGY Name Description UseSystemPas Gets or sets a value indicating swordChar whether the text in the TextBox control should appear as the default password character Visible Gets or sets a value indicating whether the control and all its child controls are displayed Enabled Gets or sets a value indicating whether the control can respond to user interaction DONG NAI UNIVERSITY OF TECHNOLOGY Name TextChanged Description Occurs when the Text property value changes private void txtN am e_TextChanged (object sender, EventArgs e) { this.Text = txtN am e.Text; } DONG NAI UNIVERSITY OF TECHNOLOGY Add Button & Event at Runtime txtNumberControl pnButton AutoScroll=true lblMessage btnAddButton DONG NAI UNIVERSITY OF TECHNOLOGY p rivate void b tn A d d B u tton _C lick(ob ject sen d er, Even tA rg s e) { pnButton.Controls.Clear(); for (int i= 0; i< Int32.Parse(txtN um berControl.Text); i+ + ) { Button btnRuntim e = new Button(); btnRuntim e.BackColor = Color.Red; btnRuntim e.Location = new System D raw ing.Point (pnButton.W idth/2-btnRuntim e.W idth/2, i* btnRuntim e.H eight); btnRuntim e.Text = "a_" + i; btnRuntim e.Tag = i; btnRuntim e.Click + = btnRuntim e_click; pnButton.Controls.Add(btnRuntim e);} } p rivate void b tn R u n tim e_click(ob ject sen d er, Even tA rg s e) { Button btn = (Button)sender; DONG NAI UNIVERSITY OF TECHNOLOGY GroupBox & RadioButton Name Checked Description Gets or sets a value indicating whether the control is checked CheckAlign Gets or sets the location of the check box portion of the RadioButton if (radRed.Checked = = true) { radBlue.CheckAlign = //do som ething for Red ContentAlignm ent.M iddleLeft; } GroupBox & RadioButton pnColor DONG NAI UNIVERSITY OF TECHNOLOGY private void frm G roupBoxRadio_Load (object sender, EventArgs e) { radRed.CheckedChanged + = rad_CheckedChanged; radG reen.CheckedChanged + = rad_CheckedChanged; radBlue.CheckedChanged + = rad_CheckedChanged; } private void rad_CheckedChanged (object sender, EventArgs e) { RadioButton rad = (RadioButton)sender; pnColor.BackColor = rad.ForeColor; } DONG NAI UNIVERSITY OF TECHNOLOGY PictureBox control DONG NAI UNIVERSITY OF TECHNOLOGY Name Description BackgroundImage Gets or sets the background image displayed in the control BackgroundImage Gets or sets the background Layout image layout as defined in the ImageLayout enumeration DONG NAI UNIVERSITY OF TECHNOLOGY Name Image Description Gets or sets the image that is displayed by PictureBox SizeMode Indicates how the image is displayed DONG NAI UNIVERSITY OF TECHNOLOGY Get Picture from OpenFileDialog picDemo DONG NAI UNIVERSITY OF TECHNOLOGY private void btnO penPic_Click (object sender, EventArgs e) { O penFileD ialog fi leO penD lg = new O penFileD ialog(); fi leO penD lg.Filter = "(*.jpg)|*.jpg|(*.doc)|*.doc"; if (fi leO penD lg.Show D ialog() = = D ialogResult.O K) { //picD em o.Im age = new Bitm ap(fi leO penD lg.O penFile()); //O r picD em o.Im age = Im age.From File(fi leO penD lg.FileN am e); //O r //picD em o.BackgroundIm age = new Bitm ap(fi leO penD lg.O penFile()); } } DONG NAI UNIVERSITY OF TECHNOLOGY END ... 2. Choose Add Item 3.Choose Windows Form… DONG NAI UNIVERSITY OF TECHNOLOGY 1.Choose Windows Form Enter name frmLogin.cs 3.Click “Add” button to create DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI. .. and icon MessageBoxIcon) DONG NAI UNIVERSITY OF TECHNOLOGY MessageBox.Show("Hello Tèo 20 11"); MessageBox.Show("Hello Tèo 20 11","Title"); MessageBox.Show("Hello Tèo 20 11","Title", MessageBoxButtons.YesNoCancel);... MessageBox.Show("Hello Tèo 20 11","Title", MessageBoxButtons.OK, MessageBoxIcon.Question); DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY Panel & SplitContainer DONG NAI UNIVERSITY OF