access a textbox value from another form

  • Thread starter Thread starter Claudia Fong
  • Start date Start date
C

Claudia Fong

Hi,

I have 2 forms. Form1 has a button which calls form2, but I need to know
the value of the form1.textBox1.text.

When I show form2, I try to use the code bellow:

textBox1.Text = formst.textBox1.Text;

but this generates error -->
WindowsApplication1.frmdisplaystock.textBox1' is inaccessible due to its
protection level

Maybe the better way is to define a global variable, but where should I
declare it?

I put between namespace and public class, but it generates the error -->
expected class,delegate,enum, interface or struc.

Can someone help me?

Cheers!

Claudi
 
Hello,
Define a string property in Form2 and set it in Form1's button click
event handler before displaying the Form2.

Or, if Form2 has the reference to Form1's object then make a string
property in Form1 and access it from Form2.

HTH. Cheers.
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
Hi,

"due to its protection level" ==>

To access textBox2 in Form2 from Form1, set textBox2 as "public" in form2
déclaration (private by default):
public TextBox textBox2;

or use a Set property in form2...

Nicolas Guinet
 
Don't expose controls outside of the form to which they belong.

Instead, define public properties that allow you to get at information
on the form, and name the property according to what it returns:

public class Form1
{
private TextBox textbox1;
... etc. ...

public string SalesOrderNumber
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
}

Of course, you didn't say what "textbox1.Text" represents. I chose
"SalesOrderNumber" just as an example, to show you that you shouldn't
call it something like "TextBox1Text", which is pretty meaningless.
Similarly, if you want something to happen on Form1 and that Form2 be
able to find out when it happens, you should define an event in Form1
that is raised whenever a button is clicked, etc, but the event is
named sensibly, like "SalesOrderNumberChanged" or something like that.

The idea is that from outside Form1, nobody cares _how_ the sales order
number is displayed, or _how_ the user changes it. Outside callers just
look at properties of Form1 and listen for events from Form1 in order
to work with it.

Exposing controls publicly is extremely bad practice: it exposes the
internal workings of your form to the "outside world" and makes it much
more difficult to change that form later.
 
Claudi,

Instead of a global variable, you could pass the contents of textBox1.text
as a parameter to the constructor of Form2... something like:

In Form1's button click event...

Form MyForm2 = new Form(textBox1.text);
MyForm2.ShowDialog();

Then simply use it inside Form2 as a private form level variable...

public Form2
{
private readonly string m_someText;
...

//Form2 Constructor...
public Form2(string someText)
{
m_someText = someText;
}
...
//Now use can use m_someText anywhere in this form.

}
 
You're right in the principle and for complex forms i used to use set/get
but here, she just want the textbox value probably a simple form dialog with
a textbox...and she wants to understand the error message.

It takes 2 seconds for convert private in public and test...more than 1
minute for implement set/get = Time x 60. Programming is a lot of consumer
of "tips".

Again, i agree with you for complex form/project but simple "things" must
stay simple.

Nicolas Guinet
 
Back
Top