Extended Control Grouping
Extended Control Grouping
Extended Control Grouping
Extended Control Grouping
Extended Control Grouping
Extended Control Grouping Extended Control Grouping Extended Control Grouping Extended Control Grouping Extended Control Grouping Extended Control Grouping Extended Control Grouping Extended Control Grouping
Extended Control Grouping Extended Control Grouping
Extended Control Grouping
Go Back  Xtreme Visual Basic Talk > > > Extended Control Grouping


Reply
 
Thread Tools Display Modes
  #1  
Old 02-17-2009, 06:51 AM
Craiglowdon Craiglowdon is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default Extended Control Grouping


Hello,

I hope somebody can help me here. As a background to what I am doing, I am creating a design tool for my company which allows the users to create forms and store the design and order in a database for input on to the form later.

My problem is I have created Extended controls (after looking up the terminology on MSDN I had realised I had done this) by inheriting (for example) a textbox onto my new class.


Code:
class NewTextbox
Inherits Textbox
these controls each only have 3 extra properties. and these properties are the same.

they are ParameterIndex,Index,and DocumentIndex.

For the form they are crucial as I create temporary links and identifiers to these controls by using these values.

The problem is that with a normal textbox or a normal label I can use the control object as the above will convert perfectly.

I can not use my the control object as a conversion type on my checks as the additional properties created in my custom text boxes do not exist in the control class.

what I want to know is is there anyway to amend the control class (which i doubt) or secondly, create a collection class allows for the conversion of my extended objects and allows me to input some common properties and/or methods.

This may not be clear and if not please ask for additional information and I will be as helpful as I can.

Thank you very much in advance!

Craig Lowdon
Reply With Quote
  #2  
Old 02-17-2009, 07:07 AM
DrPunk's Avatar
DrPunkExtended Control Grouping DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I'm not sure I understand completely, but do you need to CType function to convert the object into your own textbox.

You can use the TypeOf to check the type of the object.

So for example...

Code:
If TypeOf MyObject Is TextBox Then
    ' normal textboxes and also your own textboxes will get in here because
    ' your own textbox is inheritted from textbox
    If TypeOf MyObject Is MyOwnTextBox Then
        someValue = CType(MyObject, MyOwnTextBox).MyParameterAddedToTextBox
    End If
Endif
I haven't a clue if that's help, but fingers crossed.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 02-17-2009, 07:34 AM
Craiglowdon Craiglowdon is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default

Thank you for your reply, Thats a great immediate fix to the current problem I have.

Looking at future proofing this though I am looking for a complete restructure of my classes and controls.

Preferably I want a group of extended controls to all be connected via a common class. (as controls do with the control class) and I have no idea how to achieve this.

For example

Code:
Class MyControl
Inherits Control

Property ControlIndex
Property ControlCount

End Class
I want MyTextBox control class to have all the properties and methods of MyControl which inherits Control and also have its own unique properties.

This way I can use ctype(sender,mycontrol).controlindex in my event.

Thanks again
Reply With Quote
  #4  
Old 02-17-2009, 08:02 AM
DrPunk's Avatar
DrPunkExtended Control Grouping DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I was lucky there, let's hope I'm lucky here.

What I think you want is Interfaces. Whereby you define the structure of your.... err.... interface (for want of a better word), and then your objects implement this interface.

So, for example, your interface would be...

Code:
Interface IMyInterface
    Property ControlIndex as Integer
    Property ControlCount as Integer
End Interface
Note: It's common practice to name Interfaces starting with I.
Note: Always say what type your variables are or they'll get set as objects.

Now, in your classes you are able to Implement this Interface.
Code:
Class MyClass
    Implements IMyInterface
What you'll find (if you press enter at the end of the implements line) is that the editor puts in the properties for you because your class must contain these in order to implement the interface, but it will be the classes job of taking care of their execution because the interface can only define a structure, not the implementation.
Code:
Class MyClass
    Implements IMyInterface

    Public Property ControlIndex() As Integer Implements IMyInterFace.ControlIndex
        Get

        End Get
        Set(byval value as integer)

        End Set
    End Property

    Public Property ControlCount() As Integer Implements IMyInterFace.ControlIndex
        Get

        End Get
        Set(byval value as integer)
        End Set
    End Property
End Class
Now, you can use TypeOf to check for the interface rather than a type object, and you can cast (i.e. CType) to the interface to get to those properties.

You can also pass objects around as the Interface (as long as the object implements it of course).

Hope this helps.
__________________
There are no computers in heaven!
Reply With Quote
  #5  
Old 02-17-2009, 08:25 AM
Craiglowdon Craiglowdon is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default

Thanks again DrPunk,

I did some research into interfaces and knew the majority of this but what I am lacking is the way the properties are called, stored and what you mean by "you can cast".

I cannot clearly see the benefits of these interfaces, I can see that a class that implements an interface must include all of those properties and this seems good for defining properties in many places.

I still dont see how this interface would help me group my extended controls and if this would in fact do that how I would implement all the properties of a control under this interface too.

my beginner thoughts on how to overcome my problem (and I am a beginner learning vb just over a year ago completely self taught) would be a common class that lists my control classes aswell as its own methods and properties.

i think my main problem is that I want to look inside the .net control.controlcollection class and see what they have done and I cant do that...!
Reply With Quote
  #6  
Old 02-17-2009, 08:53 AM
Craiglowdon Craiglowdon is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default

And because i know when im trying to explain what I want I do it badly here is some code to try to explain

Code:
      Dim response As String

            response = InputBox("please enter new name", "Rename", CType(sender, LabelTRD).Text)

            If response = Nothing Then
                Me.Controls.Item(CType(sender, LabelTRD).Index).Text = CType(sender, LabelTRD).Text
            Else
                Me.Controls.Item(CType(sender, LabelTRD).Index).Text = response

                For a = 0 To (CreationFormFrame.Controls.Count - 1)
                    If TypeOf CreationFormFrame.Controls.Item(a) Is TextBoxTRD Then
                        If CreationFormFrame.Controls.Item(a).parameterindex Then

                        End If
                    End If
                Next
the blue is not possible as parameter index is not a property in the control class, yet I have added the control to the form through form.controls.add

I think that there is obviously a better way for me to structure this but I am lost on how
Reply With Quote
  #7  
Old 02-17-2009, 09:42 AM
DrPunk's Avatar
DrPunkExtended Control Grouping DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I'm still not completely clear as to what it is you are trying to achieve, but what you are missing in your code is...

If CType(CreationFormFrame.Controls.Item(a), TextBoxTRD).parameterindex Then

By cast I just mean you force an object to a specific object type.

For example, you have the controls collection in a form. All of these objects in the collection are Controls and they will all inherit from Control (that might not be 100% accurate and probably isn't, it could be UserControl, I can't remember but it doesn't really matter in the scheme of things here). But if you were to iterate over those controls you would only be able to access the properties that belong to Control, not the properties that belong to the specific type of Control. To access the specific properties you have to cast the object to it's specific type, like shown above.

Also, in your code you say...

CType(sender, LabelTRD).Text

You do not need to Ctype here, because Text belongs to LabelTRD from it being a label. You could quite happily say LabelTRD.Text. You only need Ctype if you are dealing with variables of one type and need to get to properties that don't belong to the type that you are treating it as.

I've probabaly not explained that very well, but hopefully we're getting somewhere. Don't be afraid to tell me I've confused the issue more and to try and help instead of hinder
__________________
There are no computers in heaven!
Reply With Quote
  #8  
Old 02-17-2009, 10:22 AM
DrPunk's Avatar
DrPunkExtended Control Grouping DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Maybe to help explain further, consider For a = 0 To (CreationFormFrame.Controls.Count - 1)

Instead of a for couting loop you can use a for each loop...

Code:
Dim c As Control
For Each c in CreationFormFram.Controls
     ' Do whatever you want with each c
     If TypeOf c IS TextBoxTRD Then
         if CType(c, TextBoxTRD).ParameterIndex Then

         end if
     Elseif TypeOf c is LabelTRD Then

     Endif
Next
But by using an interface we could catch ALL the controls that have that interface and not worry about what type of control they actually are

Code:
Dim c As Control
For Each c in CreationFormFram.Controls
     ' Do whatever you want with each c
     If TypeOf c IS ITRD Then
         ' Catches textboxTRD, labelTRD, any control that implements ITRD.
         if CType(c, ITRD).ParameterIndex Then

         end if
     Endif
Next
__________________
There are no computers in heaven!
Reply With Quote
  #9  
Old 02-17-2009, 10:24 AM
Craiglowdon Craiglowdon is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default

It all makes perfect sense, ive been trying to do this bit of code for so long ive gone away from what I was trying to do.

I understand the use of CType as a conversion function to change the object you give it to a type that uses the property and can see why it is needed for the parameter index.

That solves the problem with this particular piece of code.

what I think im doing here is simply being stubborn. I can easily use what I have based on the help you have given to find what I need.

The only reason I would preferably like a common class is so that I can do this in one go rather than doing it for my classes NewTextbox, NewLabel, NewCheckBox, NewCmboBox etc which would require

If TypeOf MyObject Is <> Then

multiple times having to cast on each occasion.

without this I can still create my program im just itching at the perfect way to create relational classes that have common attributes and methods.

Thank you very much for your help!
Reply With Quote
  #10  
Old 02-17-2009, 10:33 AM
Craiglowdon Craiglowdon is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default

Quote:
Originally Posted by DrPunk View Post
Maybe to help explain further, consider For a = 0 To (CreationFormFrame.Controls.Count - 1)

Instead of a for couting loop you can use a for each loop...

Code:
Dim c As Control
For Each c in CreationFormFram.Controls
     ' Do whatever you want with each c
     If TypeOf c IS TextBoxTRD Then
         if CType(c, TextBoxTRD).ParameterIndex Then

         end if
     Elseif TypeOf c is LabelTRD Then

     Endif
Next
But by using an interface we could catch ALL the controls that have that interface and not worry about what type of control they actually are

Code:
Dim c As Control
For Each c in CreationFormFram.Controls
     ' Do whatever you want with each c
     If TypeOf c IS ITRD Then
         ' Catches textboxTRD, labelTRD, any control that implements ITRD.
         if CType(c, ITRD).ParameterIndex Then

         end if
     Endif
Next
This helps a lot, i didnt realise you can actually convert it to an interface type?
Reply With Quote
  #11  
Old 02-17-2009, 10:34 AM
DrPunk's Avatar
DrPunkExtended Control Grouping DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I think it's the restriction of only being able to inherit from one class that you're fighting with.

Like you could create a base Control that has your required common functionality. But then the classes that you inherit this from won't be able to inherit from TextBox and you'd have to implement the whole textbox functionality yourself.

I'm only really starting to understand some of this myself. VB6 does you know favours when it comes to inheritance. Maybe someone else has a better solution for you. But stick with it and maybe you'll notice something along the way. It takes trying to get better.
__________________
There are no computers in heaven!
Reply With Quote
  #12  
Old 02-17-2009, 10:38 AM
DrPunk's Avatar
DrPunkExtended Control Grouping DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Quote:
Originally Posted by Craiglowdon View Post
This helps a lot, i didnt realise you can actually convert it to an interface type?
Yes, and like I said you can pass variables around as interface types. E.g...

Code:
Private Sub CheckParameter(byval trd as ITRD)
    If trd.ParameterIndex then
    End if
End Sub

' And using it
Private Sub DoSomething()
    Dim x A New TextBoxTRD

    CheckParameter(x)
End Sub
Obviously if you tried to pass something that didn't implement ITRD then it wouldn't be very happy.
__________________
There are no computers in heaven!
Reply With Quote
  #13  
Old 02-17-2009, 10:45 AM
Craiglowdon Craiglowdon is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default

This has really helped me understand interfaces a great deal! I will definitely be creating an interface for my parameters now and implementing these interfaces in my control classes.

I have two final questions, how many interfaces can you implement in a class?

and

why do i see classes with for example

sub TESTING() implements IIRD.testing

and how is this used?

you have already been a huge help and have permanently changed the way I will implement properties in the future!
Reply With Quote
  #14  
Old 02-17-2009, 11:23 AM
Craiglowdon Craiglowdon is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default

Quote:
Originally Posted by Craiglowdon View Post
This has really helped me understand interfaces a great deal! I will definitely be creating an interface for my parameters now and implementing these interfaces in my control classes.

I have two final questions, how many interfaces can you implement in a class?

and

why do i see classes with for example

sub TESTING() implements IIRD.testing

and how is this used?

you have already been a huge help and have permanently changed the way I will implement properties in the future!
Ive answered my second question myself as once I created my interface I realised this tells the interface which property in my class implements each property in the interface. - Works perfectly!
Reply With Quote
  #15  
Old 02-17-2009, 11:31 AM
DrPunk's Avatar
DrPunkExtended Control Grouping DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Quote:
Originally Posted by DrPunk View Post
Also, in your code you say...

CType(sender, LabelTRD).Text

You do not need to Ctype here, because Text belongs to LabelTRD from it being a label. You could quite happily say LabelTRD.Text. You only need Ctype if you are dealing with variables of one type and need to get to properties that don't belong to the type that you are treating it as.
I have to apologise for posting this part which I've realised was correct and I identified it wrong.

I didn't realise you were using sender, which is Object and therefore must be cast to a type to use.

Sorry, I hope it didn't confuse things.
__________________
There are no computers in heaven!
Reply With Quote
  #16  
Old 02-17-2009, 11:46 AM
Craiglowdon Craiglowdon is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default

Quote:
Originally Posted by DrPunk View Post
I have to apologise for posting this part which I've realised was correct and I identified it wrong.

I didn't realise you were using sender, which is Object and therefore must be cast to a type to use.

Sorry, I hope it didn't confuse things.

I tried it out and realised I had to keep it in in this case, but at least it let me re-examine what I was doing as I had lost what i was trying to achieve in the first place!!
Reply With Quote
  #17  
Old 02-17-2009, 12:11 PM
DrPunk's Avatar
DrPunkExtended Control Grouping DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Every cloud...

I'm not aware of a limit on how many interfaces you can implement. Keep typing and find out.

There's a load of interfaces built into the framework to use. Like the IComparable interface that lets Array.Sort sort an array of your class. It's worth checking them out.
__________________
There are no computers in heaven!
Reply With Quote
  #18  
Old 02-17-2009, 12:12 PM
AtmaWeapon's Avatar
AtmaWeaponExtended Control Grouping AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Your classes implement a common interface but need to be able to derive from different classes. You could approach this by exploiting the fact that they have the common ancestor Control (or, less specifically, Object) but the language provides Interfaces for this reason.

Put the common properties in an interface and have all of your extended controls implement the interface. For example, if it was only the Index property, you might write code like this:
Code:
Interface IExtendedControl

    Public Property Index() As Integer

End Interface

Class NewTextBox
    Inherits TextBox, Implements IExtendedControl ' not positive on syntax here

    Public Property Index() As Integer Implements IExtendedControl.Index
        Get...
        Set...
    End Property
End Class
Then, when you need to work with the extended properties, cast to the interface type:
Code:
Dim ctrl As Control = GetControlSomehow()
Dim face As IExtendedControl = TryCast(ctrl, IExtendedControl)
If face IsNot Nothing
    ' you have one of your extended controls; access the interface properties
    ' through face, the control properties through ctrl.
End If
Edit by NEOLLE: this post has been transferred from a duplicate thread
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.

Last edited by NEOLLE; 02-17-2009 at 05:20 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Extended Control Grouping
Extended Control Grouping
Extended Control Grouping Extended Control Grouping
Extended Control Grouping
Extended Control Grouping
Extended Control Grouping Extended Control Grouping Extended Control Grouping Extended Control Grouping Extended Control Grouping Extended Control Grouping Extended Control Grouping
Extended Control Grouping
Extended Control Grouping
 
Extended Control Grouping
Extended Control Grouping
 
-->