To get started, declare the two widgets you need, a label to hold the Hello World text and a button to exit the application: private System...

Linki


» Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.
»
3 + do granic pola eksploatacyjnego Przewietrzanie system Z 4 + + + od granic pola eksploatacyjnego Przewietrzanie...
»
2010-03-01 01:00 14636 11010 Oprogramowanie Systemw Pomiarowych\OSP\KursyNatInst\KursyNatInst\2012_LV Core 3\2012_LV Core 3_Solutions_1\Exercise...
»
2010-02-15 01:00 17262 7324 Oprogramowanie Systemw Pomiarowych\OSP\KursyNatInst\KursyNatInst\2012_LV Core 3\2012_LV Core 3_Exercises\Demonstrations\TLC...
»
Częstotliwość przeprowadzania badań marketingowychBadania ciągłe (stałe)Badania ciągłe (stałe) są prowadzone systematycznie...
»
Drugi element, ktry bdzie mia wpyw na polsk polityk celn, to system preferencji handlowych, udzielanych przez Uni Europejsk krajom Afryki, Karaibw i Pacyfiku...
»
System znany dziś jako Wiedza Tajemna istniał początkowo nic posiadając nazwy, przekazywany z pokolenia na pokolenie przez długie wieki...
»
przykładów, żeby uzasadnić tezę, iż tworzenie modeli różnych systemów oraz ich badanie przy użyciu technik komputerowej symulacji – to ważny...
»
Po trzecie, rola partii maych moe wzrosn wwczas, gdy system partyjny ulega zmianom, co najczciej wie si z modyfikacj (rzadziej rewolucyjnymi"...
»
DOS, PAKUJenter - przesuwa kursor systemowy pomidzy przecznikami a listwopcji, oraz dokonuje zatwierdzenia wybranej opcji,strzaki prawo/lewo -...
»
ły kontrolę nad małymi producentami energii, miało chrapkę naprywatyzowane systemy wodne w Afryce, Ameryce Łacińskiej i naBliskim Wschodzie...

Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.

Windows.Forms.Label lblOutput;
private System.Windows.Forms.Button btnCancel;
You're now ready to instantiate these objects, which you do in your Form's constructor: this.lblOutput = new System.Windows.Forms.Label( );
this.btnCancel = new System.Windows.Forms.Button( );
Next you can set the Form's title text to Hello World:
this.Text = "Hello World";
Note that the preceding statements appear in your form's constructor,
HandDrawnClass, and so the this keyword refers to the form itself.


Set the label's location, text, and size:
lblOutput.Location = new System.Drawing.Point (16, 24);
lblOutput.Text = "Hello World!";
lblOutput.Size = new System.Drawing.Size (216, 24);
The location is expressed as a System.Drawing.Point object, whose constructor takes a horizontal and vertical position. The size is set with a Size object, whose constructor takes a pair of integers that represent the width and height of the object.
The .NET Framework provides the System.Drawing object, which
encapsulates the Win32 GID+ graphics functions. Much of the .NET
Framework Class Library ( FCL) consists of classes that encapsulate Win32
methods as objects.

Next, do the same for the button object, setting its location, size, and text: btnCancel.Location = new System.Drawing.Point (150,200);
btnCancel.Size = new System.Drawing.Size (112, 32);
btnCancel.Text = "&Cancel";

page 258
Programming C#
The button also needs an event handler. As described in Chapter 12, events (in this case the cancel button-click event) are implemented using delegates. The publishing class (button) defines a delegate (System.EventHandler) that the subscribing class (your form) must implement.
The delegated method can have any name but must return void and take two parameters: an object (sender) and a SystemEventArgs object, typically named e:
protected void btnCancel_Click (
object sender, System.EventArgs e)
{
//...
}
You register your event handler method in two steps. First you create a new System.EventHandler delegate, passing in the name of your method as a parameter:
new System.EventHandler (this.btnCancel_Click);
You then add that delegate to the button's click event handler list with the += operator.
The following line combines these steps into one:
one:btnCancel.Click +=
new System.EventHandler (this.btnCancel_Click);
Now you must set up the form's dimensions. The form property AutoScaleBaseSize sets the base size used at display time to compute the scaling factor for the form. The ClientSize property sets the size of the form's client area, which is the size of the form excluding borders and titlebar (when you use the designer, these values are provided for you interactively):
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.ClientSize = new System.Drawing.Size (300, 300);
Finally, remember to add the widgets to the form:
this.Controls.Add (this.btnCancel);
this.Controls.Add (this.lblOutput);
Having registered the event handler, you must supply the implementation. For this example, clicking Cancel will exit the application, using the static method Exit( ) of the Application class:
protected void btnCancel_Click (
object sender, System.EventArgs e)
{
Application.Exit ( );
}
That's it; you just need an entry point to invoke the constructor on the form: public static void Main( )
{
Application.Run(new HandDrawnClass( ));
}

page 259
Programming C#
The complete source is shown in Example 13-1. When you run this application, the window is opened and the text is displayed. Pressing Cancel closes the application.
Example 13-1. Creating a hand-drawn Windows Form
using System;
using System.Windows.Forms;

namespace ProgCSharp
{
public class HandDrawnClass : Form
{
// a label to display Hello World
private System.Windows.Forms.Label
lblOutput;

// a cancel button
private System.Windows.Forms.Button
btnCancel;

public HandDrawnClass( )
{
// create the objects
this.lblOutput =
new System.Windows.Forms.Label ( );
this.btnCancel =
new System.Windows.Forms.Button ( );

// set the form's title
this.Text = "Hello World";

// set up the output label
lblOutput.Location =
new System.Drawing.Point (16, 24);
lblOutput.Text = "Hello World!";
lblOutput.Size =
new System.Drawing.Size (216, 24);

// set up the cancel button
btnCancel.Location =
new System.Drawing.Point (150,200);
btnCancel.Size =
new System.Drawing.Size (112, 32);
btnCancel.Text = "&Cancel";

// set up the event handler
btnCancel.Click +=
new System.EventHandler (this.btnCancel_Click);

// Add the controls and set the client area
this.AutoScaleBaseSize =
new System.Drawing.Size (5, 13);
this.ClientSize =
new System.Drawing.Size (300, 300);
this.Controls.Add (this.btnCancel);
this.Controls.Add (this.lblOutput);

}

// handle the cancel event
protected void btnCancel_Click (
object sender, System.EventArgs e)
{
Application.Exit( );

page 260
Programming C#
}

// Run the app
public static void Main( )
{
Application.Run(new HandDrawnClass( ));
}
}
}
13.1.2 Using the Visual Studio.Net Designer
Although hand coding is always great fun, it is also a lot of work, and the result in the previous example is not as elegant as most programmers would expect. The Visual Studio IDE provides a design tool for Windows Forms that is much easier to use.

Powered by MyScript