【发布时间】:2011-02-10 15:52:19
【问题描述】:
我的表单有一个包含两个重叠矩形的组框。窗体的其他控件是两组四个数字上下控件,用于设置矩形的颜色。 (nudF1,2,3 和 4 设置前面的矩形,nudB1,2,3 和 4 设置后面的矩形。)一切正常,除了矩形不显示在数字上下设置时设置的颜色表单首先加载。数字上下控件的 ChangeValue 事件都调用 ShowColors() 方法。表单的 Load 事件调用 csColorsForm_Load() 方法。有什么建议吗?
namespace csColors
{
public partial class csColorsForm : Form
{
public csColorsForm()
{
InitializeComponent();
}
private void csColorsForm_Load(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.DarkBlue;
SetColors(sender, e);
}
private void SetColors(object sender, EventArgs e)
{
Control control = (Control)sender;
String ctrlName = control.Name;
Graphics objGraphics;
Rectangle rect1, rect2;
int colorBack, colorFore;
objGraphics = this.grpColor.CreateGraphics();
// If calling control is not a forecolor control, paint backcolor rectangle
if (ctrlName.Substring(0,4)!="nudF")
{
colorBack = int.Parse(SetColorsB("nudB"), NumberStyles.HexNumber);
SolidBrush BrushB = new SolidBrush(Color.FromArgb(colorBack));
rect1 = new Rectangle(this.grpColor.Left, this.grpColor.Top,
this.grpColor.Width, this.grpColor.Height);
objGraphics.FillRectangle(BrushB, rect1);
}
// Always paint forecolor rectangle
colorFore = int.Parse(SetColorsB("nudF"), NumberStyles.HexNumber);
SolidBrush BrushF = new SolidBrush(Color.FromArgb(colorFore));
rect2 = new Rectangle(this.grpColor.Left, this.grpColor.Top,
this.grpColor.Width, this.grpColor.Height);
objGraphics.FillRectangle(BrushF, rect2);
objGraphics.Dispose();
}
private string SetColorsB(string nam)
{
string txt="";
for (int n = 1; n <= 4; ++n)
{
var ud = Controls[nam + n] as NumericUpDown;
int hex = (int)ud.Value;
txt += hex.ToString("X2");
}
return txt;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
【问题讨论】:
-
你想做什么?隐藏控件?
-
不,我没有隐藏任何控件。我正在使用表格来显示混合两种颜色的结果。
标签: c# winforms graphics forms