Adding controls to Extended Form class

Cep

Member
Joined
Apr 29, 2008
Messages
11
Programming Experience
1-3
Hello there,

I am currently extending the form class to create a form template class for forms in my project. One of the things I want to do is product a label on each form with my name in the bottom right at about -10, -10 pixels from the border of the form.

The problem I seem to be having is that each child object can have a different size and my template class has a form with 300 x 300 size. So as you may have guessed when the form object is created with a size of 100 x 200, my dynamic label is appearing at 290, 290 still.

Here is my code,

VB.NET:
Public Class FadingForm

    Public Sub New()
        MyBase.New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.        
        MyClass.AddCopyrightLabel()
        Me.Text = "My Form"
        Me.Opacity = 0
        Me.FadeTimer.Enabled = True
        Me.BackColor = Color.LightBlue
        Me.Icon = GetEmbeddedIcon("MyNamespace.my.ico")

    End Sub

    Private Sub AddCopyrightLabel()

        Dim CopyrightLabel As Label = New Label

        CopyrightLabel.AutoSize = True
        CopyrightLabel.Text = "My name is here"

        CopyrightLabel.Location = New Point(MyClass.Width - 10, MyClass.Height - 10)

        Me.Controls.Add(CopyrightLabel)

    End Sub

    Private Sub FadeTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FadeTimer.Tick
        If Me.Opacity < 1 Then
            Me.Opacity += 0.05
        Else
            Me.FadeTimer.Enabled = False
        End If
    End Sub

    Private Function GetEmbeddedIcon(ByVal strName As String) As Icon
        Return New  _
    Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream( _
         strName))
    End Function

End Class

The problem is this line I think but I am not sure what to put there instead.

VB.NET:
CopyrightLabel.Location = New Point(MyClass.Width - 10, MyClass.Height - 10)
 
Anchor it to bottom right. You don't have to write code to add the label, use the Windows Form Designer.
 
Thanks for the reply John, I have tried to anchor the control as you suggested but I end up getting exactly the same problem on any form that inherits my template class, the label will appear in the exact same place as it would on the template design view, so for smaller forms inheriting this class the label does not appear.
 
Anchor it to the bottom right and make it a Friend in the base class. This will allow it to be moved in the inheriting class (which should move it around when the form resizes).
 
Hi jshultz, nope I get exactly the same problem the label appears at the 300, 300 location on all forms regardless of what size they actually are.
 
Well, I don't know what to tell you then. I just tried this in a test application and it worked fine. Post your code if anything has changed from your previous posting.
 
Hi, here is the code

VB.NET:
Public Class FadingForm

    Public Sub New()
        MyBase.New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.        
        MyClass.AddCopyrightLabel()
        Me.Text = "My Form"
        Me.Opacity = 0
        Me.FadeTimer.Enabled = True
        Me.BackColor = Color.LightBlue
        Me.Icon = GetEmbeddedIcon("MyNamespace.my.ico")

    End Sub

    Private Sub AddCopyrightLabel()

        Dim CopyrightLabel As Label = New Label

        CopyrightLabel.AutoSize = True
        CopyrightLabel.Text = "My name is here"

        CopyrightLabel.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)


        Me.Controls.Add(CopyrightLabel)

    End Sub

    Private Sub FadeTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FadeTimer.Tick
        If Me.Opacity < 1 Then
            Me.Opacity += 0.05
        Else
            Me.FadeTimer.Enabled = False
        End If
    End Sub

    Private Function GetEmbeddedIcon(ByVal strName As String) As Icon
        Return New  _
    Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream( _
         strName))
    End Function

End Class
 
Two things.

1) Why aren't you doing this from the designer instead of in the code?!
2) You are setting your anchor to the top and right. You need to set it to the bottom and right.
 
1. I have done it in both, dynamically using the method above and using the form designer neither work.

2. I have tried changing the code to top left, bottom right, top right, bottom left, all sorts, the control just always appears in top left when using the anchor code above.

If I don't use the code line above the location is always 300, 300 regardless of whether I drag a label in designer or create it via the method.
 
Well, I don't know what you are doing wrong, but you are doing something wrong.

I created a test application and dragged and dropped a label on the form. I set the label's anchor to bottom and right. I then inherited the form and it adjusted the label to the correct position whenever the form was resized.
 
Could you zip up the project and send it to me? I am getting the feeling this isn't anything I am doing, as I have followed numerous examples, I am beginning to think there is something wrong with my VS.
 
I have also done this in Designer without problems, and it works for different sized inherited forms.

Your problem above is that you are creating a control in code without setting its location, so it defaults to (0,0). You did that in first post, but only relative to parents initial size.

The point of the Anchor property is that the control will stick to its anchored walls when the parent container is resized. You still have to put it at correct location initially, this what you do in Designer; put controls where they belong and configure them. The label Modifiers property should be set to Protected Friend (or wider scope) in parent for you to be able to move in inherited forms Designer, this because the inherited form needs to override and store the new initial location.

I have attached the test project.
 

Attachments

  • vbnet20-FormsInheritance.zip
    22.7 KB · Views: 24
Thanks for the upload John, I will try this on my machine tomorrow and see if I can get it working, i'll keep you posted :)
 
Sorry, I'm at work and got really busy so I didn't have time to upload the project. Thanks JohnH for jumping on that =-)
 
Hello again,

I have tried your project out but all I receive are errors when running it. I am guessing you are are using VS2005 because my VS had to convert the project first.

After which on the child forms I have these errors,

The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: Derived --- The base class 'FormInherit.ParentForm' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

I am also looking into the possibility this is a bug as another person has mentioned that using inherit does not seem to work on the code for forms as intended and that you have add a new class, close your solution and then open up the designer.vb file of the class and alter the the Inherits System.Windows.Forms.Form to Inherits BaseForm/Parentform in the code before it can work.

I have tried this with a new project and once I have made the direct edit in the designer file it appears to work as you both mention.
 
Back
Top