概要
cscの作法、調べてみた。
練習問題、やってみた。
練習問題
ComboBoxを配列で扱え。
方針
- new ComboBox[5]する。
写真
サンプルコード
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Data;
namespace App
{
public partial class Form1: Form {
ComboBox[] comboBoxs;
public Form1() {
Text = "ComboBox";
ClientSize = new Size(300, 300);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("ID", typeof(int)),
new DataColumn("Obj", typeof(string))
});
dt.Rows.Add(0, "");
dt.Rows.Add(1, "バナナ");
dt.Rows.Add(2, "マンゴー");
dt.Rows.Add(3, "パパイア");
dt.Rows.Add(4, "キウイ");
comboBoxs = new ComboBox[5];
for (int i = 0; i < this.comboBoxs.Length; i++)
{
comboBoxs[i] = new ComboBox();
comboBoxs[i].Name = "ComboBox" + i.ToString();
comboBoxs[i].Location = new Point(30, 10 + 30 * i);
comboBoxs[i].DataSource = new BindingSource(dt, null);
comboBoxs[i].DisplayMember = "Obj";
comboBoxs[i].ValueMember = "ID";
comboBoxs[i].SelectedIndex = -1;
}
Controls.AddRange(this.comboBoxs);
Button button1 = new Button();
button1.Location = new Point(100, 250);
button1.Text = "test";
button1.Click += new EventHandler(button1_Click);
this.Controls.Add(button1);
}
private void button1_Click(object sender, EventArgs e) {
string str = "0: " + comboBoxs[0].Text + " 1: " + comboBoxs[1].Text + " 2: " + comboBoxs[2].Text + " 3: " + comboBoxs[3].Text + " 4: " + comboBoxs[4].Text;
MessageBox.Show(str);
}
[STAThread]
public static void Main() {
Application.Run(new Form1());
}
}
}
以上。