Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 https://docs.google.com/open?id=0B3aGOxBwxg17MUlreTdZQW1od1U
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()
        MyBase.New()

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

        Me.BackColor = Color.LightSteelBlue
    End Sub

    Public Overrides Property BackColor As System.Drawing.Color
        Get
            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
Comments
[no name] 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.

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,
—SA
 
Share this answer
 
Comments
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!
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

e.g
VB
dim MyBlueForm as new BlueForm()
MyBlueForm.Text="MyBlueForm"
MyBlueForm.show()


Happy Coding!
:)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900