0

Today I account a question, In c# winform program.

if there are Form A and Form B; Form B has a textbox (need to read only)

The Form A code like this:

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

The FormB code like this:

Situation1:

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

   this.textbox1.Enable = false;
}

I deploy the program to customer server, but the textbox1.text = "", have no value, but it works in my local computer.

I try this:

Situation2:

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

    this.textbox1.ReadOnly = true;        
}

Then texbox1.text = "FormB"; it works in my local computer and customer server.

The key and important question is ,why the situation1 can works my local computer not works in customer server?

Can anybody tell this is why?

jack.li
  • 973
  • 1
  • 9
  • 20

3 Answers3

2

Let me see if I understand you correctly, you are asking why the text on the TextBox is updated when you use ReadOnly = true and not Enabled = false?


If so, I guess it's by design.

From MSDN: TextBoxBase.ReadOnly Property

When this property is set to true, the contents of the control cannot be changed by the user at runtime. With this property set to true, you can still set the value of the Text property in code. You can use this feature instead of disabling the control with the Enabled property to allow the contents to be copied and ToolTips to be shown.

Mario S
  • 11,715
  • 24
  • 39
  • 47
  • but ,the situation1 can work fine in my local computer,not work in customer server.how to explain this? because of OS? or runtime? – jack.li Oct 12 '12 at 13:42
  • Are there different versions of .Net running on your local environment and the customer server env.? – Mario S Oct 12 '12 at 14:01
1

Similar questions have been asked and answered... Here are some samples I've provided

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

Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142
0

You need to have InitializeComponent() in your constructor - the same as the blank constructor.

http://www.dotnetperls.com/initializecomponent

LukeHennerley
  • 6,344
  • 1
  • 32
  • 50
  • Calling the base should work too: `public B(string str) : base()` – Joel Etherton Oct 12 '12 at 12:29
  • of course ,my constructor inlcude InitializeComponent like this: public B(string str) { InitializeComponent(); this.textbox1.text=str; this.textbox1.Enable=false; } – jack.li Oct 12 '12 at 13:38