C # Window Programming (2)

Source: Internet
Author: User

A program that applies button controls, including button creation and event processing.

The sample code is as follows: (A Message prompt box is displayed when you click the button)

Using system. Windows. forms;
Using system;
Class form1: Form
{
Private button button1; // defines a button variable button1
Public form1 ()
{
Initializecomponent ();
}
Public void initializecomponent ()
{
// Set window properties
This. Text = "this is my window"; // window name
This. size = new system. Drawing. Size (500,450); // window size, 500 in width, 450 in height
This. maximizebox = false; // disable the window maximization button.
This. backcolor = system. Drawing. color. fromargb (192,255,192); // the background color of the window.
This. startposition = system. Windows. Forms. formstartposition. centerscreen; // the initial position of the window in the middle of the screen.

// Create button
Button1 = new button ();
Button1.text = "OK"; // button name
Button1.location = new system. Drawing. Point (this. Size. Width-80, 10); // The Position of the button in the window
Button1.size = new system. Drawing. Size (60, 30); // button width and height
Button1.click + = new eventhandler (button#click); // associate the button and event handler

// Use the visual style. The button background color does not change with the window background color. Use your own visual style background.
// That is, the application enables the visual style enablevisualstyles, which is valid.
Button1.usevisualstylebackcolor = true;

// Add a button to the window (join)
This. Controls. Add (button1 );

}

// Click the event processing function
Private void button#click (Object sender, eventargs E)
{
// Simple message prompt
MessageBox. Show ("You clicked this button! ");
}

}

Class Program
{
Static void main ()
{
// Enable the visual style, which means that the control style is changed according to the desktop theme, otherwise it is the classic control style.
Application. enablevisualstyles ();
Application. Run (New form1 (); // The form1 object of the new derived class
}
}

 

The Form class represents the window, so the button is the button class. I will not talk about anything else. I just want to talk about how the event is handled.

Look at this sentence: button1.click + = new eventhandler (button#click );

Click Is a delegate variable in the button class.The definition of this delegate is as follows:

Public Delegate void eventhandler (Object sender, eventargs E );

If you do not know anything about delegation, you can refer to: http://hi.baidu.com/3582077/blog/item/bd81b61f06528015304e15b3.html

Let's look at a simple example of an event:

Public Delegate void eventhandler (Object sender, eventargs E );
Public class button
{
Public event eventhandler click;

Protected void onclick (eventargs E)
{
If (Click! = NULL) Click (this, e );
}
}

Click (this, e );The first parameter is this, which corresponds to the object. This is the base class of all classes. If you do not customize the class, by default, classes are derived from objects. So about the event processing function: void button#click (Object sender, eventargs
E)
The first parameter can be converted to the button type. See the following code:

Void button#click (Object sender, eventargs E)

{

Button BTN = (button) sender;
String STR = "the name of the button you clicked is :";
STR + = BTN. text;
MessageBox. Show (STR );

}

In this way, you can determine the specific control event based on the sender..

The delegate defined by the event. There is an event keyword before the variable. This is the identifier of the event. It is also available before the click variable. After the event is modified, the click variable cannot be assigned directly, you can only use + = to assign values. For details about this usage, refer to multicast delegation in C # syntax (3.

 

Well, I have a rough understanding of these basics. Next I will learn the basic applications of various controls. In the above example, the code is hand-played. This time, we will create a "Windows form application". For some complicated code, let the compiler help us generate it.

Combo box

Create a new project. The project type is "Windows Forms Application". After confirming, enter the window editing area and drag a combo control (Name: combox1) to the window ),

Drag a button control.

Add the item text to the combo box in the initializecomponent function:

Combobox1.items. Add ("1111111 ");

Combobox1.items. Add ("2222222 ");

Combobox1.items. Add ("3333333 ");

Combobox1.items. Add ("4444444 ");

Double-click the "button" control and add and click event processing. The Code is as follows:

Private void button#click (Object sender, eventargs E)
{
// Combobox1.text stores the selected item text
MessageBox. Show (combobox1.text );
}

 

Checkbox check box Control
The checkbox class has a bool type variable checked, which is used to determine whether to select a box.
Sample Code:
String STR = "selected: \ n ";
If (checkbox1.checked)
STR + = checkbox1.text;
If (checkbox2.checked)
{
STR + = "\ n ";
STR + = checkbox2.text;
}
MessageBox. Show (STR );

 

Textbox text box and picturebox image control, and openfiledialog file opening dialog box

Here is a simple application for opening images. You can use the picturebox control to display images and use the openfiledialog dialog box to browse images,
The selected image path is displayed in textbox.
The picturebox class has an attribute variable image of the system. Drawing. Image type. This variable determines the image to be displayed by the picturebox control, similar to the "handle" of C ++ ".

The image class has a fromfile function that can load image files on disks.

Let's look at the Code directly. Every key code has a comment.

Add textbox, picturebox, And button controls to the window. The code for clicking the event processing function on the button is:

Private void button1_click_1 (Object sender, eventargs E)
{
Openfiledialog openflg = new openfiledialog ();
// Filter file types, which are the types of files that can be opened. The format is the same as that in the MFC dialog box.
Openflg. Filter = "jpg image | *. jpg | BMP bitmap | *. BMP ";

// If you press the OK button in the open file dialog box
If (openflg. showdialog () = dialogresult. OK)
{
Textbox1.text = openflg. filename; // obtain the path. The path is displayed in the text box.
// Load the image to the picturebox Control
Picturebox1.image = image. fromfile (openflg. filename );
}
}

If you want to learn more about the openfiledialog dialog box, go to msdn and check it. I don't know it here. Well, I only want to talk about the key part. Let's do some other work.

Program running diagram:

In addition:The picturebox class has a sizemode attribute variable.This specifies the image display mode. Normally, when the image size exceeds the control, it is not displayed.

If you set sizemode to stretchimage, you can zoom in and out the image, which is the same as the control size.

That is, this. picturebox1.sizemode = system. Windows. Forms. pictureboxsizemode. stretchimage;

Pictureboxsizemode is an enumeration type. For details, refer to msdn

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.