473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

access a textbox value from another form



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
5 30851
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul T. Rong | last post by:
Hello there, I have tried all day but in vain....here is the problem: Form1: there are 50 labels (so far, the number is subject to change), each label caption is a fruit or a vegetable, for...
1
by: RD | last post by:
How can I get a value from a textbox on another form? Thanks RD
1
by: Sam Miller | last post by:
I have several text boxes like FirstName, LastName, etc; the usual. When the page is loaded I fill in the values from a member record. Like: Session("FirstName") =...
5
by: Boki | last post by:
Hi All, There are two forms, when some click happen, it fires to set form2's viable as enable. The form2 is for user to input some text and then the data need to be collected into form1's...
2
by: Ric Pascual | last post by:
Hello Everyone. Having a problem on this one. I like to have the default value of a field in one table to be equal to the value of a field in an exisitng record in another table. Hopefully...
11
by: Gilberto | last post by:
Hello I have two forms: Projects and Engineering In PROJECTS i have a textbox (VARIANT1) where the user will enter some text. I want this text to automatically appear as text on another TEXTBOX...
2
by: Gilberto | last post by:
Hello, this is my situation: On the first form (PROJECTS) i have a textbox (PROJECTNAME) which the user enters when opening the database. On a second form (ENGINEERING) i have a list of...
3
by: vzrandom | last post by:
Hi guys, I have one easy question for you. I have class with public static variable. I enter value in one form (in this public static variable) and when I passed it to another form in textbox I...
1
by: sprad | last post by:
I try to copy value of textbox from one form to another textbox of another form when checkbox is checked. So did anybody have idea how to do it.Please reply me as soon as possible. Thanks..
2
by: Rob Dunkley | last post by:
i get the error "An object reference is required for the non-static field, method, or property" when i try to use form2.textbox1.text i have two forms main and advanced. in advanced there is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.