C # dynamically create controls and event handlers

Source: Internet
Author: User
ArticleDirectory
    • C # dynamically create controls and event handlers
C # dynamically create controls and Event Processing Program

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;

Namespace miner
{
///
/// Summary des cription for form1.
///
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. Panel Panel1;
///
/// Required designer variable.
///
///

Private button [] n = new button [100];
Private int [] kN = new int [100];

Private system. componentmodel. Container components = NULL;

Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent call
//
}

///
/// Clean up any resources being used.
///
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# region windows Form Designer generated code
// required method for designer support-do not modify
// The contents this method with the code editor.
//
private void initializecomponent ()
{< br> This. panel1 = new system. windows. forms. panel ();
This. suspendlayout ();
// Panel1
//
This. panel1.location = new system. drawing. point (8, 8);
This. panel1.name = "Panel1";
This. panel1.size = new system. drawing. size (400,400);
This. panel1.tabindex = 0;
// form1
//
This. autoscalebasesize = new system. drawing. size (6, 14);
This. backcolor = system. drawing. color. white;
This. clientsize = new system. drawing. size (416,413);
This. controls. addrange (new system. windows. forms. control [] {
This. panel1});
This. name = "form1";
This. TEXT = "form1";
This. load + = new system. eventhandler (this. form1_load);
This. resumelayout (false);

}
# Endregion

///
/// The main entry point for the application.
///
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}

Private void form1_load (Object sender, system. eventargs E)
{
Int A = 0;
Int x = 0, y = 0;
For (A = 0; A <= 99; A ++)
{
N [a] = new button ();
N [A]. backcolor = color. White;
N [A]. flatstyle = flatstyle. Flat;
N [A]. width = panel1.width/10;
N [A]. Left = x * n [A]. width;
N [A]. Height = panel1.height/10;
N [A]. Top = y * n [A]. height;
N [A]. Name = "B" +;
Panel1.controls. Add (N [a]);
Panel1.controls [A]. mousedown + = new mouseeventhandler (this. buttonarray_onclick );

X + = 1;
If (x = 10)
{
X = 0;
Y + = 1;
}
}

}

private void buttonarray_onclick (Object sender, mouseeventargs e)
{< br> mouseeventargs Arg = (mouseeventargs) E;
button b1 = (button) sender;
If (Arg. button = mousebuttons. right)
b1.backcolor = color. white;
else
{< br> // b1.backcolor = color. white;
b1.image = image. fromfile ("f :\\ My Documents \ my pictures \ elements \ regular_smile.gif ");
}< br>
}< BR >}

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.