[英]Validating event not fired for child controls of picturebox
昨天,我已經在WinForm上的組框中實現了一些控件的驗證事件。 我已將窗體的AutoValidate屬性設置為Disabled,已將控件的CausesValidation屬性設置為true並實現了控件的Validating事件。 通過調用以下形式的ValidateChildren()方法,我強制執行了驗證事件。 一切正常。
但是在將此組框放置在圖片框頂部並將該圖片框設置為該組框的父級之后,不再執行驗證事件...。
下面是一些演示代碼。 表單僅包含圖片框,組框,文本框和按鈕。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
MessageBox.Show("Validating textbox");
e.Cancel = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (ValidateChildren())
MessageBox.Show("Validation not executed :-(");
else
MessageBox.Show("Validation executed :-)");
}
private void Form1_Load(object sender, EventArgs e)
{
groupBox1.Parent = pictureBox1;
}
}
ValidateChildren()方法調用ValidateChildren(ValidationConstraints.Selectable)來完成工作。 對於PictureBox來說,這是一個問題,它是無法選擇的。 因此,它的所有子級都沒有得到驗證。
使用ValidationConstraints調用它也無效,ContainerControl實現了驗證子控件的能力,PictureBox也不能從中派生。 因此,您也無法在PictureBox上調用ValidateChildren。 自己枚舉控件並觸發Validating事件也不起作用,PerformControlValidation()方法是內部的。
您需要重新考慮嘗試將PictureBox轉換為ContainerControl的想法。 如果不通過BackgroundImage屬性,然后通過Paint事件,則大多數任何控件都可以類似於圖片框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.