Simpel custom 'checkgroupbox'
Er ved ar parre en checkbox ovenpå en groupbox til en ny kontrol kaldet checkgroupbox. Ret simpelt, alle kontroller der er contained i groupboxen skal disables, hvis der ikke er valgt i checkboxen. Og omvendt er checkboxen valgt skal alle kontrolle enables. Teksten på groupboxen kommer fra Checkboxen, der er placeret oven i der hvor groupbox.text ville have stået. Men det virker ikke helt. f.eks bliver tekst / caption ikke vist. Nogle forslag til forbedringer ?using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace CheckBoxGroup
{
public class CheckBoxGroup : GroupBox
{
private CheckBox checkBox;
private string _text = "";
private bool _enabled = true;
private bool _checked;
/// <summary>
/// Initializing constructor.
/// </summary>
public CheckBoxGroup()
{
InitializeCustomComponent();
// Make space for a entered caption.
for (int i = 0; i < checkBox.Text.Length; i++)
this.Text += " ";
}
private void UpdateState()
{
_checked = checkBox.Checked;
for(int index = 0; index < this.Controls.Count; index++)
if (this.Controls[index].Name!="checkBox")
this.Controls[index].Enabled = checkBox.Checked;
}
/// <summary>
/// Initial the custom component.
/// </summary>
private void InitializeCustomComponent()
{
this.checkBox = new CheckBox();
this.SuspendLayout();
//
// checkGroupBox
//
this.Controls.Add(this.checkBox);
this.Location = new Point(48, 32);
this.Name = "groupBox1";
this.TabStop = false;
this.Text = "";
//
// checkBox
//
this.checkBox.Location = new Point(10, -8);
this.checkBox.Name = "checkBox";
this.checkBox.TabIndex = 1;
this.checkBox.Size = new Size(160, 30);
this.checkBox.Text = "groupCheckBox";
this.checkBox.Checked = true;
this.checkBox.CheckedChanged += new EventHandler(this.checkBox_CheckedChanged);
this.checkBox.BringToFront();
//
// this
//
this.Text = "";
this.ResumeLayout(false);
}
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
UpdateState();
}
[Category("Test")]
[Description("True if the CheckGroupBox is checked.")]
public bool Checked
{
get { return _checked; }
set { _checked = checkBox.Checked = value; }
}
[Category("Test")]
[Description("Used the Checked property instead.")]
public new bool Enabled
{
get { return _enabled; }
set { _enabled = value; UpdateState(); }
}
[Category("Test")]
[Description("Note text size not implemented to dynamic change of control size.")]
//public override string Text
public new string Text
{
get { return _text; }
set { _text = this.checkBox.Text = value; }
}
}
}
