Hi Tracy,
Windows forms controls have a property called "Parent" that maintains a reference to their container control. The problem is that some controls might have different types of parents (e.g. a RadioButton may be contained within a GroupBox, not directly within a Form). So, this "Parent" property is typed as "System.Windows.Forms.Control" class and you have to try casting it to the appropriate container type in order to use it. This all seems too much work and is certainly troublesome.
There is no equivalent to VFP's "THISFORM" in .NET, but you can use a very convenient method called FindForm() that is available in every windows form control. This method returns a reference to the form that contains the control, independent of its direct parent control.
I hope the code below can demonstrate the FindForm() method usage more clearly than my words <g>:
using System;
using System.Windows.Forms;
public class Program
{
static void Main()
{
MyForm myForm = new MyForm();
myForm.Text = "This is My Form";
myForm.ShowDialog();
}
}
public class MyForm : Form
{
protected MyButton _myButton = new MyButton();
public MyForm()
{
InitializeComponents();
}
private void InitializeComponents()
{
_myButton.Text = "Click Here";
_myButton.Left = 10;
_myButton.Top = 10;
this.Controls.Add(_myButton);
}
}
public class MyButton : Button
{
protected override void OnClick(EventArgs e)
{
Form containingForm = this.FindForm();
if (containingForm != null)
{
string msg = "The title of the containing form is [{0}]";
MessageBox.Show(string.Format(msg, containingForm.Text));
}
}
}