Bài giảng C# 2010 - Chapter 2: Windows Form and Windows Forms Controls (Part 1)

private void btnAddButton_Click(object sender, EventArgs e)

{

 pnButton.Controls.Clear();

 for (int i = 0; i < Int32.Parse(txtNumberControl.Text); i++)

 {Button btnRuntime = new Button();

 btnRuntime.BackColor = Color.Red;

 btnRuntime.Location = new System.Drawing.Point (pnButton.Width/2-btnRuntime.Width/2,

 i * btnRuntime.Height);

 btnRuntime.Text = "a_" + i;

 btnRuntime.Tag = i;

 btnRuntime.Click += btnRuntime_click;

 pnButton.Controls.Add(btnRuntime);}

}

private void btnRuntime_click(object sender, EventArgs e)

{

 Button btn = (Button)sender;

 lblMessage.Text = "Button : "+btn.Text +" was clicked";

}

 

pptx45 trang | Chuyên mục: Visual C# | Chia sẻ: dkS00TYs | Lượt xem: 2181 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng C# 2010 - Chapter 2: Windows Form and Windows Forms Controls (Part 1), để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 13/05/2013 ‹#› Object MarshalByRefObject Component Control Label Textbox Button LinkLabel ScrollableControl ContainerControl Form … Windows Form Popular Properties Popular Events Tab order Create Form Represents a window or dialog box that makes up an application’s user interface Create Form Right Click on Project Name Choose Add Item Choose Windows Form… 2. Enter name frmLogin.cs 1.Choose Windows Form 3.Click “Add” button to create Popular Properties Name Description Text Gets or sets the text associated with this control Name Gets or sets the Name Size Gets or sets the size of the form. 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 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. Name Description FormBorderStyle Gets or sets the border style of the form None FixedSingle Fixed3D FixedDialog Sizable FixedToolWindow SizableToolWindow Name Description BackColor Gets or sets the background color for the control BackGroundImage Gets or sets the background image displayed in the control MainMenuStrip Gets or sets the primary menu container for the form ForceColor Gets or sets the foreground color of the control Name Description Cursor Gets or sets the cursor that is displayed when the mouse pointer is over the control Location Gets or sets the Point that represents the upper-left corner of the Form in screen coordinates Icon Gets or sets the icon for the form. Opacity Gets or sets the opacity level of the form Name Description AcceptButton Gets or sets the button on the form that is clicked when the user presses the ENTER key. CancelButton Gets or sets the button control that is clicked when the user presses the ESC key DEMO Windows Forms Popular Events For Windows Forms & Another Controls Name Description Click Occurs when the control is clicked. (Inherited from Control.) DoubleClick Occurs when the control is double -clicked. (Inherited from Control.) Load Occurs before a form is displayed for the first time. FormClosing Occurs before the form is closed. FormClosed Occurs after the form is closed. Name Description KeyDown Occurs when a key is pressed while the control has focus. (Inherited from Control.) KeyPress Occurs when a key is pressed while the control has focus. (Inherited from Control.) KeyUp Occurs when a key is released while the control has focus. (Inherited from Control.) Name Description MouseClick Occurs when the control is clicked by the mouse. (Inherited from Control.) MouseDoubleClick Occurs when the control is double clicked by the mouse. (Inherited from Control.) MouseDown Occurs when the mouse pointer is over the control and a mouse button is pressed.(Control) Name Description MouseEnter Occurs when the mouse pointer enters the control.(Control) MouseHover Occurs when the mouse pointer rests on the control(Control) MouseLeave Occurs when the mouse pointer leaves the control. (Control) MouseMove Occurs when the mouse pointer is moved over the control. (Control) Name Description MouseUp Occurs when the mouse pointer is over the control and a mouse button is released.(Control) MouseWheel Occurs when the mouse wheel moves while the control has focus.(Control) DEMO Events MessageBox class Name Description Show(string) Show a message box with text. Show(string,string) Show a message box with text and caption. Show(string,string, MessageBoxButtons) Show a message box with text, caption, and buttons. Show(string,string, MessageBoxButtons, MessageBoxIcon) Show a message box with text, caption, buttons, and icon. MessageBox.Show("Hello Tèo 2011"); MessageBox.Show("Hello Tèo 2011","Title"); MessageBox.Show("Hello Tèo 2011","Title", 	 MessageBoxButtons.YesNoCancel); MessageBox.Show("Hello Tèo 2011","Title", 	MessageBoxButtons.OK, 	 MessageBoxIcon.Question); Panel & SplitContainer Label LinkLabel TextBox Button LinkLabel Events Name Description LinkClicked The LinkClicked event is handled to perform tasks when the user clicks on a link in the control. It is passed an instance of the LinkLabelLinkClickedEventArgs class private void linkLabel1_LinkClicked(object sender, 	LinkLabelLinkClickedEventArgs e) { switch (e.Button) { case MouseButtons.Left: System.Diagnostics.Process.Start(linkLabel1.Text); break; case MouseButtons.Right: 	MessageBox.Show("Right Click"); break; case MouseButtons.Middle: MessageBox.Show("Middle Click", "Title", 	MessageBoxButtons.YesNoCancel, 	MessageBoxIcon.Information); break; } } How add LinkLabel control & event at Runtime? private LinkLabel linkTeo; private void Form1_Load(object sender, EventArgs e) { //Create the linklabel linkTeo = new LinkLabel(); //set location for linklabel linkTeo.Location =new System.Drawing.Point(100, 100); //Set the text for linklabel linkTeo.Text= ""; //Add the controls to the form this.Controls.Add(linkTeo); //Add an event at runtime linkTeo.LinkClicked += linkTeo_Clicked; } private void linkTeo_Clicked(object sender, 	LinkLabelLinkClickedEventArgs e) { if (sender is LinkLabel) 	MessageBox.Show(((LinkLabel)sender).Text); MessageBox.Show("Add event at runtime"); } TextBox – Properties - Events Name Description Anchor 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 Left Right Bottom 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 Left Right Bottom Fill Name Description Multiline 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. Name Description UseSystemPasswordChar Gets or sets a value indicating 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. Name Description TextChanged Occurs when the Text property value changes. private void txtName_TextChanged (object sender, EventArgs e) { this.Text = txtName.Text; } pnButton AutoScroll=true lblMessage txtNumberControl btnAddButton Add Button & Event at Runtime private void btnAddButton_Click(object sender, EventArgs e) { pnButton.Controls.Clear(); for (int i = 0; i < Int32.Parse(txtNumberControl.Text); i++) {Button btnRuntime = new Button(); btnRuntime.BackColor = Color.Red; btnRuntime.Location = new System.Drawing.Point 	(pnButton.Width/2-btnRuntime.Width/2, 	 i * btnRuntime.Height); btnRuntime.Text = "a_" + i; btnRuntime.Tag = i; btnRuntime.Click += btnRuntime_click; pnButton.Controls.Add(btnRuntime);} }	 private void btnRuntime_click(object sender, EventArgs e) { Button btn = (Button)sender; lblMessage.Text = "Button : "+btn.Text +" was clicked"; } GroupBox & RadioButton Name Description Checked 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) { //do something for Red } radBlue.CheckAlign = ContentAlignment.MiddleLeft; private void frmGroupBoxRadio_Load (object sender, EventArgs e) { radRed.CheckedChanged += 	rad_CheckedChanged; radGreen.CheckedChanged += 	rad_CheckedChanged; radBlue.CheckedChanged += 	rad_CheckedChanged; } private void rad_CheckedChanged (object sender, EventArgs e) { RadioButton rad = (RadioButton)sender; pnColor.BackColor = rad.ForeColor; } pnColor GroupBox & RadioButton PictureBox control Name Description BackgroundImage Gets or sets the background image displayed in the control BackgroundImageLayout Gets or sets the background image layout as defined in the ImageLayout enumeration Name Description Image Gets or sets the image that is displayed by PictureBox SizeMode Indicates how the image is displayed. Get Picture from OpenFileDialog picDemo private void btnOpenPic_Click (object sender, EventArgs e) { OpenFileDialog fileOpenDlg = new OpenFileDialog(); fileOpenDlg.Filter = "(*.jpg)|*.jpg|(*.doc)|*.doc"; if (fileOpenDlg.ShowDialog() == DialogResult.OK) { //picDemo.Image = new Bitmap(fileOpenDlg.OpenFile()); //Or picDemo.Image = Image.FromFile(fileOpenDlg.FileName); //Or //picDemo.BackgroundImage = new Bitmap(fileOpenDlg.OpenFile()); } } END 

File đính kèm:

  • pptxBài giảng C# 2010 - Chapter 2 Windows Form and Windows Forms Controls (Part 1).pptx