我創建了自己的單選按鈕類 - 即MyRadioButton,因爲.NET類中內置的類沒有有效放大。 (使用這個觸摸屏)Radiobutton類 - 需要將Radiobuttons分組到羣組
MyRadioButton類運行良好,期望的問題,我不知道如何解決 - 當我有一個窗體上多個MyRdaioButtons,我可以選擇所有......他們以某種方式不應該像在選擇一個時那樣工作,其他人將被自動取消選擇。
我的代碼如下:
public class MyRadioButton : Control
{
public MyRadioButton()
{
}
private string textTowrite;
private bool checkStatus;
private int width;
private int height;
public event EventHandler CheckedChanged;
public delegate void MyHandler1(object sender, EventArgs e);
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
if (Checked)
Checked = false;
else
Checked = true;
Invalidate(true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ButtonState btnstate;
Rectangle rRadioButton;
if (checkStatus)
{
btnstate = ButtonState.Checked;
}
else
btnstate = ButtonState.Normal;
rRadioButton = new Rectangle(0, 0, RBWidth, RBHeight);
FontFamily ft = new FontFamily("Tahoma");
Font fnt_radio = new Font(ft, (int)(18), FontStyle.Bold);
ControlPaint.DrawRadioButton(e.Graphics, -2, 10, rRadioButton.Width,
rRadioButton.Height, btnstate);
//RadioButton's text left justified & centered vertically
e.Graphics.DrawString(textTowrite, fnt_radio, new SolidBrush(Color.Black), rRadioButton.Right + 1, 16);
}
protected virtual void OnCheckedChanged(EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(this, e);
}
}
public override string Text
{
get { return textTowrite; }
set { textTowrite = value; }
}
public bool Checked
{
get { return checkStatus; }
set
{
checkStatus = value;
OnCheckedChanged(EventArgs.Empty);
}
}
public int RBWidth
{
get
{
if (width == 0)
{
width = 40;
}
return width;
}
set
{
if (width != value)
{
width = value;
Invalidate();
}
}
}
public int RBHeight
{
get
{
if (height == 0)
{
height = 40;
}
return height;
}
set
{
if (height != value)
{
height = value;
Invalidate();
}
}
}
}
如果有人能爲我提供了一個解決方案,將不勝感激,因爲我拉出我的頭髮
感謝
延
如果我直接從RadioButton繼承控件 - OnPaint函數將繪製更大的單選按鈕(我想要的),以及原始的較小單選按鈕。因此,我爲什麼要寫我自己的。 – Jens 2010-01-06 18:59:27
正確,這是您完全覆蓋的事件。 – JonathanK 2010-01-29 16:30:58