|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Accessing value of a Variable in parent from custom controlIs it possible to access value of a variable in Parent Form within control at
runtime? Please provide an example (possibly in VB). It isnt very object oriented as the control isnt part of the form and could
be used on a form without that variable. Unless the controls code definition was inside the forms, in which case it would be a able to access privates after casting the Parent property to the type of the parent form. Ciaran O'Donnell Show quote "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). 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 -- Show quoteDale 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). 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. Show quote "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). 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 -- Show quoteDale 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). 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. ------------------------- Show quote "Dale" <dale0973@nospam.nospam> wrote in message news:6AD37C2A-C539-45DB-BE10-E733ACEBC094@microsoft.com... > 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). 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)? Show quote "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@nospam.nospam> wrote in message > news:6AD37C2A-C539-45DB-BE10-E733ACEBC094@microsoft.com... > > 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). > > > Well, I was just trying to translate from C# to VB for you, but I've gone
back and reread the whole thread. If all you want is to be able to get some kind of information from the parent form in which the control resides, i'd think you could just add a public property to the parent form. If Form1 implements an interface, it means that you must provide the code for the properties defined in the interface. I think the line > myControl = DirectCast(Me, iUsesMyControl) allows you to access the methods and properties in the interfacefrom the control. I think what this allows you to do is set the properties in the form that implement the interface to private. Then casting myControl to that interface allows it to see the private properties. So only the control can see the properties, not the whole project. So in the control, maybe you could access myControl.FormInt and myControl.FormString and see the properties from the form. Can anybody verify that I'm understanding that right? However, I don't really see the need for something this complicated if all you are trying to do is provide the ability for the control to see something in the form. I would think you could just add a property to the form, and let the control access it. The code you have for the properties (which are public) should work okay. Then in your control, I would think you could access the properties as Form1.FormInt and Form1.FormString. I guess it depends on whether it's okay to make those properties public to the rest of the project or not. Hope that helps. If anybody can shed some wisdom here, that would be great. Thanks, Robin S. Show quote "Zahid Hayat" <ZahidHa***@discussions.microsoft.com> wrote in message news:9A6BB7EC-1ED6-43D6-BBA8-70328817CF0B@microsoft.com... > 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@nospam.nospam> wrote in message >> news:6AD37C2A-C539-45DB-BE10-E733ACEBC094@microsoft.com... >> > 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). >> >> >> I am developing this project using VisualStudio2005. The control is developed
independently and is not a part of the project (form1). I will add it to list of controls in VS2005 and then drop it on the form where ever desired. The problem is that the control is not awair of either the iUsesMycontrol interface nor the the form1. Show quote "RobinS" wrote: > Well, I was just trying to translate from C# to VB for you, but I've gone > back and reread the whole thread. If all you want is to be able to > get some kind of information from the parent form in which the control > resides, i'd think you could just add a public property to the parent form. > > If Form1 implements an interface, it means that you must provide the > code for the properties defined in the interface. I think the line > > myControl = DirectCast(Me, iUsesMyControl) > allows you to access the methods and properties in the interface > from the control. I think what this allows you to do is set the > properties in the form that implement the interface to private. > Then casting myControl to that interface allows it to see the > private properties. So only the control can see the properties, > not the whole project. So in the control, maybe you could access > myControl.FormInt and myControl.FormString and see the > properties from the form. > > Can anybody verify that I'm understanding that right? > > However, I don't really see the need for something this complicated > if all you are trying to do is provide the ability for the control > to see something in the form. I would think you could just add a > property to the form, and let the control access it. The code > you have for the properties (which are public) should work okay. > Then in your control, I would think you could access the > properties as Form1.FormInt and Form1.FormString. > I guess it depends on whether it's okay to make those > properties public to the rest of the project or not. > > Hope that helps. If anybody can shed some wisdom here, > that would be great. > > Thanks, > Robin S. > > > "Zahid Hayat" <ZahidHa***@discussions.microsoft.com> wrote in message > news:9A6BB7EC-1ED6-43D6-BBA8-70328817CF0B@microsoft.com... > > 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@nospam.nospam> wrote in message > >> news:6AD37C2A-C539-45DB-BE10-E733ACEBC094@microsoft.com... > >> > 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). > >> > >> > >> > > > I don't know the answer to your question. You might try posting it
in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls and see if anyone else can help. Please post it to both groups at once, so if someone in one group answers it, people in the other group will also be able to see the answer. Robin S. --------------------------------------------- Show quote "Zahid Hayat" <ZahidHa***@discussions.microsoft.com> wrote in message news:AEC52FC1-B9A2-40B0-91C9-01E32470B961@microsoft.com... >I am developing this project using VisualStudio2005. The control is >developed > independently and is not a part of the project (form1). I will add it to > list > of controls in VS2005 and then drop it on the form where ever desired. The > problem is that the control is not awair of either the iUsesMycontrol > interface nor the the form1. > > > "RobinS" wrote: > >> Well, I was just trying to translate from C# to VB for you, but I've gone >> back and reread the whole thread. If all you want is to be able to >> get some kind of information from the parent form in which the control >> resides, i'd think you could just add a public property to the parent >> form. >> >> If Form1 implements an interface, it means that you must provide the >> code for the properties defined in the interface. I think the line >> > myControl = DirectCast(Me, iUsesMyControl) >> allows you to access the methods and properties in the interface >> from the control. I think what this allows you to do is set the >> properties in the form that implement the interface to private. >> Then casting myControl to that interface allows it to see the >> private properties. So only the control can see the properties, >> not the whole project. So in the control, maybe you could access >> myControl.FormInt and myControl.FormString and see the >> properties from the form. >> >> Can anybody verify that I'm understanding that right? >> >> However, I don't really see the need for something this complicated >> if all you are trying to do is provide the ability for the control >> to see something in the form. I would think you could just add a >> property to the form, and let the control access it. The code >> you have for the properties (which are public) should work okay. >> Then in your control, I would think you could access the >> properties as Form1.FormInt and Form1.FormString. >> I guess it depends on whether it's okay to make those >> properties public to the rest of the project or not. >> >> Hope that helps. If anybody can shed some wisdom here, >> that would be great. >> >> Thanks, >> Robin S. >> >> >> "Zahid Hayat" <ZahidHa***@discussions.microsoft.com> wrote in message >> news:9A6BB7EC-1ED6-43D6-BBA8-70328817CF0B@microsoft.com... >> > 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@nospam.nospam> wrote in message >> >> news:6AD37C2A-C539-45DB-BE10-E733ACEBC094@microsoft.com... >> >> > 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). >> >> >> >> >> >> >> >> >> |
|||||||||||||||||||||||