.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

Passing Data in between windows forms

Posted By: jokertin92     Posted Date: March 28, 2010    Points:2   Category :Windows Application
Hi, clear me that how can we access the text box, buttons of Form1 in Form2 in windows application using C#

Responses
Author: Amit Mehra             
Posted Date: March 28, 2010     Points: 5   

Hi,

There are many ways you can pass data between one form to another in windows application. Below is the 2ways

1 way) Overload the the constructor

say form1 is having textbox and you want to pass the data to form2

then in that case overload the form2 constructor to accept the value. and pass the value fro form1 like this

Form2 frm1 = new Form2(TextBox1.Text.trim())

2 Way) Declare the properties in form2 and pass the values from From1 while calling form2.

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Lalij Mer             
Posted Date: March 28, 2010     Points: 5   

Hi,

.You will get idea from this link,please check this


http://msdn.microsoft.com/en-us/library/f6525896.aspx
http://www.daniweb.com/forums/thread167238.html#

Please Check my answer if you help....
Thank You...

Lalit.
Author: jokertin92             
Posted Date: March 29, 2010     Points: 5   

Hi lalit,
Thank you finally I got it. The thing I missed is I didn't make the textbox as public.

But, now I am getting one more doubt, if I have 15 textboxes in a Form how can i change all the things of a Form as public

Author: Dilip             
Posted Date: March 29, 2010     Points: 5   

Hi Jokertin,

I would suggest not to make textboxes public in order to pass the data...That is not a good sign of coding. Why because controls should never be made public.

You can create a separate class with properties and access the properties in the new form when loaded.

Let me know if you need the code.

Regards
Dilip

Regards
Dilip
Author: jokertin92             
Posted Date: March 29, 2010     Points: 5   

Yes Dilip, give me the code. But, clear if I have more text boxed, buttons... it's such an irritating thing for creating properies isn't?

Author: Dilip             
Posted Date: March 30, 2010     Points: 5   

Can you tell me how your app should behave? You can tweak your code based on the logic how you think?

Let me know what you are trying to achieve in the code.

Regards
Dilip
Author: jokertin92             
Posted Date: March 30, 2010     Points: 5   

Yes Dilip,
I would like to create an application for a company which is used to maintain the accounts (expenditure) information of it's all branches. And there must be the facility for creating reports.

What I planned to do is,
* After the login form, If there is no table in the branch name, a table will be created by the login name(i.e branch name) with a default column"S.No" and it must be displayed (May be using a Datagrid)
* We need to manually add columns according to the user wishes and these changes has to be reflected
immediately in the data grid
* then adding the expenditure details
* save into database
* a button to prepare this data as a report (May be as an excel)

I know it's a very simple program, But I am a newbie for programming as well as C#.. So, I am struggling a lot.. Help me

Author: Dilip             
Posted Date: March 30, 2010     Points: 5   

Well I cannot visualize properly...I bielieve this will work for you

Not sure how to attach the file for a praticular reply, Here you go with the sample code:

MyClass.cs
----------------

public class MyClass
{
private static string[] strValues;
public static string[] GetValue
{
get
{
return strValues;
}
set
{
strValues = value;
}
}
}

Form1.cs
-----------

private void button1_Click(object sender, EventArgs e)
{
String[] p = new String[2] { "Hello", "Mello"};
MyClass.GetValue = p;

Form2 f2 = new Form2();
f2.Show();
}

Form2.cs
-----------
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = MyClass.GetValue[0].ToString();
textBox2.Text = MyClass.GetValue[1].ToString();
}

Regards
Dilip


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend