Creating Top-Notch User Experiences With Windows Forms in Visual Studio .NET
//Windows界面开发就是基于事件驱动的
//wincv(class viewr) 在命令行中输入
//建立新窗体,在属性中改变control的值,可以显示/隐藏(最大、小化按钮)
2.1 Using Controls
n Controls represent items on a form that allow user to manipulate data
n The forms designer allows you to drag and drop controls
n Can set control properties at design time to visually create the GUI
n Common controls: TextBox Label ComboBox ListBox Button CheckBox RadioButton
n Other powerful controls: TreeView ListView TabControl OpenFileDialog SaveDialog
Practice: Using Controls to Indicate Common User Interactions
n True/False Flag: CheckBox
n Single Choice: RadioButtons
contained in a GroupBox
n Select from a list: CheckBox, ListBox, ComboBox
n Accept/Cancel: Buttons
2.2 Dialog Forms
n Dialog Forms will be shown modally using Form.ShowDialog();
n Synchronous block until form is closed
n Clicking OK or Cancel should return different DialogResult
Practice:
n Form.ShowDialog()
Method returns a
DialogResult
enumeration value
n Set forms
DialogResult value
inside button click
event handler
2.3 MDI Forms//多文档接口窗体
n Multiple Document Interface (MDI) forms contain children forms (document/view concept)
n Namesake – multiple documents (child documents) loaded at one time; one active
n Children can be arranged tiled or stacked with Window menu
n All functionality is provided by Microsoft .NET forms
Practice:
n Parent: Form.IsMdiContainer property
n Child: Form.MdiParent property
2.4 Menus and Context Menus//鼠标右键事件
n Use menus to organize functions
n Menu designer
n Add code to event hanlers
n Standard menus: File Edit Help
n Context menu
Practice
n MainMenu control
n ContextMenu control and Form.ContextMenu property
2.5 Event-Driven Code: Events and Delegates
n User clicks button
n Button invokes delegate for the event
n Delegate calls the bound event
n Click event fires
n Your code written in Click event handler runs
n Delegates allow multiple events (all matching signatures) to be handled by one method
n Delegates can be multicast: call multiple event handlers
Practice:
Event Handlers with C#
Private void InitializeComponent()
{
...
this.btGetCustomer.Click += new
System.EventHandler(
this.btGetCustomer_Click);
...
}
Private void btGetCustomer_Click(
object sender, System.EventArgs e)
{
//your code goes here
...
}
2.6 Validation
n Validation needs to be performed at the Control and the Form level
n ErrorProvider control
n Control.CausesValidation property
n Validating event
n Form level validation done in accept button click event
2.7 Use Whichever Language You Feel Most Productive With
n The CLR equalizes functionality of languages
n There are subtle differences between Microsoft Visual C# and Visual Basic (VB) but they’re really negligible: C# has operator overloading, unsafe code, XML Comments; VB can be more verbose
n Editors have slightly different features
n End the disagreement; accept and embrace other languages for what they are
n The CLR and the framework class libraries are really what you need to learn
Session Summary
n Using .NET forms provides for an excellent way to visually create a GUI
n Controls allow you to model a UI using visual elements that are familiar to Windows users
n Writing code is easy with the event-programming model of .NET
n Validation is easy to attain with the Framework
n CLR Makes all languages equal