}
Следующий фрагмент кода посвящён разработке составного элемента управления, представляющего собой “батарею” из 16 одинаковых кнопочек, располагаемых на одной панели. Несмотря на внушительное количество составляющих элементов, перед нами один элемент управления с общим единым обработчиком события, связанного с нажатием на одну из 16 кнопочек. Естественно, что реализация единого обработчика требует дополнительных усилий: каждой кнопке назначается один и тот же стандартный обработчик с двумя параметрами. Один из которых используется для идентификации нажатой кнопки. При нажатии на кнопку происходит изменение состояния этой кнопки, которое кодируется посредством текстового значения (свойства Text), отображаемого на поверхности кнпки.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace By2
{
/// <summary>
/// Summary description for ButtonsControl.
/// </summary>
public class ButtonsControl : System.Windows.Forms.UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private Form1 f1;
private ArrayList bArrList;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button16;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
public ArrayList parameterList;
public void SetParent(Form1 fKey)
{
f1 = fKey;
}
private System.ComponentModel.Container components = null;
public ButtonsControl()
{
InitializeComponent();
bArrList = new ArrayList();
bArrList.Add(button16);
bArrList.Add(button15);
bArrList.Add(button14);
bArrList.Add(button13);
bArrList.Add(button12);
bArrList.Add(button11);
bArrList.Add(button10);
bArrList.Add(button9);
bArrList.Add(button8);
bArrList.Add(button7);
bArrList.Add(button6);
bArrList.Add(button5);
bArrList.Add(button4);
bArrList.Add(button3);
bArrList.Add(button2);
bArrList.Add(button1);
parameterList = new ArrayList();
}
private void SetParameterList()
{
int i, I = bArrList.Count ;
parameterList.Clear();
for (i = 0; i < I; i++)
{
parameterList.Add(int.Parse(((Button)bArrList[i]).Text));
}
f1.formDecimalValue(parameterList);
}
public void SetButtons(ArrayList lst)
{
int i, I = lst.Count, J = bArrList.Count;
for (i = 0; i < J; i++)
{
((Button)bArrList[i]).Text = "0";
}
for (i = 0; i < I; i++)
{
((Button)bArrList[i]).Text = ((int)lst[i]).ToString();
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.button16 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Controls.Add(this.button16);
this.panel1.Controls.Add(this.button15);
this.panel1.Controls.Add(this.button14);
this.panel1.Controls.Add(this.button13);
this.panel1.Controls.Add(this.button12);
this.panel1.Controls.Add(this.button11);
this.panel1.Controls.Add(this.button10);
this.panel1.Controls.Add(this.button9);
this.panel1.Controls.Add(this.button8);
this.panel1.Controls.Add(this.button7);
this.panel1.Controls.Add(this.button6);
this.panel1.Controls.Add(this.button5);
this.panel1.Controls.Add(this.button4);
this.panel1.Controls.Add(this.button3);
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(8, 8);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(648, 48);
this.panel1.TabIndex = 0;
//
// button16
//
this.button16.Location = new System.Drawing.Point(605, 12);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(32, 23);
this.button16.TabIndex = 146;
this.button16.Text = "0";
this.button16.Click += new System.EventHandler(this.buttonX_Click);
//
// button15
//
this.button15.Location = new System.Drawing.Point(565, 12);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(32, 23);
this.button15.TabIndex = 145;
this.button15.Text = "0";
this.button15.Click += new System.EventHandler(this.buttonX_Click);
//
// button14
//
this.button14.Location = new System.Drawing.Point(525, 12);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(32, 23);
this.button14.TabIndex = 144;
this.button14.Text = "0";
this.button14.Click += new System.EventHandler(this.buttonX_Click);
//
// button13
//
this.button13.Location = new System.Drawing.Point(485, 12);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(32, 23);
this.button13.TabIndex = 143;
this.button13.Text = "0";
this.button13.Click += new System.EventHandler(this.buttonX_Click);
//
// button12
//
this.button12.Location = new System.Drawing.Point(445, 12);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(32, 23);
this.button12.TabIndex = 142;
this.button12.Text = "0";
this.button12.Click += new System.EventHandler(this.buttonX_Click);
//
// button11
//
this.button11.Location = new System.Drawing.Point(405, 12);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(32, 23);
this.button11.TabIndex = 141;
this.button11.Text = "0";
this.button11.Click += new System.EventHandler(this.buttonX_Click);
//
// button10
//
this.button10.Location = new System.Drawing.Point(365, 12);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(32, 23);
this.button10.TabIndex = 140;
this.button10.Text = "0";
this.button10.Click += new System.EventHandler(this.buttonX_Click);
//
// button9
//
this.button9.Location = new System.Drawing.Point(325, 12);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(32, 23);
this.button9.TabIndex = 139;
this.button9.Text = "0";
this.button9.Click += new System.EventHandler(this.buttonX_Click);
//
// button8
//
this.button8.Location = new System.Drawing.Point(285, 12);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(32, 23);
this.button8.TabIndex = 138;
this.button8.Text = "0";
this.button8.Click += new System.EventHandler(this.buttonX_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(245, 12);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(32, 23);
this.button7.TabIndex = 137;
this.button7.Text = "0";
this.button7.Click += new System.EventHandler(this.buttonX_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(205, 12);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(32, 23);
this.button6.TabIndex = 136;
this.button6.Text = "0";
this.button6.Click += new System.EventHandler(this.buttonX_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(165, 12);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(32, 23);
this.button5.TabIndex = 135;
this.button5.Text = "0";
this.button5.Click += new System.EventHandler(this.buttonX_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(125, 12);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(32, 23);
this.button4.TabIndex = 134;
this.button4.Text = "0";
this.button4.Click += new System.EventHandler(this.buttonX_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(85, 12);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(32, 23);
this.button3.TabIndex = 133;
this.button3.Text = "0";
this.button3.Click += new System.EventHandler(this.buttonX_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(45, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(32, 23);
this.button2.TabIndex = 132;
this.button2.Text = "0";
this.button2.Click += new System.EventHandler(this.buttonX_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(5, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(32, 23);
this.button1.TabIndex = 131;
this.button1.Text = "0";
this.button1.Click += new System.EventHandler(this.buttonX_Click);
//
// ButtonsControl
//
this.Controls.Add(this.panel1);
this.Name = "ButtonsControl";
this.Size = new System.Drawing.Size(664, 64);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void buttonX_Click(object sender, System.EventArgs e)
{
if (((Button)sender).Text.Equals("0")) ((Button)sender).Text = "1";
else ((Button)sender).Text = "0";
SetParameterList();
}
}
}
Литература
1. Петзольд Чарльз Программирование для Microsoft Windows на С#
В 2-х томах
2002 г., Русская редакция
2. Петзольд Чарльз Программирование в тональности С#
512 стр., 2004 г., Русская редакция
3. Эндрю Троелсен C# и платформа .NET. Библиотека программиста
796 стр., 2004 г., Питер. Серия: Библиотека программиста.
4. Герберт Шилдт C#. Учебный курс
511 стр., 2002 г., Питер.
5. Герберт Шилдт Полный справочник по C#
752 стр., 2004 г., Вильямс.
6. Джеффри Рихтер Программирование на платформе Microsoft .NET Framework
512 стр., 2005 г., Питер, Русская редакция.
7. В. Жарков Самоучитель Жаркова по анимации и мультипликации в Visual С# .NET
432 стр., 2004 г., Жарков Пресс.
8. Джесс Либерти Программирование на C#
688 стр., 2003 г., Символ-Плюс.
9. Александр Фролов, Григорий Фролов Язык C#. Самоучитель
560 стр., 2002 г., Диалог-МИФИ.
10. Майо Джозеф C#. Искусство программирования
656 стр., 2002 г., ДиаСофт.
11. У. Робисон C# без лишних слов
352 стр., 2002 г., ДМК.
12. Микелсен К. Язык программирования C#. Лекции и упражнения
656 стр., 2002 г., ДиаСофт
13. Разработка WINDOWS-приложений на Microsoft Visual Basic.NET и Microsoft Visual C# .NET. Учебный курс.
512 стр., Русская Редакция
14. Библиотека MSDN на русском языке
http://msdn.microsoft.com/library/rus/