[개체 배열]
[개요] 알파벳 버튼을 누르면 새 창이 뜨고 그 창에 무슨 버튼을 눌렀는지 나오게 만들어라. 버튼은 배열을 사용하여 만들 것.[ Source ]using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;
namespace array2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
///
Button[] objButton; // 버튼 배열 선언
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <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 Windows Form 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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(416, 309);
this.Name = "Form1";
this.Text = "알파벳 출력기";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
// 버튼에 들어갈 알파벳
char[] arrButton = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X'};
// 버튼을 24개 만듦
objButton=new Button[arrButton.Length];
int x = 0, y = 0;
for ( int j = 0 ; j < 3; j++ )
{
x = 0;
for ( int i = 0 ; i < 8; i++ )
{
x = 20 + ( i * 42 ); // 버튼이 만들어질 x,y 좌표
y = 30 + ( j * 52 ); // 규칙성 있게 x,y를 변화시킴
objButton[i + (8 * j)] = new Button();
objButton[i + (8 * j)].FlatStyle = FlatStyle.Standard;
objButton[i + (8 * j)].Size = new System.Drawing.Size(40,40);
objButton[i + (8 * j)].Location = new System.Drawing.Point(x,y);
objButton[i + (8 * j)].Name = Convert.ToString(arrButton[i + (8*j)]);
objButton[i + (8 * j)].Text = Convert.ToString(arrButton[i + (8*j)]);
objButton[i + (8 * j)].Click += new System.EventHandler(this.Button_Click);
// 위의 속성대로 버튼이 추가된다.
this.Controls.Add(objButton[i + (8 * j)]);
}
}
}
// 버튼 클릭 이벤트
private void Button_Click(object sender, System.EventArgs e)
{
MessageBox.Show("클릭하신 버튼은 " + ((Button)sender).Text + " 입니다.","이런 ㅂㅂㅂ");
}
}
}
댓글
댓글 쓰기