resx,designer和cs文件如何在表单中传递值?

问题描述

以Windows形式。 form.resx中有一个xml数据

<data name="$this.Text" xml:space="preserve">
  <value>Report</value>
</data>

所以在form.designer.cs

public System.Windows.Forms.ListView report;
private void InitializeComponent()
{
this.report.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.report_ColumnClick);
}

在form.cs

private void report_ColumnClick(Object eventSender,ColumnClickEventArgs eventArgs)
        {
            if (this.Text != "Report")
            {
                //Some code
            }
        }

问题是,如何在form.cs中识别form.resx中的值。如何在desginer和CS文件中识别出this.text

解决方法

Form.csForm.designer.cs之间的关系由类的名称和partial关键字确定。

您可以将类分为多个部分或文件,只要为该类赋予相同的名称并在其前面添加partial关键字即可,在编译时,编译器会将其视为一个大类。 例如。

Forms.cs 文件

partial class Form
{
 //contain all the implementation code for the form
 //all the code added by the programmer
}

Form.designer.cs 文件

partial class Form
{
// contains all the auto generated code
// contains the InitializeComponent() method
}

编译后,编译器会将以上两个文件都视为Form的1类。

对于.resx文件,see this answer

.resx文件还可以帮助Visual Studio在设计时跟踪在设计时要显示的值。

如果要更改代码中的this.Text,可以在表单Load事件中进行。 例如。

private void Form1_Load(object sender,EventArgs e)
{
    string oldText = this.Text; //oldText will be 'Report' or 'Form1'
    this.Text = "whatever you want it to be";
}