0

I have a windows application which has three forms : Form1,2,3.

--Form 1, has two buttons , openform2, openform3. --Form2 has a textbox form2_textbox,and button form2_button --Form3 has a textbox form3_textbox

now, on clicking button openform2 on form1, Form2 opens, a string is entered in textbox form2_textbox of Form2, now when bu tton form2_button of this form is clicked, then i want that Form1 receives this string value & stores it in a string receivefromform2, and then displays this string value on to form3_textbox of Form3.

please guide me how to do this task?

2 Answers 2

4

Ignore the fact that they're forms. Think of them as any other objects - you'd use properties, methods, events and constructors. GUI controls have a few oddities around them, mostly in terms of thread affinity, but usually you should apply the same object oriented approaches to them as you would anything else.

For example, when constructing Form2 in Form1, add an event handler to the button in Form2 (either by creating a new event in Form2 or exposing the button via a property and attaching it directly). The event handler would ask Form2 for the text in the textbox, and use that when creating Form3.

Sign up to request clarification or add additional context in comments.

6 Comments

@Jon Skeet , Dear Sir, thanks a lot for your reply , can you give the code for this? i am still confused?
@sqlchild: I'm afraid I don't have the time to do that now. Which exact bit is confusing you?
@Jon Skeet, how to create event handler in Form2? i have created button click event, but what to write in that?
@sqlchild: I didn't say to create an event handler in Form2 - I said to either create an event in Form2, or expose the button via a property so that Form1 can subscribe directly to the button's click event. Please read my article about events for more information: csharpindepth.com/Articles/Chapter2/Events.aspx
@Jon Skeet, dear sir, can you give me the code please, am totally messed up, since 10 hours am stuck up with this question without any solution
|
1

There are a couple of ways to do this. Using .NET events is one but requires pretty coupled wiring.

What I would suggest (and this is how I do this) is to use your own bus (observer pattern). Let's call it MessageBus. All your forms could use the same instance of this bus and when something interesting happens you could publish some Message. I would keep it strongly typed but for the sake of simplicity let's say this:

public class Message<T> 
{ 
    public string Name { get; set; }
    public T Data { get; set; }
}

You would then have subscribers on your bus that respond to messages that they are interested in.

public class MessageBus
{
    public void Subscriber(ISubscriber subsriber)
    {
         // register your subscriber in some list
    }

    public void Publish(Message message)
    {
         // loop through subscribers and let them know
         // e.g. subscriber.Handle(message);
    }
}

So to wire all this up each form that would like to publish an event (such as your form2) would need a reference to the message bus and each object that is interested in receiving events (such as form3) would register as a subscriber.

The only difference between this an using .NET events is that the various publishers and subscribers do not need to know about each other so they are loosely coupled --- they only need to know about the bus. It is possible to get the same loose coupling using .NET events but that takes a lot of fancy footwork.

More than one way to skin a cat I suppose.

I have a more mature implementation of this in my composite ui framework I use for the tooling on our FOSS service bus. You can take a look if you are interested:

Shuttle Service Bus on CodePlex

If you download the source you will find it in the Shuttle.Core.UI project.

Hope it makes sense.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.