Design (gui)

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

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!
 
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
 
The scenario is form1 control affects property "a" that affect on
property "b" in form2.what is the best option? Thank you!
 
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.
 
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!
 
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!
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
 
How do formB is aware of that event? How do I connect the both forms?
Thank you!
 
How do formB is aware of that event? How do I connect the both forms?
Thank you!
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
 
The scenario is clear but how do I implement such a thing is there is
no connection between the two forms? Thanks again:)!
 
You can use the sample which uses cast and interface which I posted to you
in other your thread.

Andrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top