我在c形式上动态添加了复选框....
并且在加载表单时它们被禁用...
默认情况下,每个复选框都有红色...我已经以编程方式将其指定为黑色...
但是当表格加载时它是红色的......我不知道为什么会发生这种情况......
private void Form2_Load(object sender, EventArgs e)
{
for (int i = 0; i < list.Count; i++)
{
CheckBox c = new CheckBox();
c.Text = i.ToString();
c.Width = 120;
flowLayoutPanel1.Controls.Add(c);
c.ForeColor = Color.Black;
}
flowLayoutPanel1.Enabled = false;
}
只有在启用flowLayoutPanel后才会出现黑色....
即使加载表单,我也希望复选框有黑色...
答案 0 :(得分:2)
这是一种方法。将您的代码更改为:
for (int i = 0; i < list.Count; i++)
{
CheckBox c = new CheckBox();
c.Text = "";
c.Tag = i.ToString();
c.Width = 120;
flowLayoutPanel1.Controls.Add(c);
c.Paint += new PaintEventHandler(c_Paint);
}
flowLayoutPanel1.Enabled = false;
在c_Paint方法中,您可以绘制控件的文本(保存在Tag属性中)
void c_Paint(object sender, PaintEventArgs e)
{
Control c = sender as Control;
if (c != null)
{
string text = c.Tag.ToString();
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
RectangleF rect = new RectangleF(
new PointF(19, 5),
e.Graphics.DrawString(text, this.Font, Brushes.Black, new PointF(19, 5));
}
}
答案 1 :(得分:0)
你应该创建一个函数来执行它并在表单构造函数中的InitializeComponent();
之后调用它。您也应该首先启用面板。
public partial class YourForm: Form
{
public YourForm()
{
InitializeComponent();
// put your function here
}