473,382 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 ***
Nov 27 '07 #1
9 1286
On 2007-11-27 01:10:22 -0800, csharpula csharp <cs*******@yahoo.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

Nov 27 '07 #2
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 ***
Nov 27 '07 #3
On 2007-11-27 02:56:14 -0800, csharpula csharp <cs*******@yahoo.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.

Nov 27 '07 #4

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 ***
Nov 28 '07 #5
On Wed, 28 Nov 2007 00:03:14 -0800, csharpula csharp
<cs*******@yahoo.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
Nov 28 '07 #6
How do formB is aware of that event? How do I connect the both forms?
Thank you!

*** Sent via Developersdex http://www.developersdex.com ***
Nov 28 '07 #7
On Wed, 28 Nov 2007 01:45:13 -0800, csharpula csharp
<cs*******@yahoo.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 PremiseMapEventArgs:EventArgs
{

private IEnumerable<KeyValuePair<Int32,Premise>pList;
public PremiseMapEventArgs(IEnumerable<KeyValuePair<Int32 ,
Premise>pCol)
{
pList = pCol;
}
public IEnumerable<KeyValuePair<Int32,Premise>PremisesToM ap
{
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,p remise);//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<RouteRouteList;
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,Premis e>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<Int3 2,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<I nt32,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
Nov 29 '07 #8


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 ***
Nov 30 '07 #9
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 ***

Nov 30 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Steven O. | last post by:
I am seeking some kind of tool that I can use for GUI prototyping. I know how to use Visual Basic, but since a lot of software is being coded in Java or C++, I'd like to learn a Java or C++ -based...
0
by: Terry Hancock | last post by:
Newcomers to Blender (3D modelling/animation program) often find its fairly unique UI a bit off-putting, but on closer inspection, I find it's a very compelling design for "power users" (i.e....
2
by: seash | last post by:
H My application consists of three layers, presentaion layer(PL), Business Logic layer(BLL) and Data Access layer(DAL) my application is a single windows form application now BLL consits of...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
6
by: Javaman59 | last post by:
This must be a common GUI design issue - whether to treat the GUI object which represents a thing as if it were the thing itself. I'll put it in the form which I've come across recently... I...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
12
by: cmk128 | last post by:
Hi I am an os developer, i am developing a gui library. I found that java swing is the most easiest to use gui library , am i correct? I am trying to port swing to c++, is there any thing i...
6
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.