C#
パターン4: GetValues / GetNamesでComboBoxバインド
出典: C# Enum 完全ガイド — Description 属性 / [Flags] / 数値変換の使い分け5パターン — パターン4: GetValues / GetNamesでComboBoxバインド
using System;
using System.Linq;
using System.Windows.Forms;
public class StatusForm : Form
{
private ComboBox statusComboBox;
public StatusForm()
{
statusComboBox = new ComboBox();
statusComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
// Enumの全値を取得してComboBoxにバインド
var items = Enum.GetValues(typeof(ProductStatus))
.Cast<ProductStatus>()
.Select(s => new
{
Value = (int)s,
Display = s.GetDescription()// Description属性で表示
})
.ToList();
statusComboBox.DataSource = items;
statusComboBox.ValueMember = "Value";
statusComboBox.DisplayMember = "Display";
Controls.Add(statusComboBox);
}
}
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
Source収録記事
この snippet は記事の「パターン4: GetValues / GetNamesでComboBoxバインド」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
同じ記事から
7 件if (status == 1){ /* 販売中 */ } else if (status == 2){ /* 一時停止 */ } else if (status == 9){ /* 廃番 */ }未収録
if (status == 1){ /* 販売中 */ }
#e9dce50030d4
//商品ステータスのEnum public enum ProductStatus { Active = 1, //販売中
▶ 実行可
パターン1:基本宣言と数値変換
#4c6d50a09fcd
using System; using System.ComponentModel; using System.Reflection;
▶ 実行可
パターン2: Description属性で画面表示
#b083bdc7c698
//キャッシュ版 public static class EnumDescriptionCache { private static readonly Dictionary<Enum, string> _cache = new Dictionary<Enum, string>();未収録
パターン2: Description属性で画面表示
#9ca736d259fd
using System; [Flags] public enum Permission
▶ 実行可
パターン3: [Flags]属性でビット組合せ
#55cfcc401bc8
using System; public enum ProductStatus {
▶ 実行可
パターン5: Parse / TryParse + IsDefinedでDB値検証
#89bad7c4a5ae
![C# Enum 完全ガイド — Description 属性 / [Flags] / 数値変換の使い分け5パターン](/_next/image/?url=https%3A%2F%2Fhiropon-progra.com%2Fwp-content%2Fuploads%2F2026%2F05%2Fcsharp-enum-complete-guide.png&w=3840&q=75)