透過反射列舉 System.Drawing.Color 靜態屬性,可動態取得所有預定義顏色名稱與值,填充至 DropDownList 供使用者選擇。
using System.Reflection;
using System.Drawing;
using System.Collections;
PropertyInfo[] properties = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static);
ArrayList colors = new ArrayList();
foreach (PropertyInfo prop in properties)
{
Color color = (Color)prop.GetValue(null, null);
// 過濾不需顯示的特殊顏色
if (color == Color.Transparent) continue;
if (color == Color.Empty) continue;
try
{
// 填充 DropDownList:Text 為顏色名稱,Value 可存 ARGB 值
ddlList.Items.Add(new ListItem(prop.Name, color.ToArgb().ToString()));
}
catch { /* 忽略個別轉換錯誤 */ }
}
// 一行搞定:將所有安裝字型加入 ComboBox
comboBox1.Items.AddRange(FontFamily.Families);
.udl(如 MyDB.udl),圖示會變成資料庫連線圖示。Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=MyDB;Data Source=MyServer
若使用 SQL Server,可將 Provider=SQLOLEDB.1; 替換為 Server=localhost; 等標準關鍵字。
Color 靜態屬性包含 140+ 種命名顏色(如 Red、AliceBlue、Control 等)。background-color: #FF0000 配合 Value (ARGB) 渲染。System.Drawing.Common 需另行安裝 NuGet 套件。