|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
||||
|
||||
|
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) |
|
#2
|
||||
|
||||
|
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] |
|
#3
|
||||
|
||||
|
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. |
|
#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 |
|
#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.
|
|
#6
|
||||
|
||||
|
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? |
|
#7
|
||||
|
||||
|
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
|
|
#8
|
||||
|
||||
|
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? |
|
#9
|
||||
|
||||
|
Quote:
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. |
|
#10
|
||||
|
||||
|
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. |
|
#11
|
||||
|
||||
|
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.
|
|
#12
|
||||
|
||||
|
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? |
|
#13
|
||||
|
||||
|
I'm using VS2005.
The contents of BaseForm.vb look like this: Code:
Public Class BaseForm End Class 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
Code:
Partial Class DerivingForm
Inherits BaseForm
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. |
|
#14
|
||
|
|