Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am working with two forms with one button and one text box on each.i want on click of button 1 in form 1 color of both text box should change.
Posted
Comments
BillWoodruff 15-Oct-14 1:07am    
Is one Form the Main Form, and the Main Form creates the instance of the second Form ?
Member 11154071 15-Oct-14 3:08am    
thanx..any good book??..i have just started working in c#

You need the "Observer Pattern". You can implement it with events: Form1 fires an event when its Button1 was clicked, Form2 subscribes to that event, and changes the color of its Textbox when it receives the event.
That is, the thing you have to study here are Events and Event Handling in C#.
 
Share this answer
 
Comments
BillWoodruff 15-Oct-14 4:30am    
This concept requires that the instance of Form2 has a reference to the Button in the instance of Form1 in order to subscribe to its Click EventHandler, or that Form1 define a public Event raised in the Button's Click EventHandler which means Form2 must have a reference to the instance of Form1 to subscribe to the Form1 Event.

Both ideas create a coupling between Form and Form2 that is "tighter" than necessary: Form2 will "know more than it needs to know" about Form1 which creates a potential for problems.

We are missing some vital information here: which of the two Forms, if any, is the Main Form here, and where exactly are both Forms created.
first create one class like this
public class Class1
   {
       public string color { get; set; }
   }


Create two forms (form1 and form2)

write this code in form1 button click event
private void button1_Click(object sender, EventArgs e)
     {
         Class1 c = new Class1();
         c.color = "red";
         textBox1.BackColor = System.Drawing.Color.Red;
         Form2 f2 = new Form2(c);
         f2.Show();
     }


Modify the constructor of form2
XML
public Form2(Class1 c) // constructor
        {
            InitializeComponent();

            if (c.color == "red")
                textBox1.BackColor = System.Drawing.Color.Red;
        }
 
Share this answer
 
Comments
BillWoodruff 15-Oct-14 2:07am    
You assume here the OP wants to create a new instance of Form2 every time the Button Click EventHandler on Form1 is executed. That is certainly incorrect: in your code, once the Click event was executed, the instance of Form2 created would be visible, but would have no reference to it, and could not interact with in code in any way.
Samatha Reddy G 15-Oct-14 2:12am    
sorry am not getting you, i was executed this code that's why i gave the solution. for me it is working perfectly.
actully using this code we can send form 1 values to form2

if am wrong please correct me
BillWoodruff 15-Oct-14 2:16am    
Yes, your code runs without error, but it is clearly not relevant to what the OP is seeking here. Why would anyone create a secondary Form in a Button Click and not keep a reference to it: it's effectively useless, then.
Samatha Reddy G 15-Oct-14 2:24am    
What did you understand from that question can you please explain me?

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