Controlling the development of user control using C #
Southwest Institute of Communications Wanghampo
01-9-7 01:21:57
--------------------------------------------------------------------------------
C # is a new generation of Microsoft's programming language, used to develop the applications under the. NET Framework. C # is evolving from C + +, but simpler and more secure than C + +. Below, we demonstrate its functionality by developing a percentage control in C #.
First, prepare the development environment, start the Visual Studio.NET Beta2, select the menu file-> new-> project, project type Select Visual C # project, template select Windows Control Library, name input percent, and set up a project as determined.
Switch to the percent.cs[design page and change UserControl1.cs to Percent.cs in Solution Explorer. Drag a label from the toolbox to control the design panel, set the properties of the label: name:lbpercent,backcolor:transparent,text:100%, and drag the label's border so that it can display just the Text. Right-click on the design panel, select View Code from the pop-up menu, and open the Code editing page.
You can see that the namespace is Percent, move the cursor below the public class Percent:System.Windows.Forms.UserControl, and enter the following lines:
private int icurpercent = 0;
Private Color clpercent = Color.Blue;
Private Color cltext = Color.Black;
The above definition and initialization of several private variables, icurpercent represents the current percentage progress, clpercent as the color of the progress, cltext the color of the percentage text displayed in the middle.
This control has three properties, setting the display color of the progress bar and text, and the current progress. A Onpercentchange event is also defined that triggers the event when the percentage changes.
After protected override void Dispose (bool disposing), add the following:
public delegate void Percentchangehandler (int currentpercent);
public event Percentchangehandler Onpercentchange;
public int Currentpercent
{
Get
{
return icurpercent;
}
Set
{
if ((Value <=) && (value >= 0))
{
Icurpercent = value;
if (Onpercentchange!= null)
Onpercentchange (icurpercent);
This. Invalidate ();
}
}
}
Public Color Percentbackcolor
{
Get
{
return clpercent;
}
Set
{
Clpercent = value;
This. Invalidate ();
}
}
Public Color Percenttextcolor
{
Get
{
return cltext;
}
Set
{
Cltext = value;
This. Invalidate ();
}
}
The above first defines the event and triggers when the percentage changes. Then the implementation of the three properties.
Below, you need to change the display state of the percent control in the paint event. Switch to the design page, select the entire design panel, move the cursor to paint in the Properties view, press ENTER, and automatically create a frame for the paint event in the code page, where one argument is System.Windows.Forms.PaintEventArgs E, You can do a lot of things with this parameter. First, enter the following code for the percentage control boundary:
Pen penblack = new Pen (color.black, 1);
Point Ptstart = new Point (0, 0);
Point ptend = new Point (this. Width-1, 0);
E.graphics.drawline (Penblack, Ptstart, ptend);
Ptstart = new Point (0, 0);
Ptend = new Point (0, this. Height);
E.graphics.drawline (Penblack, Ptstart, ptend);
Pen penwhite = new Pen (color.white, 1);
Ptstart = new Point (this. Width-1, 0);
Ptend = new Point (this. Width-1, this. Height);
E.graphics.drawline (Penwhite, Ptstart, ptend);
Ptstart = new Point (0, this. HEIGHT-1);
Ptend = new Point (this. Width, this. HEIGHT-1);
E.graphics.drawline (Penwhite, Ptstart, ptend);
In the control size changes should also be refreshed, in the design page, the cursor moved to resize, carriage return, in the Resize event write: This.invalidate ();
In this way, this percentage control is finished. Below, we are writing a test program to test this control. Select menu: File-> Add Project-> new project, Project type Select Visual C # project, template Select Windows application, named Testpercent, set up a Windows application framework as determined. Because you added a new project, there are two projects in Solution Explorer: percent and Testpercent. Right-click the mouse on the testpercent, select Add Reference from the pop-up menu, switch to the project page, the project name should be "Percent", double-click the item name, add it to the selected component, and press OK. In this way, the percent component is added to the test project. Use VC #import instruction or VB reference person to this operation may feel more cordial.
Find percent control from the toolbox, drag it to the design panel of the test project, and drag a button and a timer from the toolbox. Double-click the button to write in its event: Timer1. Enabled = true; Then go back to the design panel, double-click the timer, and write it down in its event: Percent1. Currentpercent = Percent1. Currentpercent + 1; Move the focus to the user control Percent1, locate the event Onpercentchange, enter, and write in the program frame: this. Text = Currentpercent.tostring (); In this way, the test program is complete.
The following runs the program, will testpercent set as the startup project, runs, presses the Button1, can see the progress to increase at the same time, the window title also changes.
With this simple user-controlled writing, we can realize the simplicity of C # programming and have a basic understanding of its development environment. This control can also be easily used by other development tools, such as Visual Basic.NET.
The above program is compiled in C366, 128MB, Windows2000 Advanced Server Sp2,visual Studio.NET Beta2 Chinese version.
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