Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Separate Connection DLL Passing To/From
Message
 
À
30/04/2007 16:59:16
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 8.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01221271
Message ID:
01221325
Vues:
29
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform