NET User Interfaces in Csharp Windows Forms and Custom Controls [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

NET User Interfaces in Csharp Windows Forms and Custom Controls [Electronic resources] - نسخه متنی

Matthew MacDonald

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید













































The Control Class



Chapter 2 introduced .NET control classes, and examined their place in the overall architecture of an application. To summarize:



You create and manipulate controls and forms using .NET classes. The Common Language Runtime recognizes these classes, and handles the low-level Windows details for you.



You use a control from the .NET class library by creating an instance of the appropriate class, and adding it to the Controls collection of a container control, like a panel or form. Whether you add the control at design-time or run-time, the task is the same.



You configure controls by setting properties. In addition, you can react to control events in two ways: by creating an event handler (typically in the Form class), or by deriving a custom control and overriding the corresponding method.



Every .NET control derives from the base class System.Windows.Forms.Control. Depending on the complexity of the control, it may pass through a few more stages in its evolution. Figure 3-1 shows the basic hierarchy of controls.




Figure 3-1: Control hierarchy


On its own, the Control class has no real purpose. It's mainly interesting for the basic functionality that it defines. Sorting through the functionality is no easy task. The 200+ members include countless properties, events that fire to notify you when most common properties are changed (like VisibleChanged, TextChanged, SizeChanged, and so on), and methods that reset values to their defaults, along with some more useful and unusual members. The sections in this chapter sort through many of the properties under broad topic-specific headings like "color" and "focus." Before you begin your exploration, you may want to check out some of the basic and system-related members in Table 3-1.













Table 3-1: Basic Control Members


MemberDescription










Name



Provides a short string of descriptive text that identifies your control. Usually (and by default, if you are using Visual Studio .NET), this is the same as the name of the form-level member variable that refers to the control. However, there's no direct relation; the Name property is just provided to help you when iterating through a control collection looking for a specific item.










Tag



Provides a convenient place to store an object. The Tag property is not used by the .NET framework. Instead, you use it to store associated data (like a data object or a unique ID). It's particularly useful when dealing with list controls. Unlike the Tag property in previous versions of Visual Basic, it can store any type of information or even a full-fledged object.










Controls and ControlAdded and ControlRemoved events



The Controls collection stores references to all child controls. You can use the associated events to automate layout logic, as you'll see in Chapter 11.










DesignMode



Returns true if the control is in design mode. It's useful when you are deriving or creating a custom control, so you don't perform time-consuming or system-endangering operations when the program is not running (like an automatic refresh).










Invoke() and InvokeRequired



These members are used in multithreaded programming. InvokeRequired returns true if the control is hosted on a different thread than the current thread of execution. In this case, you should not attempt to use the control directly. Instead, place the code that manipulates the control in a separate method, and pass a delegate that points to this method to the Control.Invoke() method. This ensures that your code will be marshaled to the correct thread.










Dispose()



This method, which is called automatically by the .NET framework as part of the form infrastructure, releases the resources held by a control (like the operating system window handle).







Because every control is derived from the Control class, you can always use it as a lowest common denominator for dealing with some basic Control properties in your application. For example, consider the form below that provides a text box, label, and button control. You'll find this example in the online samples as the ControlMedley project (see Figure 3-2).




Figure 3-2: A medley of different controls


The Click event for all these controls (and the underlying form) is handled by one event handler. Using Visual Studio .NET, you can configure event handlers through the Properties window, or enter the code directly:



this.Button1.Click += new System.EventHandler(this.ctrlClick);
this.TextBox1.Click += new System.EventHandler(this.ctrlClick);
this.Label1.Click += new System.EventHandler(this.ctrlClick);


The event handler code is generic: it converts the object reference of the sender into the control type, and then displays a message with the name of the clicked control.



private void ctrlClick(System.Object sender, EventArgs e)
{
Control ctrl = (Control)sender;
MessageBox.Show("You clicked: " + ctrl.Name);
}


This is one of the ways that you can replace control arrays, which were a Visual Basic 6 standby. In fact, this technique is more powerful than control arrays because it allows you to handle similar events from any type of control, rather than limiting you to one type of control (e.g., a Button) and one type of event (e.g., Button.Click).




/ 142