Design (gui)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csharpula csharp

    #1

    Design (gui)

    Hello,
    I have few 2 forms which share common methods and update of one member
    of form 1 - affect member in form 2 . I thought about placing this
    mutual functionality in one class and the question is how they will use
    it? Both of them create it or use one instance?
    Thank you!

    *** Sent via Developersdex http://www.developersdex.com ***
  • Peter Duniho

    #2
    Re: Design (gui)

    On 2007-11-27 01:10:22 -0800, csharpula csharp <csharpula@yaho o.comsaid:
    I have few 2 forms which share common methods and update of one member
    of form 1 - affect member in form 2 . I thought about placing this
    mutual functionality in one class and the question is how they will use
    it? Both of them create it or use one instance?
    That all depends on what this "mutual functionality" does.

    If the functionality is entirely stateless, you don't need even one
    instance. Just make the code all static and call it via the class name.

    If the functionality has state that makes more sense as put into a
    class instance, but that instance can be shared amongst all users, then
    you may prefer to make the class a singleton. Google Groups will help
    you with the specifics :), but the basic idea is that you set up a
    static property that returns the single instance of the class that will
    be used.

    Finally, if the class has state, but that state is best maintained
    separately for each object that uses the class, you'll want to create a
    new instance of the class for each object that uses it.

    Pete

    Comment

    • csharpula csharp

      #3
      Re: Design (gui)

      The scenario is form1 control affects property "a" that affect on
      property "b" in form2.what is the best option? Thank you!



      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Peter Duniho

        #4
        Re: Design (gui)

        On 2007-11-27 02:56:14 -0800, csharpula csharp <csharpula@yaho o.comsaid:
        The scenario is form1 control affects property "a" that affect on
        property "b" in form2.what is the best option? Thank you!
        Nothing about your description suggests "shared functionality". I'm
        afraid it doesn't do anything to help me understand better what you're
        trying to solve.

        Comment

        • csharpula csharp

          #5
          Re: Design (gui)


          I will try to explain better : I got FormA which has a combo box
          control . User will choose from combobox and this way will affect on
          what will be presented on FormB. I created a mutual class for all the
          "behind form" methods. How can I share data between form? By making this
          class and the methods static?
          Thank you!

          *** Sent via Developersdex http://www.developersdex.com ***

          Comment

          • bob clegg

            #6
            Re: Design (gui)

            On Wed, 28 Nov 2007 00:03:14 -0800, csharpula csharp
            <csharpula@yaho o.comwrote:
            >
            >I will try to explain better : I got FormA which has a combo box
            >control . User will choose from combobox and this way will affect on
            >what will be presented on FormB. I created a mutual class for all the
            >"behind form" methods. How can I share data between form? By making this
            >class and the methods static?
            >Thank you!
            >
            >*** Sent via Developersdex http://www.developersdex.com ***
            Hi,
            You may want to consider generating an event on formA that is handled
            by formB.
            The selected index changed handler on your combox raises the event
            which is carrying the data that you want to move.
            The event handler on FormB consumes the event and sets the FormB GUI
            accordingly.
            Bob

            Comment

            • csharpula csharp

              #7
              Re: Design (gui)

              How do formB is aware of that event? How do I connect the both forms?
              Thank you!



              *** Sent via Developersdex http://www.developersdex.com ***

              Comment

              • bob clegg

                #8
                Re: Design (gui)

                On Wed, 28 Nov 2007 01:45:13 -0800, csharpula csharp
                <csharpula@yaho o.comwrote:
                >How do formB is aware of that event? How do I connect the both forms?
                >Thank you!
                >
                >
                >
                >*** Sent via Developersdex http://www.developersdex.com ***
                Hi,
                1) Derive a class from Eventargs to hold your data.
                e.g. Real code snippet, the data is a dictionary

                ****Event Class Code*********
                public class PremiseMapEvent Args:EventArgs
                {



                private IEnumerable<Key ValuePair<Int32 ,Premise>pList;
                public PremiseMapEvent Args(IEnumerabl e<KeyValuePair< Int32,
                Premise>pCol)
                {
                pList = pCol;
                }
                public IEnumerable<Key ValuePair<Int32 ,Premise>Premis esToMap
                {
                get { return pList; }
                set { pList = value; }
                }
                }

                2) On the form say FormB, that needs to tell the other form whats
                happened, create an event.

                *****FORMB Event Generator Code*********

                public delegate void PremiseMapEvent Handler(object sender,
                PremiseMapEvent Args e); //Delegate

                public event PremiseMapEvent Handler MapPremise; // Event declaration
                of type previous declared

                Generating the event with data destined for FormA

                private void MapUnGeocodedPr emise_Click(obj ect sender, EventArgs e)
                {
                IEnumerable<Key ValuePair<Int32 ,Premise>pCol = new
                Dictionary<Int3 2,Premise>(); // Make the dictionary
                foreach (Premise premise in lbUngeocoded.Se lectedItems)
                {
                ((Dictionary<In t32,Premise>)pC ol).Add(premise .ID,premise);//Fill the
                Dictionary
                }
                PremiseMapEvent Args m = new PremiseMapEvent Args(pCol); //
                Make a new carrier and put in the data.
                MapPremise(this , m);// Raise the event. Handled by formA
                }




                3) On the form that needs to know what's happening (FormA) consume
                the event.

                In this case I am creating FormB from FormA. The event thing can
                work either or both ways.

                *****FORMA Consumer Code follows*******
                private void geocodeToolStri pMenuItem_Click (object sender, EventArgs
                e)//User wants to create FormB

                {
                ThreadStart ts = RunGeocode;
                Thread t = new Thread(ts);
                t.SetApartmentS tate(ApartmentS tate.STA);
                t.Start();
                }

                private void RunGeocode()// makeFormB
                {
                frmGeoCode f;
                IEnumerable<Rou teRouteList;
                if(!lRouteSelec ted)
                {
                f = new frmGeoCode(); // make a plain second form
                }

                else
                {
                RouteList = new List<Route>();
                foreach (Route r in lbRoutes.Select edItems)
                {
                ((List<Route>)R outeList).Add(r );
                }
                f= new frmGeoCode(Rout eList); make a new second form
                with preloaded data
                }


                f.myForm = this;// ***Allows FormB to consume events reaised
                f.MapPremise += new frmGeoCode.Prem iseMapEventHand ler(f_MapPremis e);
                //*****DECLARING EVENT HANDLER FOR CONSUMING EVENT FROM FORMB using
                method 'f_MapPremise' ********

                Application.Run (f);// Run FormB
                }

                Make use of the incoming data in event from FormB .

                void f_MapPremise(ob ject sender, PremiseMapEvent Args e)
                {
                CurrentPremises = e.PremisesToMap ; // get the dictionary
                out of the carrier

                PlacePremise(Cu rrentPremises); // Make use of the
                dictionary
                }

                Last point is that because event handlers are on a different thread to
                the GUI you will need to invoke any methods that update the GUI .

                private void PlacePremise(IE numerable<KeyVa luePair<Int32,P remise>p)
                {

                if (this.lbPlacing Premises.Invoke Required) //Want to
                update a listbox
                {
                dUpDatePlacingS tring d = UpdatelbPlacing Premises; //
                **********deleg ate with correct signature declared elsewhere
                i.e. private delegate void
                dUpDatePlacingS tring(IEnumerab le<KeyValuePair <Int32,Premise> p);
                ***********
                this.lbPlacingP remises.Invoke( d, new object[] { p
                });//Listbox invokes the delegate
                }
                else
                UpdatelbPlacing Premises(p);// Don't ever get here but
                heh.


                }

                private void
                UpdatelbPlacing Premises(IEnume rable<KeyValueP air<Int32,Premi se>p)
                {
                foreach (Premise premise in
                ((Dictionary<In t32,Premise>)p) .Values)
                {
                lbPlacingPremis es.Items.Add(pr emise) ;
                }
                }

                Obviously the data can be anything,it just happened to be a dictionary
                in some code I was working on recently.
                This whole approach is covered in the MSDN library search for 'custom
                events and delegates'

                hth
                Bob


                Comment

                • csharpula csharp

                  #9
                  Re: Design (gui)



                  The scenario is clear but how do I implement such a thing is there is
                  no connection between the two forms? Thanks again:)!

                  *** Sent via Developersdex http://www.developersdex.com ***

                  Comment

                  • Andrus

                    #10
                    Re: Design (gui)

                    You can use the sample which uses cast and interface which I posted to you
                    in other your thread.

                    Andrus.
                    The scenario is clear but how do I implement such a thing is there is
                    no connection between the two forms? Thanks again:)!
                    >
                    *** Sent via Developersdex http://www.developersdex.com ***

                    Comment

                    Working...