2004.7.15 [C # learning record] Use C # to write a custom control (master is not in, haha) Griefforyou [original]
Using system.collections; using system.drawing; using system.data; using system.windows.forms; namespace windowscontrollibrary1 {///
Summary of /// UserControl1. ///
Public class userControl1: system.windows.Forms.userControl {/////
/// The required designer variable. ///
Private system
.Componentmodel
.Container Components
= NULL
;
/// Private domain
Private string mytext
;
// Store the associated event processing method;
Private EventHandler OnmyTextChanged
;
Public UserControl1
(
)
{
/ / This call is required for the Windows.Forms Form Designer.
InitializeComponent
(
)
;
// Todo: Add any initialization after INITIALIZECMPONENT call
Mytext
=
Static Control
;
OnmyTextChanged
= NULL
;
}
///
/// Clean all the resources being used. ///
protected override
Void Dispose
(Bool Disposing
)
{
IF
(Disposing)
)
{
IF
(Components)
!
= NULL
)
Components
.Dispose
(
)
;
}
Base
.Dispose
(Disposing)
)
;
}
#Region Component Designer Generated Code
///
/// Designer supports the required method - Do not use the code editor // / modify the contents of this method. ///
Private
Void InitializationComponent
(
)
{
//
// UserControl1
//
THIS
.Name .name
=
"UserControl1"
;
THIS
.Size
=
New system
.Drawing
.Size
(248
128
)
;
THIS
.Load
=
New system
.Eventhandler
(
THIS
.UserControl1_load
)
;
THIS
.Paint
=
New system
.Windows
.Form
.Painteventhandler
(
THIS
.UserControl1_paint
)
;
}
#ndregion
Private
Void UserControl1_paint
(Object Sender
SYSTEM
.Windows
.Form
.Painteventargs e
)
{
Sizef Textsize
= e
.Graphics
.MeasureString
(MyText
FONT
)
;
Float XPOS
=
(ClientRectangle
.Width
/2
)
-
(Textsize
.Width
/2
)
;
Float ypos
=
(ClientRectangle
.Height / 2
)
-
(Textsize
. Height
/2
)
;
e
.Graphics
.Drawstring
(MyText
FONT
,
New Solidbrush
(Color)
.Black
)
, XPOS
, YPOS
)
;
}
Private
Void UserControl1_Load
(Object Sender
SYSTEM
.Eventargs e
)
{
}
// Open attribute
Public String ControlText
{
get
{
Return mytext
;
}
set
{
Mytext
= Value
;
// Heavy painting
Invalidate
(
)
;
// If an event processing method is associated, the method is called.
IF
(ONMYTEXTCHANGED)
!
= NULL
)
{
OnmyTextChanged
.Invoke
(
THIS
Eventargs
Empty
)
;
}
}
}
// Declare an open event
Public Event athandler mytextchanged
{
Add
{
OnmyTextChanged
= Value
;
}
Remove
{
OnmyTextChanged
= Value
;
}
}
}
}