【问题标题】:Pass data from one form to another将数据从一种形式传递到另一种形式
【发布时间】:2012-10-12 21:46:17
【问题描述】:

今天我记一个问题,在c# winform程序中。

如果有Form AForm B;表格B有一个文本框(需要只读)

Form A 代码如下:

B b = new B("FormB");
b.Show();

FormB 代码如下:

情况1:

public B(string str)
{
   this.textbox1.text = str;

   this.textbox1.Enable = false;
}

我将程序部署到客户服务器,但textbox1.text = "" 没有任何价值,但它可以在我的本地计算机上运行。

我试试这个:

情况2:

public B(string str)
{
    this.textbox1.text = str;

    this.textbox1.ReadOnly = true;        
}

然后texbox1.text = "FormB"; 它可以在我的本地计算机和客户服务器上运行。

关键和重要的问题是,为什么situation1可以在我的本地计算机上运行,​​而不能在客户服务器上运行?

谁能告诉我这是为什么?

【问题讨论】:

  • 在哪里初始化了 formB 的对象
  • public B(string str) { InitializeComponent(); this.textbox1.text=str; this.textbox1.Enable=false; }

标签: c# .net forms


【解决方案1】:

让我看看我对你的理解是否正确,你问为什么ReadOnly = trueEnabled = falseTextBox上的文字会更新?


如果是这样,我想这是设计使然。

来自 MSDN:TextBoxBase.ReadOnly Property

当该属性设置为true时,控件的内容不能 由用户在运行时更改。 将此属性设置为 true,您 仍然可以在代码中设置Text属性的值。你可以使用这个 功能而不是禁用具有 Enabled 属性的控件 允许复制内容并显示工具提示。

【讨论】:

  • 但是,情况1在我的本地计算机上可以正常工作,在客户服务器上不行。如何解释这个?因为操作系统?还是运行时?
  • 您的本地环境和客户服务器环境中是否有不同版本的 .Net 运行?
【解决方案2】:

已经提出并回答了类似的问题...这是我提供的一些示例

One Sample
Another using properties and methods to expose / exchange data -- almost step by step sample

【讨论】:

    【解决方案3】:

    您的构造函数中需要有InitializeComponent() - 与空白构造函数相同。

    http://www.dotnetperls.com/initializecomponent

    【讨论】:

    • 调用基地也应该工作:public B(string str) : base()
    • 当然,我的构造函数包含 InitializeComponent 像这样: public B(string str) { InitializeComponent(); this.textbox1.text=str; this.textbox1.Enable=false; }
    猜你喜欢
    • 2021-08-06
    • 2015-06-23
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多