Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have a SDI based project using C# with 3 forms in it.

- Form1 contains 1 button and 1 label. I wrote a method to change the text in this form.

- Form2 contains 1 button only

- Form 3 contains 1 button and 1 textbox

when user press button in Form1 then Form2 shows as a diagloque, and user press a button in Form2 then Form3 shows as diagloque too. (3 forms open at the same time)

Everytime user type anything in textbox at Form3 then the label in Form1 will have same value. I don't know how can I do it? And if it is possile, please help me!

Thanks,

Tuyen Vu Huy
Posted
Updated 2-Aug-11 18:06pm
v2
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 0:39am    
Use "Improve question". Explain why you have a doubt -- functionality looks trivial.
--SA
BillWoodruff 3-Aug-11 21:27pm    
I'll provide some clues for a solution in my answer here, but I think you should clarify: when you say "shows as a dialogue," some of us may think you mean you are using 'ShowDialog, which shows the Form modally, and others may think you just are describing the function of the Form as a dialogue, or secondary window, but it's still being shown by 'Show. What exactly do you mean when you use the term SDI (Single Document Interface) ? There's a standard meaning (an application like Word, for example), but there are some variations also (ref. Chris Sells' book on WinForms).

Make a property in Form1 and on Form3 Button click assign the Form1's property the textbox value and on Form1 assign textbox.text = Property's value

Hope you have understand :)


Eg

Form1.cs
C#
string tbox;
 public static string Txt
        {
            get
            {
                return tbox;
            }
            set
            {
                tbox= value;
            }

        }



Form3.cs
Form1.Txt =textbox1.Text;


Form1.cs

txt.Text=Txt;
 
Share this answer
 
I suggest 'de-composing' the dependencies here like this:

1. Form1 is the Main Form, the start-up Form.
2. Form2 is shown by Form1, but has no dynamic interaction with Form1
3. Form3 is shown by Form2, and changes in From3's TextBox should be immediately transmitted to Form1's Label. Form3 has no dynamic interaction with Form2

Note that you can view the relationship between Form1 and Form3 as either:

1. Form1 'observes/monitors' what happens in Form3, and updates itself. To 'observe' Form1 needs:
a. a reference to give it access to Form3's TextBox
b. a way to know when the Text on Form3's TextBox changes.

2. or, Form3 'publishes' its TextBox text as it changes, and Form1 'subscribes to the publishing event. For that to happen Form3 must define a 'publicly' visible event that Form1 can subscribe to.

Finally, a qualitatively different approach could be taken by moving all the events, and interactions, between all Forms into a special Class which will handle them, and which will expose Controls and Methods in a more general way. Perhaps think here about the role of the 'Controller' in the MVC paradigm.

One interesting 'dilemma' in this type of problem is that in order to 'hook-up' events, for Form interaction: those Forms need to be not just instantiated ... by new Form#n() ... but actually loaded and shown.

In this particular case, your design, evidently, demands the Forms not be shown until the user has taken some action.

So even if, for example, you create a public property of type TextBox on Form3, until Form3 is shown, you cannot access the actual TextBox control until Form3 is shown. And, nope, you cannot use a 'trick' like setting Form3.Visible = false, and then using 'Show, or 'ShowModal.

However, here's what you can do ... yes, at last, the simple solution, but with a slight hackish smell ...
// in class Form1 body
     Form2 f2;
     Form3 f3;

     private void Form1_Load(object sender, EventArgs e)
     {
         f2 = new Form2();
         f3 = new Form3();

         // note that setting Form2's Visible
         // to false prior to Show will have
         // no effect, and the Form is shown

         // note that using ShowDialog here
         // will not work !
         f2.SuspendLayout();
             f2.Show();
             f2.Visible = false;
         f2.ResumeLayout();

         f3.SuspendLayout();
             f3.Show();
             f3.Visible = false;
         f3.ResumeLayout();

         // note that Form2's Button will be null
         // until Form2 is actually shown !
         f2.f2Button.Click += new EventHandler(f2Button_Click);

         f3.f3TextBox.TextChanged += new EventHandler(f3TextBox_TextChanged);
     }

     private void button1_Click(object sender, EventArgs e)
     {
         f2.ShowDialog();
     }

     private void f2Button_Click(object sender, EventArgs e)
     {
         f3.ShowDialog();
     }

     private void f3TextBox_TextChanged(object sender, EventArgs e)
     {
         label1.Text = f3.f3TextBox.Text;
     }

Looking at the above code as one way to achieve a solution, you should then figure out for yourself exactly how Form2 'exposes' its Button, and Form3 'exposes' its TextBox.

Technically this solution 'injects' event handlers into Form2 and Form3: for that to happen, keep in mind those Forms have to be not just instantiated by 'new,' but created.

In my humble opinion, cases like this take you to the edge where a more general solution starts being worth the struggle, and by that I mean formalizing all event management and Form interaction into a class, which is probably static, and delegating all events into that class which then 'routes' and 'dispatches' them.

best, Bill
 
Share this answer
 
v2
Comments
Asish Limbu 4-Aug-11 0:30am    
Excellent Answer.+5.
dude, if you want transfer a value from one form to another form make the label value in other to internal and then create an object for the form then you can access the label in this form.
 
Share this answer
 
Not enough info.
You said "From3 shows as diagloque too".
I have to assume that 'too' means all 3 forms open at the same time.

Is this project an sdi based with modal dialogs showing and the time?
Is this project an mdi project?

When does the label on form1 need to be updated, when forms 2 and 3 are closed or while forms 2 and 3 are still open?
 
Share this answer
 

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