Click here to Skip to main content
Rate this: bad
good
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 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 9-Aug-12 6:39am
Comments
Wes Aday at 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
good
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,
—SA
  Permalink  
Comments
priyamtheone at 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
good
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
 
e.g
dim MyBlueForm as new BlueForm()
MyBlueForm.Text="MyBlueForm"
MyBlueForm.show()
 
Happy Coding!
Smile | :)
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 718
1 OriginalGriff 420
2 CPallini 385
3 George Jonsson 231
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,272
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2014
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