PC Review


Reply
Thread Tools Rate Thread

Design (gui)

 
 
csharpula csharp
Guest
Posts: n/a
 
      27th Nov 2007
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 ***
 
Reply With Quote
 
 
 
 
Peter Duniho
Guest
Posts: n/a
 
      27th Nov 2007
On 2007-11-27 01:10:22 -0800, csharpula csharp <(E-Mail Removed)> said:

> 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

 
Reply With Quote
 
csharpula csharp
Guest
Posts: n/a
 
      27th Nov 2007
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 ***
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      27th Nov 2007
On 2007-11-27 02:56:14 -0800, csharpula csharp <(E-Mail Removed)> said:

> 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.

 
Reply With Quote
 
csharpula csharp
Guest
Posts: n/a
 
      28th Nov 2007

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 ***
 
Reply With Quote
 
bob clegg
Guest
Posts: n/a
 
      28th Nov 2007
On Wed, 28 Nov 2007 00:03:14 -0800, csharpula csharp
<(E-Mail Removed)> wrote:

>
>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
 
Reply With Quote
 
csharpula csharp
Guest
Posts: n/a
 
      28th Nov 2007
How do formB is aware of that event? How do I connect the both forms?
Thank you!



*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
bob clegg
Guest
Posts: n/a
 
      29th Nov 2007
On Wed, 28 Nov 2007 01:45:13 -0800, csharpula csharp
<(E-Mail Removed)> wrote:

>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 PremiseMapEventArgs:EventArgs
{



private IEnumerable<KeyValuePair<Int32,Premise>> pList;
public PremiseMapEventArgs(IEnumerable<KeyValuePair<Int32,
Premise>> pCol)
{
pList = pCol;
}
public IEnumerable<KeyValuePair<Int32,Premise>> PremisesToMap
{
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 PremiseMapEventHandler(object sender,
PremiseMapEventArgs e); //Delegate

public event PremiseMapEventHandler MapPremise; // Event declaration
of type previous declared

Generating the event with data destined for FormA

private void MapUnGeocodedPremise_Click(object sender, EventArgs e)
{
IEnumerable<KeyValuePair<Int32,Premise>> pCol = new
Dictionary<Int32,Premise>(); // Make the dictionary
foreach (Premise premise in lbUngeocoded.SelectedItems)
{
((Dictionary<Int32,Premise>)pCol).Add(premise.ID,premise);//Fill the
Dictionary
}
PremiseMapEventArgs m = new PremiseMapEventArgs(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 geocodeToolStripMenuItem_Click(object sender, EventArgs
e)//User wants to create FormB

{
ThreadStart ts = RunGeocode;
Thread t = new Thread(ts);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}

private void RunGeocode()// makeFormB
{
frmGeoCode f;
IEnumerable<Route> RouteList;
if(!lRouteSelected)
{
f = new frmGeoCode(); // make a plain second form
}

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


f.myForm = this;// ***Allows FormB to consume events reaised
f.MapPremise += new frmGeoCode.PremiseMapEventHandler(f_MapPremise);
//*****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(object sender, PremiseMapEventArgs e)
{
CurrentPremises = e.PremisesToMap; // get the dictionary
out of the carrier

PlacePremise(CurrentPremises); // 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(IEnumerable<KeyValuePair<Int32,Premise>> p)
{

if (this.lbPlacingPremises.InvokeRequired) //Want to
update a listbox
{
dUpDatePlacingString d = UpdatelbPlacingPremises; //
**********delegate with correct signature declared elsewhere
i.e. private delegate void
dUpDatePlacingString(IEnumerable<KeyValuePair<Int32,Premise>> p);
***********
this.lbPlacingPremises.Invoke(d, new object[] { p
});//Listbox invokes the delegate
}
else
UpdatelbPlacingPremises(p);// Don't ever get here but
heh.


}

private void
UpdatelbPlacingPremises(IEnumerable<KeyValuePair<Int32,Premise>> p)
{
foreach (Premise premise in
((Dictionary<Int32,Premise>)p).Values)
{
lbPlacingPremises.Items.Add(premise) ;
}
}

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


 
Reply With Quote
 
csharpula csharp
Guest
Posts: n/a
 
      30th Nov 2007


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 ***
 
Reply With Quote
 
Andrus
Guest
Posts: n/a
 
      30th Nov 2007
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 ***



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design/Pattern guidance to refector my current design for unit testing sklett Microsoft C# .NET 1 22nd Aug 2007 12:58 PM
how do i apply an Axis design or radial design from design templa. =?Utf-8?B?bmFkaWE=?= Microsoft Powerpoint 1 3rd Apr 2005 02:21 AM
Looking for help/resources on Writing a nice detailed design / tech design for vb.net code SpamProof Microsoft VB .NET 4 1st Dec 2003 06:06 AM
Looking for help/resources on Writing a nice detailed design / tech design for vb.net code SpamProof Microsoft C# .NET 3 1st Dec 2003 06:06 AM
Looking for help/resources on Writing a nice detailed design / tech design for vb.net code SpamProof Microsoft Dot NET 2 27th Nov 2003 08:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:41 PM.