Click here to Skip to main content
13,248,766 members (44,892 online)
Rate this:
Please Sign up or sign in to vote.
When you add a new standard windows form to an application through 'Add New Item' dialog box you are prompted to write the name of the new form in the 'Name' field. When the new form is created its Text is set as its Name by default. Suppose if I name the form as 'Form1', when its created its Text is set as 'Form1'. Follow link to view screenshot
But when I add a new inherited form in the same way its Text property stays null. Is it normal for it to behave in that way or am I missing something?

The inherited form component is built as undermentioned:
I built a ClassLibrary where a custom form component is created that inherits from the Form class. I simply set the default backcolor of the form there. Its Text property is never overriden. No more complex code is involved. The code follows:

Imports System.Windows.Forms
Imports System.Drawing
Public Class BlueForm
    Inherits Form
    Public Sub New()
        'This call is required by the Component Designer.'
        Me.BackColor = Color.LightSteelBlue
    End Sub
    Public Overrides Property BackColor As System.Drawing.Color
            Return MyBase.BackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.BackColor = value
        End Set
    End Property
End Class

After referencing the ClassLibrary to the actual WinForms project, I add a new inherited form based on BlueForm through the 'Add New Item' dialog box. While adding, I set the name of the form as 'MyBlueForm' and click the 'Add' button. The new form is created as 'MyBlueForm' but its Text remains null while it is supposed to be 'MyBlueForm' just like any other standard windows form. Why didn't that happen?

Hope I could make it more clear this time!!!
Posted 9-Aug-12 7:39am
Wes Aday 9-Aug-12 12:50pm
Why is Text not null? Because you did not set to anything other than null. Every standard windows form Text property is null until you set to be something else. You did not do that so it's null.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The property Text is virtual and can be overridden, but it cannot help you. If you had the property Name virtual, you could override it to modify Text, but it is not virtual.

Actually, there is no default value for Name except null. It becomes non-null when a user of the form class, a developer, assigns a value to it. What you saw is just the designer behavior: an initial value for Text is set to the auto-generated value of Name. If you think that setting Name modifies Text, thing again: you will see that it is not so — if you modify Name, the value of Text won't change. These properties are totally independent, and this is as it is required by designer functionality.

By the way, one practical recommendation: never use auto-generated names. They are ugly, non informative and violate (good) Microsoft naming conventions. It's easy to change all those names to something semantic, and you should always do it. This is easy to do: the Visual Studio refactorization engine does it all without any problems.
I don't see any problem here.

Good luck,
priyamtheone 12-Aug-12 12:32pm
Ok, so in short, setting the Text of a standard windows form as its Name is explicitly done by the VS IDE and is not part of the Form class. The VS IDE doesn't do the same for inherited forms. We have to set the Text by ourselves. That's what I wanted to know. Thanks!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

solution 1 is correct
when we create template/form(class) it will set form's text property
but here you are creating object of form(One kind of class).

you should set text property after creating instance of form run-time

dim MyBlueForm as new BlueForm()

Happy Coding!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100