.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old April 29th, 2008, 04:24 AM
Cep's Avatar
Cep Cep is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 263 Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 15 h 19 m 54 sec
Reputation Power: 5
Talking Adding controls to Extended Form class - VB.NET

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,

Code:
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.

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

Reply With Quote
  #2  
Old April 29th, 2008, 07:51 AM
f'lar's Avatar
f'lar f'lar is offline
Senior WeyrLeader
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: WI
Posts: 3,737 f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 8 h 6 m 39 sec
Reputation Power: 675
Send a message via Google Talk to f'lar
CopyrightLabel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)

You can set that in the designer too.
__________________
Primary Forums: .Net Development, MS-SQL, C Programming
VB.Net: It's not your father's Visual Basic.

[Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

Reply With Quote
  #3  
Old April 29th, 2008, 08:15 AM
Cep's Avatar
Cep Cep is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 263 Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 15 h 19 m 54 sec
Reputation Power: 5
Can I ask whether your replacing the line I mentioned with that line in the template form class or are you setting that in the constructor of the new form that inherits the class?

If you are replacing it, this doesn't seem to work, I just end up with a copyright label in the top left of my form when it should be in the bottom right.

Reply With Quote
  #4  
Old April 29th, 2008, 08:18 AM
danielk's Avatar
danielk danielk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: Earth
Posts: 207 danielk User rank is Lance Corporal (50 - 100 Reputation Level)danielk User rank is Lance Corporal (50 - 100 Reputation Level)danielk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 23 h 20 m 15 sec
Reputation Power: 4
in the Visual Studio designer, you will see that the label has a property called Anchor
just change it from top, left to bottom right
then it will anchor itself to the bottom right corner

Reply With Quote
  #5  
Old April 29th, 2008, 08:23 AM
Cep's Avatar
Cep Cep is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 263 Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 15 h 19 m 54 sec
Reputation Power: 5
Hi Danielk, I have had a number of people say this to me elsewhere, and it still does not work. The label will appear at location 300, 300 even if I friend the label to the base class. I am beginning to suspect this is a bug because everyone keeps giving me the same types of answers yet none actually appear to work.

Reply With Quote
  #6  
Old April 29th, 2008, 08:30 AM
danielk's Avatar
danielk danielk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: Earth
Posts: 207 danielk User rank is Lance Corporal (50 - 100 Reputation Level)danielk User rank is Lance Corporal (50 - 100 Reputation Level)danielk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 23 h 20 m 15 sec
Reputation Power: 4
Did you set the anchor on the base form or the child forms? If you set anchor to bottom right on the base form after you created the child form then most probably in the child form's InitializeComponent () function the anchor is being set back to top left. Is this the case?

do you explicitly set the location of the label anywhere in your code?

Reply With Quote
  #7  
Old April 29th, 2008, 10:26 AM
Cep's Avatar
Cep Cep is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 263 Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 15 h 19 m 54 sec
Reputation Power: 5
Hi Dan, I only ever placed this line of code to explicitly set the location of the control originally,

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


I replaced this with the anchor code as provided by f'lar (which gave the same problem) and I also removed the addCopyrightLabel method and added a label using the form designer with bottom right anchor and friend properties and got the same result again. I did this all on the base form.

The child forms inherit from the base form with no constructor of their own. Here is an example of a child form,

Code:
Imports System.Security.Cryptography
Public Class Form1

    Inherits FadingForm

    Public Function ComputeHashValue(ByVal data() As Byte) As Byte()
        Dim hashAlg As SHA1 = SHA1.Create()
        Dim hashvalue() As Byte = hashAlg.ComputeHash(data)

        Return hashvalue
    End Function

    Private Sub uxEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxEncrypt.Click

        Dim MyNewHash As Byte()

        MyNewHash = ComputeHashValue(System.Text.Encoding.ASCII.GetBytes(uxText.Text))

        uxPlain.Text = uxText.Text

        uxEncrypted.Text = BitConverter.ToString(MyNewHash)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "This is Form 1"
    End Sub

End Class

Reply With Quote
  #8  
Old April 29th, 2008, 10:37 AM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,538 LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 h 19 m
Reputation Power: 357
Alright, so you said you used f'lar's code on the base form. Where?

There should only be the Label positioned where you want it, with it's Anchor property set to Right | Bottom. You shouldn't need anything else. (And make sure the Label is Private so that deriving Forms can't change it.)

The Label isn't inside any Panels, is it?
__________________
Joel B Fant - LyonHaert.net

2 + 2 is... 10... in base 4

Reply With Quote
  #9  
Old April 29th, 2008, 10:59 AM
Cep's Avatar
Cep Cep is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 263 Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 15 h 19 m 54 sec
Reputation Power: 5
Quote:
Originally Posted by Cep
Code:
CopyrightLabel.Location = New Point(MyClass.Width - 10, MyClass.Height - 10)


I replaced this with the anchor code as provided by f'lar.


Here, in the base class addCopyrightLabel method.

There are no panels on this or any other form.

As mentioned if I remove the method that creates the control dynamically and do this in the form designer of the base class form, setting a labels properties for anchor at bottom right, the control will always appear at the location 300, 300 on a child form regardless of the child forms size.

Reply With Quote
  #10  
Old April 29th, 2008, 11:21 AM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,538 LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 h 19 m
Reputation Power: 357
And I'm telling you I created a throwaway project to test it and it worked fine. There is some other factor here.

Here's what I did:
1. Create a new VB Windows Application.
2. Rename Form1 to BaseForm.
3. Add a Label to BaseForm, put it in the lower-right corner, and change its Anchor property to Bottom, Right.
4. Add a new Class (not a new Form) to the project, naming it DerivingForm.
5. Add the line Inherits BaseForm to the class DerivingForm.
6. Edit the Project Properties and set the Startup Object to DerivingForm instead of BaseForm.
7. Run the project.
8. Resize the instance of DerivingForm and watch the Label stay in the lower-right corner.

If you like, I can upload the solution for you to examine.

Edit: Oddly, this project isn't behaving at all like it should. It's not instantiating DerivingForm. Apparently I need to add a new Form, close the Solution, and then edit the new DerivingForm.Designer.vb file to change the Inherits System.Windows.Forms.Form to Inherits BaseForm. Now setting the Startup Object to DerivingForm actually works.

Edit: InherittedLabel.zip

Last edited by LyonHaert : April 29th, 2008 at 11:53 AM.

Reply With Quote
  #11  
Old April 29th, 2008, 02:38 PM
Cep's Avatar
Cep Cep is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 263 Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 15 h 19 m 54 sec
Reputation Power: 5
Thanks, I'll try your project out and see what results I get tomorrow morning. I am beginning to think that this may be something to do with my VS because I can't see how this is not working myself either.

Reply With Quote
  #12  
Old May 2nd, 2008, 06:53 AM
Cep's Avatar
Cep Cep is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 263 Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level)Cep User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 15 h 19 m 54 sec
Reputation Power: 5
Hi LyonHeart,

Are you using VS2005 or VS2008? I have had to convert your project and in both classes there is no code so I am assuming that the convertor has removed it as you mentioned you had added the line inherits baseform to the derivingform but I cannot see this.

I take it though from your Edit that you had to remove this and do the inheritance via the vb file instead?

Reply With Quote
  #13  
Old May 2nd, 2008, 10:50 AM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,538 LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 h 19 m
Reputation Power: 357
I'm using VS2005.

The contents of BaseForm.vb look like this:
Code:
Public Class BaseForm

End Class
Same for DerivingForm.vb. I didn't put any buttons or functionality. Everything happens in the Designer-generated FormName.Designer.vb files.

In BaseForm.Designer.vb, it creates the Label and sets its Anchor and Position properties, because I did those things in the Designer.

The only file I edited was DerivingForm.Designer.vb, and not in VS. I added a new Form to the project, naming it DerivingForm. Then I saved and closed the solution.

I opened DerivingForm.Designer.vb in a text editor (lines 2 and 3 shown)...
Code:
Partial Class DerivingForm
    Inherits System.Windows.Forms.Form
...and made the following change...
Code:
Partial Class DerivingForm
    Inherits BaseForm
However, I just learned that this step was completely unnecessary.

After creating your base Form, all you have to do is add a new item to the project, so that you get the big window with all the different types of things you can add. Inherited Form is one of those options, and then it brings up a window to pick which form it inherits from.

So here are the revised steps I went through:
1. Create a new project.
2. Rename Form1 to BaseForm.
3. Add a Label to BaseForm.
4. Move the Label to the lower-right.
5. Set the Label's Anchor to Bottom | Right.
6. Add a new Inherited Form to the project, naming it DerivingForm, and selecting BaseForm as the Form to inherit from. (I also changed the Text property so I could visually tell BaseForm appart from DerivingForm.)
7. Set the project's Startup Object to DerivingForm.
8. Run.

Reply With Quote
  #14  
Old May 6th, 2008, 09:11 AM
Cep's Avatar
Cep