Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Questions/Problems with subclassing controls and forms
Message
From
08/08/2010 01:18:12
 
 
To
07/08/2010 13:33:01
General information
Forum:
ASP.NET
Category:
Object Oriented Programming
Miscellaneous
Thread ID:
01475116
Message ID:
01475632
Views:
38
Denis,

Perhaps VB is slightly different than C#, but I cannot set this.DialogResult = null; in C#. And another thing, in my testing, I don't need to mess with the DialogResult anyway. Try this and you'll see what I mean:
public class MyForm : Form, IDoExit
{
	public void DoExit()
	{
		MessageBox.Show("Closing Form via DoExit");
		this.Close();
	}
}
public class MyButton : Button
{
	protected override void OnClick(EventArgs e)
	{
	    base.OnClick(e);

	    Form oForm = this.FindForm();
	    if (oForm is IDoExit)
	    {
	        ((IDoExit)oForm).DoExit();
	        MessageBox.Show("Click From Button Class");
	    }
	}
}
And from a button click event handler on your main form, create and open the dialog form:
private void btnOpenForm_Click(object sender, System.EventArgs e)
{
	MyForm oForm = new MyForm();
	MyButton btn = new MyButton();
	oForm.Controls.Add(btn);
	oForm.ShowDialog();
}
Click the button on the dialog form, it closes just fine and I have not done a thing with DialogResult.

Now, just for giggles, in the MyButton class, move the base.OnClick(e) to the end of the OnClick() method, and guess what ... it will no longer close!
public class MyButton : Button
{
	protected override void OnClick(EventArgs e)
	{
	    //base.OnClick(e); comment out, move to end of method

	    Form oForm = this.FindForm();
	    if (oForm is IDoExit)
	    {
	        ((IDoExit)oForm).DoExit();
	        MessageBox.Show("Click From Button Class");
	    }
	    base.OnClick(e); // now form will not close!!!
	}
}
I suspect the problem you were both seeing was where to put the call to the base.OnClick(e)!!

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform