By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,494 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

access a textbox value from another form

P: n/a


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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2

P: n/a
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

"Claudia Fong" <cd********@yahoo.co.uk> a écrit dans le message de news:
uq*************@TK2MSFTNGP12.phx.gbl...


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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #3

P: n/a
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.

Nov 17 '05 #4

P: n/a
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.

}
"Claudia Fong" <cd********@yahoo.co.uk> wrote in message
news:uq*************@TK2MSFTNGP12.phx.gbl...


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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #5

P: n/a
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
"Bruce Wood" <br*******@canada.com> a écrit dans le message de news:
11*********************@g14g2000cwa.googlegroups.c om...
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.

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.