C# cont d (C-sharp) (many of these slides are extracted and adapted from Deitel s book and slides, How to Program in C#. They are provided for CSE3403 students only. Not to be published or publicly distributed without permission by the publisher). 1
User-Defined Controls Custom controls that inherit from other classes Ex: can change appearance of a label 2
GUI with user defined controls 3
Can edit the control s placeholder, but not the (inherited) control itself. 4
Running 5
Building the controls 6
Building the controls Dropped a standard Button in the new UserControl1 placeholder and adjusted its properties. 7
Building the controls Dropped 2 standard Buttons and adjusted their properties (and events). Dropped a standard Label and adjusted its properties. The userdefined control s placeholder. 8
Building the controls The resulting code components after building the controls. 9
Building the controls Once build the project, a.dll named WindowsControlLibrary1.dll is generated. This.DLL contains all the classes that correspond to the constructed controls. 10
To use any user defined controls Create new windows application. Then, from the toolbox, right-click and choose Add/Remove Items. 11
Select the desired controls The selected controls are made available in the Toolbox. You can use these controls to build any Windows Application, as usual. 12
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication10UsingUserDefinedControl / <summary> / Summary description for Form1. / </summary> public class Form1 : System.Windows.Forms.Form private WindowsControlLibrary1.UserControl1 usercontrol11; private WindowsControlLibrary1.UserControl1 usercontrol12; private WindowsControlLibrary1.UserControl2 usercontrol22; / <summary> / Required designer variable. / </summary> private System.ComponentModel.Container components = null; public Form1() Required for Windows Form Designer support InitializeComponent(); The code Form1.cs that uses user-defined controls Declare 2 blue buttons (UserControl1) and 1 timer (UserControl2). TODO: Add any constructor code after InitializeComponent call 13
Increase the default size of the placeholder of this blue button.. For one of the blue buttons, associate a click event on its placeholder. Keep the default size of the placeholder of this blue button. private void InitializeComponent() this.usercontrol11 = new WindowsControlLibrary1.UserControl1(); this.usercontrol12 = new WindowsControlLibrary1.UserControl1(); this.usercontrol22 = new WindowsControlLibrary1.UserControl2(); this.suspendlayout(); usercontrol11 this.usercontrol11.backcolor = System.Drawing.Color.DodgerBlue; this.usercontrol11.location = new System.Drawing.Point(16, 8); this.usercontrol11.name = "usercontrol11"; this.usercontrol11.size = new System.Drawing.Size(120, 80); this.usercontrol11.tabindex = 0; this.usercontrol11.click += new System.EventHandler(this.userControl11_Click); usercontrol12 this.usercontrol12.backcolor = System.Drawing.Color.DodgerBlue; this.usercontrol12.location = new System.Drawing.Point(192, 24); this.usercontrol12.name = "usercontrol12"; this.usercontrol12.size = new System.Drawing.Size(96, 48); this.usercontrol12.tabindex = 2; usercontrol22 this.usercontrol22.location = new System.Drawing.Point(48, 104); this.usercontrol22.name = "usercontrol22"; this.usercontrol22.size = new System.Drawing.Size(192, 104); this.usercontrol22.tabindex = 3; 14
Form1 this.autoscalebasesize = new System.Drawing.Size(5, 13); this.clientsize = new System.Drawing.Size(304, 229); this.controls.add(this.usercontrol22); this.controls.add(this.usercontrol12); this.controls.add(this.usercontrol11); this.name = "Form1"; this.text = "Form1"; this.resumelayout(false); / #endregion / <summary> / The main entry point for the application. / </summary> [STAThread] static void Main() Application.Run(new Form1()); private void usercontrol11_click(object sender, System.EventArgs e) MessageBox.Show("User control clicked"); Executes when user clicks on the placeholder of the control (but not on the blue button itself). 15
The code for UserControl1 (blue button) and UserControl2 (timer) 16
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; namespace WindowsControlLibrary1 / <summary> / Summary description for UserControl1. / </summary> public class UserControl1 : System.Windows.Forms.UserControl private System.Windows.Forms.Button button1; / <summary> / Required designer variable. / </summary> private System.ComponentModel.Container components = null; UserControl1.cs public UserControl1() This call is required by the Windows.Forms Form Designer. InitializeComponent(); TODO: Add any initialization after the InitComponent call / <summary> / Clean up any resources being used. / </summary> User defined controls are made as subclasses of class UserControl. Put a button in this user defined control. 17
protected override void Dispose( bool disposing ) if( disposing ) if( components!= null ) components.dispose(); base.dispose( disposing ); #region Component Designer generated code / <summary> / Required method for Designer support - do not modify / the contents of this method with the code editor. / </summary> private void InitializeComponent() this.button1 = new System.Windows.Forms.Button(); this.suspendlayout(); button1 this.button1.font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(161))); this.button1.forecolor = System.Drawing.Color.Black; this.button1.location = new System.Drawing.Point(0, 0); this.button1.name = "button1"; this.button1.size = new System.Drawing.Size(96, 48); this.button1.tabindex = 0; this.button1.text = "Blue Button"; this.button1.click += new System.EventHandler(this.button1_Click); 18
#endregion / UserControl1 this.backcolor = System.Drawing.Color.DodgerBlue; this.controls.add(this.button1); this.name = "UserControl1"; this.size = new System.Drawing.Size(96, 48); this.resumelayout(false); private void button1_click(object sender, System.EventArgs e) MessageBox.Show("Blue button clicked"); Executes when user clicks on the blue button within this control. 19
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; UserControl2.cs (timer) namespace WindowsControlLibrary1 / <summary> / Summary description for UserControl2. / </summary> public class UserControl2 : System.Windows.Forms.UserControl private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.ComponentModel.IContainer components; Declare needed components that are included in this control. public UserControl2() This call is required by the Windows.Forms Form Designer. InitializeComponent(); TODO: Add any initialization after the InitializeComponent call / <summary> / Clean up any resources being used. / </summary> 20
protected override void Dispose( bool disposing ) if( disposing ) if(components!= null) components.dispose(); base.dispose( disposing ); #region Component Designer generated code / <summary> / Required method for Designer support - do not modify / the contents of this method with the code editor. / </summary> private void InitializeComponent() this.components = new System.ComponentModel.Container(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.suspendlayout(); timer1 this.timer1.tick += new System.EventHandler(this.timer1_Tick); label1 this.label1.backcolor = System.Drawing.Color.Yellow; this.label1.font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(161))); this.label1.location = new System.Drawing.Point(16, 48); this.label1.name = "label1"; this.label1.size = new System.Drawing.Size(168, 40); this.label1.tabindex = 0; this.label1.textalign = System.Drawing.ContentAlignment.MiddleCenter; 21
button1 this.button1.location = new System.Drawing.Point(16, 16); this.button1.name = "button1"; this.button1.tabindex = 1; this.button1.text = "Start Timer"; this.button1.click += new System.EventHandler(this.button1_Click); button2 this.button2.location = new System.Drawing.Point(104, 16); this.button2.name = "button2"; this.button2.tabindex = 2; this.button2.text = "Stop Timer"; this.button2.click += new System.EventHandler(this.button2_Click); UserControl2 this.controls.add(this.button2); this.controls.add(this.button1); this.controls.add(this.label1); this.name = "UserControl2"; this.size = new System.Drawing.Size(192, 104); this.resumelayout(false); #endregion 22
/ private void timer1_tick(object sender, System.EventArgs e) label1.text = DateTime.Now.ToLongTimeString(); private void button1_click(object sender, System.EventArgs e) timer1.start(); private void button2_click(object sender, System.EventArgs e) timer1.stop(); 23
C#.end 24