Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Separate Connection DLL Passing To/From
Message
 
To
30/04/2007 16:59:16
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 8.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01221271
Message ID:
01221325
Views:
36
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));
        }
    }
}
-----
Fabio Vazquez
http://www.fabiovazquez.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform