Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Separate Connection DLL Passing To/From
Message
From
01/05/2007 07:58:30
 
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:
01221396
Views:
17
Thanks so much Fabio. So, if I am creating a textbox for use app wide that in development isn't associated with any form yet (like foundation or parent base class controls in VFP which doesn't care if THISFORM exists yet during dev mode), the code below won't bomb in development mode (before it is dropped on a form)? We are trying to create common controls for use with common code prebuilt into the controls as well as baseforms.


>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));
>        }
>    }
>}
>
.·*´¨)
.·`TCH
(..·*

010000110101001101101000011000010111001001110000010011110111001001000010011101010111001101110100
"When the debate is lost, slander becomes the tool of the loser." - Socrates
Vita contingit, Vive cum eo. (Life Happens, Live With it.)
"Life is not measured by the number of breaths we take, but by the moments that take our breath away." -- author unknown
"De omnibus dubitandum"
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform