Re: Accessing value of a Variable in parent from custom control
- From: Zahid Hayat <ZahidHayat@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 18 Nov 2006 03:34:01 -0800
My parent Form looks like this:
=======================
Public Class Form1
Implements iUsesMyControl
Private _formInt As Integer
Private _formString As String
Public var1 As Integer
Private mycontrol As ContainerControl
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_formInt = 123
_formString = "Zahid"
mycontrol = DirectCast(Me, iUsesMyControl)
End Sub
Public Property FormInt() As Integer Implements iUsesMyControl.FormInt
Get
Return _formInt
End Get
Set(ByVal value As Integer)
_formInt = value
End Set
End Property
Public Property FormString() As String Implements
iUsesMyControl.FormString
Get
Return _formString
End Get
Set(ByVal value As String)
_formString = value
End Set
End Property
End Class
============
I do not understand the implements of Control itself. Do we need to
implement another interface 'iUsesMyControl' as we did in the parent from
(Form1)?
"RobinS" wrote:
I think this would be the VB code for that:.
In your form, add an Implements for the interface, and add the properties.
You will want to have private variables to keep the current value,
you are exposing them through a property.
public class MyFrom
Implements iUsesMyControl
Private _FormInt as Integer
Public Property FormInt() As Integer Implements
IUsesMyControl.FormInt
Get
return _FormInt
End Get
Set(ByVal value As String)
_FormInt = value
End Set
End Function
Private _FormString as String
Public Property FormString() As String Implements
IUsesMyControl.FormString
Get
return _FormString
End Get
Set(ByVal value As String)
_FormString = value
End Set
End Function
....(other form code)
End Class
Public Interface IUsesMyControl
Property FormInt as Integer
Property FormString as String
End Interface
In your control:
'**I'm not sure about this; Either I have it wrong,
'** or there's some way to define something as
'** an interface. If this doesn't work, try
'** "implements IUsesMyControl" instead of "as IUsesMyControl".
Private _parentForm as IUsesMyControl
Public Readonly Property ParentForm as IUsesMyControl
Set
_parentForm = value
End Set
End Property
Somewhere early in your parent form
myControl.ParentForm = DirectCast(me, IUsesMyControl)
I think that's right; feel free to correct me if I got any of it wrong.
Robin S.
-------------------------
"Dale" <dale0973@xxxxxxxxxxxxx> wrote in message
news:6AD37C2A-C539-45DB-BE10-E733ACEBC094@xxxxxxxxxxxxxxxx
Unfortunately, I am not a VB.Net developer but you should hopefully be
able
to figure this out from the C#.
Let's say that your control is MyControl and your form is MyForm. The
instance, in my example, of MyControl is named myControl. Assume your
form
needs a string variable we'll call formString and an int variable we'll
call
formInt that are defined in the parent form. Create an interface called,
for
instance, iUsesMyControl.
In your form class declaration, change
public class MyForm : Form
to
public class MyForm : Form, iUsesMyControl
I think the VB would look like
Public Class MyForm : Extends iUsesMyControl
Your interface, iUsesMyControl, would look like:
internal interface iUsesMyControl
{
int FormInt { get; set; }
string FormString { get; set; }
}
Basically, we're defining two properties in the interface that all classes
implementing iUsesMyControl must implement. Check your VB documentation
for
how to define properties in an interface in VB.
Your implementation of iUsesMyControl in your form would look like
private int formInt; // though this may have been previously defined
elsewhere.
internal int FormInt
{
get { return formInt; }
set { formInt = value; }
}
private string formString;
internal string FormString
{
get { return formString; }
st { formString = value; }
}
Check your VB documentation for how to define the variables and expose
them
as properties in VB to accomplish the above C# code in VB.
In your control, add a property:
private iUsesMyControl parentForm;
internal iUsesMyControl ParentForm
{
set { parentForm = value; }
}
Somewhere early in your parent form, perhaps the load event, add
myControl.ParentForm = (iUsesMyControl)this;
This passes the MyForm to MyControl but by casting to iUsesMyControl it
tells MyControl only about the two variables defined in iUsesMyControl. I
think this, in VB would be:
myControl.ParentForm = CType(Me, iUsesMyControl)
Now, in your control, you have available:
parentForm.FormInt
and
parentForm.FormString
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Zahid Hayat" wrote:
First I would like to thank both of you for replying to my question. As I
do
not have any experience with interfaces, therefore if you can provide an
example I will be greatfull.
Zahid.
"Dale" wrote:
You can create a property in the control and then, when the parent form
initializes the control or changes the value of the variable, set the
value
of the property in the control.
Though, like Ciaran says, it is not very object oriented, I have had
cases
where I had to pass the parent form as a property to a control. To get
around the issue he identifies of the control being used in a different
form,
then you create an interface for the data that the control requires
from its
parent and implement that interface in all parents that use that
control. In
that way, by casting to the interface type, you can make any form that
uses
your control into something you can use inside your control.
HTH
Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Zahid Hayat" wrote:
Is it possible to access value of a variable in Parent Form within
control at
runtime?
Please provide an example (possibly in VB).
- Follow-Ups:
- References:
- Prev by Date: Re: Raw TCP/IP library?
- Next by Date: Re: Raw TCP/IP library?
- Previous by thread: Re: Accessing value of a Variable in parent from custom control
- Next by thread: Re: Accessing value of a Variable in parent from custom control
- Index(es):