C# 開発

【C#】ComboBox (1)

2025.01.23 ComboBox : アイテムを表示する

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.Items.Add("NEC");
    comboBox1.Items.Add("SONY");
    comboBox1.Items.Add("DELL");
}

2025.01.23 ComboBox : 配列の内容を設定する

private void Form1_Load(object sender, EventArgs e)
{
    string[] s1 = { "NEC", "SONY", "DELL" };
    comboBox1.Items.AddRange(s1);
}

private void Form1_Load(object sender, EventArgs e)
{
    string[] s1 = { "NEC", "SONY", "DELL" };
    comboBox1.DataSource = s1;
}

2025.01.24 ComboBox : ArrayList の内容を設定する

private void Form1_Load(object sender, EventArgs e)
{
    var al = new ArrayList { "NEC", "SONY", "DELL" };
    comboBox1.Items.AddRange(al.ToArray());
}

private void Form1_Load(object sender, EventArgs e)
{
    var al = new ArrayList { "NEC", "SONY", "DELL" };
    comboBox1.DataSource = al;
}

2025.01.24 ComboBox : List の内容を設定する

private void Form1_Load(object sender, EventArgs e)
{
    var lt = new List { "NEC", "SONY", "DELL" };
    comboBox1.Items.AddRange(lt.ToArray());
}

2025.01.24 ComboBox : 全てのフォント名を設定する

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DataSource = FontFamily.Families;
    comboBox1.DisplayMember = "Name";
}

2025.01.23 ComboBox : アイテムを選択する

private void button1_Click(object sender, EventArgs e)
{
    comboBox1.SelectedItem = "DELL";
}

private void button2_Click(object sender, EventArgs e)
{
    comboBox1.SelectedIndex = 2;
}

2025.01.23 ComboBox : アイテムを挿入する

private void button1_Click(object sender, EventArgs e)
{
    comboBox1.Items.Insert(1, "HP");
}

2025.01.23 ComboBox : アイテム追加を高速に行う

private void button1_Click(object sender, EventArgs e)
{
    // 描画を一時停止
    comboBox1.BeginUpdate();

    foreach (var idx in Enumerable.Range(0, 100000))
    {
        comboBox1.Items.Add(idx.ToString());
    }

    // 描画を更新 (2倍近いパフォーマンス向上)
    comboBox1.EndUpdate();
}

2025.01.23 ComboBox : ドロップダウンリストの幅を変更する

private void button1_Click(object sender, EventArgs e)
{
    string[] s1 = { "NEC", "SONY", "DELL" };
    comboBox1.Items.AddRange(s1);
    comboBox1.DropDownWidth = 200;
}

2025.01.24 ComboBox : オーナードローしたアイテムにフォーカスした際のデザインを描画する

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DrawMode = DrawMode.OwnerDrawFixed;

    string[] s1 = { "NEC", "SONY", "DELL" };
    comboBox1.Items.AddRange(s1);
}

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    // コンボボックスのアイテムをオーナードローで描画した場合、フォーカスに関する情報も合わせて設定する必要があります。
    // DrawBackground は背景色を設定し、DrawFocusRectangle は枠の点線を設定します。
    // しかし、枠については、コンボボックスは意味がないかもしれません。

    if (e.Index == -1) return;

    e.DrawBackground();
    e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
                          this.Font,
                          new SolidBrush(Color.Black),
                          e.Bounds.X,
                          e.Bounds.Y);
                          e.DrawFocusRectangle();
}

2025.01.24 ComboBox : オーナードローしたアイテムのフォーカスを表示する

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
    comboBox1.ItemHeight = 40;

    string[] s1 = { "NEC", "SONY", "DELL" };
    comboBox1.Items.AddRange(s1);
}

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index == -1) return;

    // コンボボックスのアイテムにアイコンを表示するには、オーナードローモードでアイテムを描画する必要があります。

    e.Graphics.DrawImage(System.Drawing.Image.FromFile(@"C:\Program Files\Internet Explorer\images\bing.ico"), e.Bounds.X, e.Bounds.Y);
    e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
                     new System.Drawing.Font(comboBox1.Items[e.Index].ToString(), 12),
                     new SolidBrush(Color.Black),
                     e.Bounds.X + System.Drawing.Image.FromFile(@"C:\Program Files\Internet Explorer\images\bing.ico").Width,
}

2025.01.24 ComboBox : オーナードローしたアイテムのフォーカスを表示する

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
    comboBox1.ItemHeight = 20;
    comboBox1.MaxDropDownItems = 20;
    comboBox1.IntegralHeight = false;

    foreach (FontFamily item in FontFamily.Families)
    {
        if (item.IsStyleAvailable(FontStyle.Regular))
        {
            comboBox1.Items.Add(item.Name);
        }
    }
}

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    // コンボボックスのアイテムのフォントを個別に変更するには、オーナードローモードでアイテムを描画する必要があります。
    // これは通常の Add メソッド実行時に呼ばれる DrawItem イベントを使います。

    if (e.Index == -1) return;

    e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
                          new System.Drawing.Font(comboBox1.Items[e.Index].ToString(), 12),
                          new SolidBrush(Color.Black),
                          e.Bounds.X,
                          e.Bounds.Y);
}

2025.01.24 ComboBox : オーナードローしたアイテムのフォーカスを表示する

private void Form1_Load(object sender, EventArgs e)
{
    // コンボボックスのリストの表示数を 10 に設定しています。この場合、IntegralHeight を false に合わせて設定します。

    comboBox1.DataSource = FontFamily.Families;
    comboBox1.DisplayMember = "Name";

    comboBox1.MaxDropDownItems = 10;
    comboBox1.IntegralHeight = false;
}

-C# 開発