Csharp/CSharp Tutorial/GUI Windows Forms/CheckBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Add CheckBox to a Form
using System;
using System.Windows.Forms;
public class CheckBoxFormDemo{
[STAThread]
public static void Main(string[] args)
{
Application.Run(new CheckBoxForm());
}
}
public partial class CheckBoxForm : Form
{
public CheckBoxForm()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string[] foods = {"A", "B", "C", "D","E","F","G","H","I","J"};
this.SuspendLayout();
int topPosition = 10;
foreach (string food in foods)
{
CheckBox checkBox = new CheckBox();
checkBox.Top = topPosition;
checkBox.Left = 10;
checkBox.Text = food;
topPosition += 30;
panel1.Controls.Add(checkBox);
}
this.ResumeLayout();
}
}
partial class CheckBoxForm
{
private System.ruponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(292, 266);
this.panel1.TabIndex = 0;
//
// CheckBoxForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Name = "CheckBoxForm";
this.Text = "CheckBoxForm";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
}
Change Label font by CheckBoxes
using System;
using System.Drawing;
using System.Windows.Forms;
public class LabelFontChangedByCheckBoxes : Form
{
Label lbl;
Panel pnl;
FontStyle[] theStyles;
public LabelFontChangedByCheckBoxes()
{
Size = new Size(300,250);
lbl = new Label();
lbl.Parent = this;
lbl.Text = "test";
lbl.Location = new Point(0,0);
lbl.AutoSize = true;
lbl.BorderStyle = BorderStyle.Fixed3D;
int yDelta = lbl.Height + 10;
FontStyle theEnum = new FontStyle();
theStyles = (FontStyle[])Enum.GetValues(theEnum.GetType());
pnl = new Panel();
pnl.Parent = this;
pnl.Location = new Point(0, yDelta );
pnl.Size = new Size(150, (theStyles.Length + 1) * yDelta);
pnl.BorderStyle = BorderStyle.FixedSingle;
int i = 1;
CheckBox cb;
foreach (FontStyle style in theStyles)
{
cb = new CheckBox();
cb.Parent = pnl;
cb.Location = new Point(25, (yDelta * (i - 1)) + 10);
cb.Size = new Size(75,20);
cb.Text = style.ToString();
cb.Tag = style;
cb.CheckedChanged += new System.EventHandler(cb_CheckedChanged);
if (cb.Text == "Regular")
cb.Checked = true;
i++;
}
}
static void Main()
{
Application.Run(new LabelFontChangedByCheckBoxes());
}
private void cb_CheckedChanged(object sender, EventArgs e)
{
FontStyle fs = 0;
for (int i = 0; i < pnl.Controls.Count; i++)
{
CheckBox cb = (CheckBox)pnl.Controls[i];
if (cb.Checked)
fs |= (FontStyle)cb.Tag;
if (((CheckBox)pnl.Controls[i]).Checked)
fs |= (FontStyle)((CheckBox)pnl.Controls[i]).Tag;
}
lbl.Font = new Font(lbl.Font, fs);
}
}
CheckedChanged event for CheckBox
using System;
using System.Drawing;
using System.Windows.Forms;
class CheckBoxDemo: Form
{
public static void Main()
{
Application.Run(new CheckBoxDemo());
}
public CheckBoxDemo()
{
CheckBox[] achkbox = new CheckBox[4];
int cyText = Font.Height;
int cxText = cyText / 2;
string[] astrText = {"Bold", "Italic", "Underline", "Strikeout"};
for (int i = 0; i < 4; i++){
achkbox[i] = new CheckBox();
achkbox[i].Text = astrText[i];
achkbox[i].Location = new Point(2 * cxText, (4 + 3 * i) * cyText / 2);
achkbox[i].Size = new Size(12 * cxText, cyText);
achkbox[i].CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged);
}
Controls.AddRange(achkbox);
}
void CheckBoxOnCheckedChanged(object obj, EventArgs ea)
{
Invalidate(false);
}
FontStyle[] afs = { FontStyle.Bold,FontStyle.Italic, FontStyle.Underline, FontStyle.Strikeout };
FontStyle fs = 0;
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
for (int i = 0; i < 4; i++){
if (((CheckBox) Controls[i]).Checked){
fs |= afs[i];
}
}
grfx.DrawString(Text, new Font(Font, fs), new SolidBrush(ForeColor), 0, 0);
}
}