473,786 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to access the values of form1 control to form2

13 New Member
Hi,
I have two form i my project. Form1 contains a text box with public access modifiers and a button.
and Form 2 contains a label and a button.
the following task i am trying to do.
when some one enter some text in text box and click the button it will show the form two and its bit easy task.
But the problem is that I want to access the values of text box(Form1) to Form2 when i will click the button on form 2 but getting error the code is
Expand|Select|Wrap|Line Numbers
  1. //Form1 Button Code
  2. private void button1_Click(object sender, EventArgs e)
  3.         {
  4.             if (textBox1.Text != "")
  5.             {
  6.                 frm.Show();
  7.  
  8.             }
  9.         }
  10. //form 2 code
  11. public partial class Form2 : Form
  12.     {
  13.         public Form2()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private void button1_Click(object sender, EventArgs e)
  19.         {
  20.             label1.Text = Form1.ActiveForm.Controls["textBox1"].Text.ToString();
  21.         }
  22.     }
  23.  
Aug 10 '10 #1
5 5890
fastestindian
74 New Member
You need to instance of the of the Form2.
When u create you have to create the object of Form2 which you can store globaly.

Create a property in that class in Form2.
Expand|Select|Wrap|Line Numbers
  1. public string PropNearest        {            get            {                return Nearest.Text;            }            set            {                Nearest.Text = value;            }        }        public string PropNearest
  2.         {
  3.             get
  4.             {
  5.                 return Nearest.Text;
  6.             }
  7.             set
  8.             {
  9.                 Nearest.Text = value;
  10.             }
  11.         }
Whenever you needed the value of the Form2 text box then just type Form2.PropNeare st;

Best of luck....
Aug 10 '10 #2
ezhar
13 New Member
i want value from form1
Aug 11 '10 #3
mcfly1204
233 New Member
So you have form1 with a textbox that contains a value and you want to create an instance of form2 from within form1, and then reference that value of the text box in form1?
Aug 11 '10 #4
ezhar
13 New Member
yes i want to do the same
Aug 13 '10 #5
mcfly1204
233 New Member
I think this is correct, and textbox1 should be public.

Expand|Select|Wrap|Line Numbers
  1. Form1 form = (Form1)Application.OpenForms["Form1"];
  2.  
  3. label1.Text = form.textbox1.Text.ToString();
Aug 13 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1207
by: Altex | last post by:
Hi, I've got the name of a control (mycontrol3) in string format string cotrolname = "mycontrol3"; how can I access to the control instead of doing this mycontrol3.Height = 100; I'd like to do some thing like this (but in the right way)
2
3959
by: Stan | last post by:
This is how I access Pieces field in my editable datargid during Update event: protected void grdMain_OnUpdate(Object sender, DataGridCommandEventArgs e) { string Pieces = ((TextBox) e.Item.FindControl ("txtPieces")).Text; .... }
3
1847
by: Antuane | last post by:
hi guys, its me again, with a few simple (probably silly) quesetion... hi guys, i've managed to create a custom textbox class. & i'd like to access the parent control (in this case this would be the form, cuz i drop the textbox onto the form), in the constructor of the custom textbox class. i'd like to access the parent control in the constructur of the textbox. But i can't do this, cuz i get an error, saying that the parent property...
5
14180
by: John | last post by:
Hi, I can't find a simple example for a simple(?) problem. I am working on an application with a variable in form1, that variable is needed in form2 for a calculation but i can't get that variable in form2. Is there a simple method (in VSexpress2005) to get that specific variable in form2? TIA,
5
7285
by: RedHair | last post by:
I have a ASP.NET 2.0 web page which inherits a customized base page class and have a master page, their relation is as below Customized base page class --> web page --> master page How to access the property and control of master page from the customized base page class? Btw, I can access the server control of master page from base page class via
2
2846
by: Svein Erik | last post by:
C# 2.0 How can i update my progress bar on Form1, from Form2? Form 2 contains a button that is supposed to update the progressbar on Form1 by 5 in value. I can't find out how to do this, please help me with some code samples :) Thanks!
0
1136
by: Manoj Mittal | last post by:
hi I want to access the Html control i.e Textbox .which is in seperate file and want to access in seperate ASP.NET file .. how I can access the control of Html control in ASP.NetPage.. Suggest me. Mail me this code on manoj.mcans@gmail.com
1
3509
by: cdm2883 | last post by:
/bin/sh/ tty: can't access tty; job control turned off how do i get past this?
1
1544
by: ashwinigopi | last post by:
I have two forms, form1 & form 2. Form1 has datagridview. I am not supposed use any sql command in form1. So i added form2 and wrote the sql commands using stored procedures to get the data from the table. The problem is the datagridview is unavailable in form2. I donno how to link datagridview from form1 and form2 or either way.I am using c#.net
1
1370
by: avinash73 | last post by:
how to access values in database via combobox
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.