新创建的继承表单的Text属性显示为null(VS.Net 2010,WinForms). [英] Text property of a newly created inherited form shows null (VS.Net 2010, WinForms).

查看:97
本文介绍了新创建的继承表单的Text属性显示为null(VS.Net 2010,WinForms).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过添加新项"对话框将新的标准Windows窗体添加到应用程序时,系统会提示您在名称"字段中输入新窗体的名称.创建新表单时,默认情况下将其文本"设置为其名称".假设我将表单命名为"Form1",则在其创建时将其文本"设置为"Form1".按照链接查看屏幕截图 https://docs.google.com/open?id=0B3aGOxBwxg17MUlreTdZQW1od1U
但是,当我以相同的方式添加新的继承表单时,其Text属性保持为null.以这种方式表现是正常的还是我错过了一些东西?

继承的表单组件的构建如下所述:
我构建了一个ClassLibrary,在其中创建了一个自Form类继承的自定义表单组件.我只是在那里设置表单的默认背景色.它的Text属性永远不会被覆盖.不再涉及复杂的代码.代码如下:

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



在将ClassLibrary引用到实际的WinForms项目之后,我通过添加新项"对话框添加了一个基于BlueForm的新继承表单.添加时,我将表单的名称设置为"MyBlueForm",然后单击添加"按钮.新窗体创建为"MyBlueForm",但与其他任何标准Windows窗体一样,它的Text仍为null,而它应该是"MyBlueForm".为什么没有发生?

希望这次我可以使它更清楚!!



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!!!

推荐答案

属性Text是虚拟的,可以被覆盖,但不能为您提供帮助.如果您具有Name虚拟属性,则可以覆盖它以修改Text,但它不是虚拟属性.

实际上,除了null之外,没有Name的默认值.当表单类的用户(开发人员)为其分配值时,该值将变为非null.您所看到的仅仅是设计者的行为:Text的初始值设置为自动生成的Name值.如果您认为设置Name会修改Text,那又是一件事情:您会发现事实并非如此—如果您修改Name,则Text的值不会改变.这些属性是完全独立的,这是设计人员功能所必需的.

顺便说一句,一个实用的建议:永远不要使用自动生成的名称.它们是丑陋的,无用的信息,并且违反了(良好的)Microsoft命名约定.将所有这些名称更改为某种语义很容易,您应该始终这样做.这很容易做到:Visual Studio重构引擎可以毫无问题地完成所有任务.
我在这里看不到任何问题.

祝你好运,
—SA
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


解决方案1是正确的
当我们创建模板/表单(类)时,它将设置表单的文本属性
但是在这里,您正在创建表单(一种类)的 object .

您应该在创建表单运行时实例后设置text属性

例如
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!
:)


这篇关于新创建的继承表单的Text属性显示为null(VS.Net 2010,WinForms).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆