Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 135,963 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,524 people online right now. Registration is fast and FREE... Join Now!




In C# 2005, .Net 2.0,

 
Reply to this topicStart new topic

In C# 2005, .Net 2.0,

hhuntman
23 May, 2008 - 08:45 AM
Post #1

New D.I.C Head
*

Joined: 23 May, 2008
Posts: 2

How can I change the properties of controls on another form. For example, with a label control on Form 1, how can I change its text property from Form 2?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: In C# 2005, .Net 2.0,
23 May, 2008 - 12:34 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well remember that controls are private members of a form class. So to get access to them you either have to set them up as public or expose them through a property. There are advantages to both methods and it will depend on your design as to which way you want to go with it. If you just want to make them public, then select the control on the second form and go to the properties window to "Modifiers" and change it from private to public. Then back on form1 you will have access to the label on form2.

Enjoy! smile.gif
User is online!Profile CardPM
+Quote Post

djkitt
RE: In C# 2005, .Net 2.0,
23 May, 2008 - 01:01 PM
Post #3

D.I.C Head
**

Joined: 22 May, 2008
Posts: 128



Thanked: 13 times
My Contributions
If you want to access Form1 from Form2 then set the label on Form1 as public (or make a property or a public method) and when you create your Form2 instance set its Owner property to Form1...



CODE

//Assuming you have made Label1 public in Form1...

//In Form1
Form2 f2 = new Form2()
f2.Owner = this; // 'this' would be Form1
f2.ShowDialog();

//In Form2
((Form1) f2.Owner).Label1.Text = "Some Text";


This post has been edited by djkitt: 23 May, 2008 - 01:03 PM
User is offlineProfile CardPM
+Quote Post

hhuntman
RE: In C# 2005, .Net 2.0,
27 May, 2008 - 11:58 AM
Post #4

New D.I.C Head
*

Joined: 23 May, 2008
Posts: 2

QUOTE(djkitt @ 23 May, 2008 - 02:01 PM) *

If you want to access Form1 from Form2 then set the label on Form1 as public (or make a property or a public method) and when you create your Form2 instance set its Owner property to Form1...



CODE

//Assuming you have made Label1 public in Form1...

//In Form1
Form2 f2 = new Form2()
f2.Owner = this; // 'this' would be Form1
f2.ShowDialog();

//In Form2
((Form1) f2.Owner).Label1.Text = "Some Text";



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:36AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month