Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
i want to know how to change my controls properties from another form , i have two form one of them is "Form1" and another is "Form2 " , for example i want to change my Button1.Enable to false in Form1 from Form2 , i tried some methods !
but non of them did not work !
i really need help.

for example if u want to do this it wont work :
in "Form2" :
C#
Form1 form1  = new Form();
form1.Button1.Enabled = false;

i fought that i have to change button to static ! but by my research it's not a good idea ! i think i need to use reference type but i do not know HOW ?

sorry for my bad English !
Posted
Updated 12-Aug-13 4:01am
v5
Comments
CHill60 12-Aug-13 8:20am    
Please use the Improve question link to post at least one of the methods you tried so that we can try to point out where you went wrong
Sridhar Patnayak 12-Aug-13 8:22am    
Why you require this functionality, Also it is not clear.
[no name] 12-Aug-13 8:41am    
The problem is, is that your code does work. But it only works for the instance that you are creating. Once your new instance goes out of scope, that's it. You need to pass the existing instance of Form1 to Form2 for you to do whatever it is that you want to do to work.
Hamed_z 12-Aug-13 9:43am    
i need to pass the existing instance of form1 to form2 ?
i think the problem is this !
but could you bring some code how to do that ?

I would have a read of this

Transferring information between two forms, Part 1: Parent to Child[^]

There is a good series that OriginalGriff has produced
 
Share this answer
 
v2
Comments
Herbisaurus 12-Aug-13 10:38am    
*****
Maciej Los 13-Aug-13 1:51am    
+5!
Hamed_z 18-Aug-13 8:05am    
good idea
thank u for the post ;)
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Comments
Maciej Los 12-Aug-13 17:27pm    
One way of interaction between forms is (...): interfaces, and this one is the most robust, in my opinion - Holy True!
+5!
Sergey Alexandrovich Kryukov 12-Aug-13 17:45pm    
Thank you, Maciej.
—SA
Hamed_z 18-Aug-13 8:03am    
thank u sergey
i solve the problem ;)
Sergey Alexandrovich Kryukov 18-Aug-13 11:30am    
Will you accept my answer formally (green button)?
—SA
Hamed_z 18-Aug-13 15:22pm    
wow ! ok ;)
you can change other form controls properties after create objects of that forms

first of all you have to define public type to the control on form1
then follow the methodology
C#
form1 frm1;
frm1.Button1.Enable=false;

or you may use delegates to do that work.....
 
Share this answer
 
v3
Comments
Hamed_z 12-Aug-13 8:42am    
i tried that method , but nothing changed !

u told i use Delegates ? can u bring example ?
Sergey Alexandrovich Kryukov 12-Aug-13 17:49pm    
Please do yourself a favor and ignore this "answer", it is the total gibberish of someone who has no a clue.
Please see my other comment below.
—SA
Aarti Meswania 12-Aug-13 8:48am    
which form you have open first?
is form2 is opened on form1's button click?
Hamed_z 12-Aug-13 9:03am    
the first one is Form1 that is open
and then i will open Form2 , in Form2 i want to change for example button1.Enabled to false !
the firs Form1 is still open !
[no name] 12-Aug-13 9:07am    
can you place your code here.................
i will help you to manage your code...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900