C # 21 creating MDI Application and Component development

Source: Internet
Author: User
Tags delete key

Introduction to MDI Applications

In Vc#, an MDI application consists of two parts, the MDI parent form and the MDI child form.

An MDI application can contain one or more MDI parent forms, and each MDI parent form can contain more than one MDI child form. An MDI parent form can contain all MDI child forms.

In Vc#, an MDI parent form can contain more than one MDI child form, but it cannot be another MDI parent form at the same time.

In other words, a form cannot be both an MDI parent form and an MDI child form.

In the MDI parent form, the MDI child form is displayed as a separate window, and the user interacts with the MDI application through the MDI child window and the MDI parent window. So in vc#, you can integrate multiple windows in an MDI application. To create an MDI parent form at design or run time, you can set the form's IsMdiContainer property to True.

MDI applications support multiple child form instances, so it must know that an operation should be performed on that instance. To get the child form instance that has focus or is recently active, you can use the Activemdichild property.

For example, the following code returns the active MDI child form object:

Form Activechildform=this. Activemdichild;

An MDI application may contain multiple windows or documents, so you need to provide a mechanism for switching between windows or documents. You can enable users to switch between multiple windows or documents by creating menus for MDI applications.

By default, when you open a subform that contains a menu, the menu on the subform is added to the MDI parent form menu and to the end. The fusion of menus can be implemented using the Mergeaction and Mergeindex properties of the ToolStripMenuItem control. By default, the value of the Mergeaction property is append, which means that the menu is fused to the end of the MDI parent form menu.

Layout MDI child forms

An MDI application consists of multiple forms. In some cases, users may need to open multiple subforms at the same time. At this point, the user needs to arrange the MDI form properly so that multiple subforms can be manipulated at the same time.

Windows applications allow users to open multiple files at once. When working with multiple files, users need to properly layout the files on the screen in order to increase productivity. All Windows-based applications have a menu called window, where options can be used to arrange all open windows.

In order to arrange the subform that is open in the MDI parent form, you need to write the following code:

①this. LayoutMDI (mdilayout.arrangeicons);

All MDI child icons are arranged in the client area of the MDI parent form. As shown in the following:

②this. LayoutMDI (Mdilayout.cascade);

All MDI child windows are stacked in the client area of the MDI parent form. As shown in the following:

③this. LayoutMDI (mdilayout.titlevertical);

All MDI child windows are horizontally stacked in the client area of the MDI parent form. As shown in the following:

④this. LayoutMDI (mdilayout.titlehorizontal);

All MDI child windows are stacked vertically in the client area of the MDI parent form. As shown in the following:

In summary, all MDI applications have a window menu. The menu contains the names of all the child windows that are open. This feature can be integrated into the vc# application through the Windowlist attribute.

Practice

Select title:

What is the default value for the Mergeaction property of the ToolStripMenuItem control?

A:append

B:insert

C:replace

D:remove

Answer: A

Summary

You can use MDI applications to open multiple forms in the same program.

English vocabulary

English Chinese

Mdi:multi-document Interface Multi-Document interface

Practical projects

Create an MDI application and arrange it in four ways as mentioned above.

Introduction to WinForm Controls

We do it ourselves. WinForm controls typically have three types: a composite control (Composite controls), an extended control (Extended controls), and a custom control.

Composite control (User control): Combines existing controls to form a new control that integrates the functionality and characteristics of an existing control. The composite control inherits from the System.Windows.Forms.UserControl class.

Extended control: A new control is derived from the control of an existing control, adding new functionality, attributes, or modifying the functionality, attributes of the original control.

Custom controls: When extended controls and composite controls do not meet our needs, we need to write our own controls. Custom controls inherit from the System.Windows.Forms.Control class. The control class provides all the basic functionality required by the control, including keyboard and mouse event handling. Custom controls are the most customizable, but the requirements for developers are high, you have to write code for the control class's OnPaint event, and you can override the WndProc method of the control class to handle a lower-level windows message. So you need to know a bit about GDI + and Windows API. In general, composite and extended controls are common practice, and we will focus on the development of composite controls and extended controls.

To create a composite control

In this example, we create a simple login box composite control. The control will implement the user name and password automatically: if the user name or password is empty popup "user name or password cannot be blank" prompt, if the user name and password are not blank open the specified form, hide the current form.

First, create the control library

In VS, click New Project, File menu, and the New Project dialog box pops up. In the left "project type", select "Visual C #", select "Windows Control Library" in the right "template", fill in the project name "MyControl", fill in the project location, click "OK" button to create our control library.

Second, create the control

1, right-click on the "MyControl" project in Solution Explorer, then "Add", "User control", pop up the "Add New Item" dialog box, select "User Control", enter "Login" in the Name field click the "Add" button to create a new blank user control.

2, in the layout of the new user control look like.

List of controls to use

Control identity

Description

Btnlogin

Login button

Btncancel

Cancel button

Txtname

User name Input Box

Txtpwd

Password entry box

3. Adding attributes

Private bool _ispassed =true;

Private Form _toform =null;

/// <summary>

/// Determine if the login is successful, if the success value is true, otherwise the value is False

/// </summary>

[Category(" custom Attribute")]

[Description(" determines if the login is successful, if the success value is true, otherwise the value is False")]

Public BOOL Ispassed

{

Get { return _ispassed;}

Set {_ispassed = value;}

}

/// <summary>

/// If the login is successful, the form specified here is opened

/// </summary>

[browsable(false)] // This setting causes the property to not be displayed in the control's property designer

Public Form Toform

{

Get { return _toform;}

Set {_toform = value;}

}

The Ispassed property is only shown here and is not used in code, it is displayed in the control's property designer. The Toform property is not displayed in the control's property designer, but we use it in code to specify the form to display when the login validation succeeds.

4. Add Login button Event method

// Login Button

Private void Btnlogin_click (objectsender,EventArgs e)

{

// if the user name or password is empty

if (String. IsNullOrEmpty (txtname.text) | | String. IsNullOrEmpty (Txtpwd.text))

{

MessageBox. Show (" user name or password cannot be empty!") ");

_ispassed = false; // Logon Failure

}

Else // if the user name and password are not empty

{

if (_toform! = null)

{

_toform.show (); // Open the target form

this. Findform (). Hide ();

}

}

}

5. Add Cancel button event handling

Cancel button

private void Btncancel_click (Objectsender, EventArgs e)

{

Form LoginForm = this. Findform ();

if (loginform! = null)

{

Loginform.close ();

}

}

Right-click on the "MyControl" project, "Build" in Solution Explorer to build our control library. To the time our login control is done. Happy first, oh yes! Oh, yes!

Using composite controls

First, create a Windows application

In VS, click New Project, File menu, and the New Project dialog box pops up. In the left "project type", select "Visual C #", select "Windows Application" in the right "template", fill in the project name "Mywin", fill in the project location, click "OK" button to create our Windows application.

Second, create a form that uses the login control Frmlogin

On the Mywin project in Solution Explorer, right-click Add Windows Forms, pop up the Add New Item dialog box, select Windows Forms, and in the Name field, enter Frmlogin to click the Add button to create the login form.

Third, add the login control to the Toolbox

Right-click the toolbox, select items, pop up the Select Toolbox Items dialog box, click the Browse button, and select the control library that we created to compile the generated DLL file.

Iv. drag and drop the login control from the Toolbox onto the Frmlogin form and declare it to be mylogin,

Add the form frmmain that you want to open after the login verification is successful:

Six, add the following code to the form Load event for Frmlogin:

Private void Frmlogin_load (objectsender,EventArgs e)

{

frmmainfrmmain = new frmmain();

Mylogin.toform = Frmmain; // Set Form Frmmain to open when validation is successful

}

Run our Mywin project. Click the "Login" button to open the Frmmain form, hide the login form, and click the "Cancel" button to close the login form.

To create an extension control

In addition to composite controls, extended controls are also common to us. We'll show you how to develop an extended control by creating a text box that can only enter numbers.

Creating controls

1, right-click on the "MyControl" project in Solution Explorer, then "Add", then "components", pop up the "Add New Item" dialog box, select "Component Class", enter "MyTextBox" in the Name field and click "Add" button to create a new component.

2. Switch to the Code view of the component and modify the inheritance class to TextBox. The code is as follows:

Public Partial class MyTextBox :TextBox

{

Public MyTextBox ()

{

InitializeComponent ();

}

Private void Mytextbox_keypress (object sender,KeyPressEventArgs e)

{

e.handled = true; // block Keyboard Input

if (E.keychar >= ' 0 ' && e.keychar <=' 9 ' | | e.keychar = = (char) 8)

{

e.handled = false; // if it is a number or a delete key, the keyboard input is not blocked

}

}

}

3. Switch to the Design view of the component, call the property designer, and add the "KeyPress" event with the following code:

Private void Mytextbox_keypress (object sender,KeyPressEventArgs e)

{

e.handled = true; // block Keyboard Input

if (E.keychar >= ' 0 ' && e.keychar <=' 9 ' | | e.keychar = = (char) 8)

{

e.handled = false; // if it is a number or a delete key, the keyboard input is not blocked

}

}

4. Compile and build our controls.

Using extended controls

First, create a form that uses the login control Frmtextbox

On the Mywin project in Solution Explorer, right-click Add Windows Forms, pop up the Add New Item dialog box, select Windows Forms, and in the Name field, enter Frmtextbox to click the Add button to create the form.

Second, add the control to the Toolbox

Right-click the toolbox, select items, pop up the Select Toolbox Items dialog box, click the Browse button, and select the control library that we created to compile the generated DLL file.

Drag and drop a numeric text box control from the Toolbox onto the Frmtextbox form to run the application

Practical issues

Select title:

1. Which class does the composite control inherit from? __________

A. System.Windows.Forms.Form B. System.Windows.Forms.Object

C. System.Windows.Forms.UserControl D. System.Windows.Form.UserControl

2. What types of WinForm controls do we develop ourselves __________

A. Handwriting control B. Extended controls

C. Composite controls D. Custom controls

Summary

In this chapter, we have mainly studied:

U Composite Control

U Extended Control

U using composite controls

Practice Items

1. Make a text box control that allows you to enter only letters.

2. Do a QQ login composite control.

C # 21 creating MDI Application and Component development

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.