PC Review


Reply
Thread Tools Rate Thread

Creating a preference window

 
 
melon
Guest
Posts: n/a
 
      10th May 2007
Let's say, I have a form class...

public class MainForm : Form {
private CustomClass cc;

public MainForm() {
DoSomething()
InitializeComponent();
}
/// Do something more
public void ShowPreference() {
PreferenceForm pf = new PreferenceForm();
pf.sometextbox.text = cc.something;
/// etc
pf.ShwoDialog();
}
}

public class PreferenceForm: Form {
/// another form
}

Basically, when the user try to close the Preference form, I want it
to be able to modify my Custom class in my main form directly.... how
could I do that?

 
Reply With Quote
 
 
 
 
Peter Duniho
Guest
Posts: n/a
 
      10th May 2007
On Thu, 10 May 2007 13:36:57 -0700, melon <(E-Mail Removed)> wrote:

> [...]
> Basically, when the user try to close the Preference form, I want it
> to be able to modify my Custom class in my main form directly.... how
> could I do that?


As long as that class is private to your MainForm class, you can't have
the PreferenceForm modify it directly.

One way is to make the class public and allow the PreferenceForm to keep a
reference to an instance of that class. The reference would be given to
the PreferenceForm by your MainForm class before showing the dialog (by
passing it in the constructor for PreferenceForm, by setting a property,
by calling a method, whatever), and then the PreferenceForm could access
it that way. A slight variation on this theme is to just make a public
interface (probably declared by the settings dialog class, but you could
put it anywhere that is accessible to both) that your private class can
expose and pass to the settings dialog.

Another way is to expose dialog input as properties in the dialog's class
and then have the client of that class use those to apply them when I use
the dialog. This requires a little more typing, but it means that the
settings dialog isn't tied to the client's class. In many cases, the
settings in the dialog are so specific to the client that having it be
aware of the client isn't really a problem, but sometimes you'll have a
dialog that is more general-purpose. Being in the habit of keeping the
two seperated means that you can always reuse the dialog when appropriate,
without having to do additional design work.

Any of these methods seem acceptable to me. Which one works best for you
depends mostly just on what seems to fit in best with the rest of your
architectural style.

Pete
 
Reply With Quote
 
PS
Guest
Posts: n/a
 
      10th May 2007

"melon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Let's say, I have a form class...
>
> public class MainForm : Form {
> private CustomClass cc;
>
> public MainForm() {
> DoSomething()
> InitializeComponent();
> }
> /// Do something more
> public void ShowPreference() {
> PreferenceForm pf = new PreferenceForm();
> pf.sometextbox.text = cc.something;
> /// etc
> pf.ShwoDialog();
> }
> }
>
> public class PreferenceForm: Form {
> /// another form
> }
>
> Basically, when the user try to close the Preference form, I want it
> to be able to modify my Custom class in my main form directly.... how
> could I do that?


Pass custom class as a parameter in your PreferenceForm constructor.

PS


 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      11th May 2007
On Thu, 10 May 2007 14:31:58 -0700, PS <(E-Mail Removed)> wrote:

> Pass custom class as a parameter in your PreferenceForm constructor.


It's a private class. It can't be passed to the PreferenceForm
constructor, because the PreferenceForm class doesn't have access to it.
 
Reply With Quote
 
Christof Nordiek
Guest
Posts: n/a
 
      11th May 2007
"melon" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Let's say, I have a form class...
>
> public class MainForm : Form {
> private CustomClass cc;
>
> public MainForm() {
> DoSomething()
> InitializeComponent();
> }
> /// Do something more
> public void ShowPreference() {
> PreferenceForm pf = new PreferenceForm();
> pf.sometextbox.text = cc.something;
> /// etc
> pf.ShwoDialog();
> }
> }
>
> public class PreferenceForm: Form {
> /// another form
> }
>
> Basically, when the user try to close the Preference form, I want it
> to be able to modify my Custom class in my main form directly.... how
> could I do that?


The PreferenceForm shouldn't modify the CustomClass.
The PreferenceForm should have a property, wich after closing returns the
result. Then the MainForm can modify its components itself. So the
PreferenceForm simply fetches input from the user, and the Mainform decides
what to do with that input.

Christof


 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      11th May 2007
On Fri, 11 May 2007 07:22:40 -0700, Christof Nordiek <(E-Mail Removed)> wrote:

>> public class MainForm : Form {
>> private CustomClass cc;
>>
>> [...]
>>
>> Basically, when the user try to close the Preference form, I want it
>> to be able to modify my Custom class in my main form directly.... how
>> could I do that?

>
> The PreferenceForm shouldn't modify the CustomClass.


Note that this is strictly a matter of personal preference. There is no
fundamental reason you cannot implement it that way if you want (though in
this case, the access modifier for CustomClass would have to be changed to
allow it)

Pete
 
Reply With Quote
 
PS
Guest
Posts: n/a
 
      12th May 2007

"Peter Duniho" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Thu, 10 May 2007 14:31:58 -0700, PS <(E-Mail Removed)> wrote:
>
>> Pass custom class as a parameter in your PreferenceForm constructor.

>
> It's a private class. It can't be passed to the PreferenceForm
> constructor, because the PreferenceForm class doesn't have access to it.


You are saying that this will not work?

public class MainForm : Form {
private CustomClass cc;

public MainForm() {
DoSomething()
InitializeComponent();
}
/// Do something more
public void ShowPreference() {
PreferenceForm pf = new PreferenceForm(cc);
pf.sometextbox.text = cc.something;
/// etc
pf.ShwoDialog();
}
}

public class PreferenceForm: Form {
/// another form

public PreferenceForm(CustomClass cc)
{
}
}


 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      12th May 2007
On Sat, 12 May 2007 05:46:11 -0700, PS <(E-Mail Removed)> wrote:

>> It's a private class. It can't be passed to the PreferenceForm
>> constructor, because the PreferenceForm class doesn't have access to it.

>
> You are saying that this will not work?


Yes, that's what I'm saying. You are trying to access the CustomClass
type from your PreferenceForm class, but it's private to the MainForm
class and so not visible to the PreferenceForm class.

Pete
 
Reply With Quote
 
PS
Guest
Posts: n/a
 
      12th May 2007

"Peter Duniho" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Sat, 12 May 2007 05:46:11 -0700, PS <(E-Mail Removed)> wrote:
>
>>> It's a private class. It can't be passed to the PreferenceForm
>>> constructor, because the PreferenceForm class doesn't have access to it.

>>
>> You are saying that this will not work?

>
> Yes, that's what I'm saying. You are trying to access the CustomClass
> type from your PreferenceForm class, but it's private to the MainForm
> class and so not visible to the PreferenceForm class.


No it isn't. Reread the code. cc is an INSTANCE not the class definition.

PS


 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      13th May 2007
On Sat, 12 May 2007 11:54:46 -0700, PS <(E-Mail Removed)> wrote:

> No it isn't. Reread the code. cc is an INSTANCE not the class definition.


Ah. Okay, you're right. I misread the code. Sorry about that.
 
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
Window Preference =?Utf-8?B?bW1henVy?= Windows Vista Performance 1 9th Aug 2007 08:58 PM
printer preference window doesn't show. =?Utf-8?B?bW9qb21vam8=?= Windows Vista Print / Fax / Scan 4 10th May 2007 02:29 AM
desktop window 'on top' preference. =?Utf-8?B?bWljaGFlbA==?= Windows XP Customization 1 10th Oct 2006 02:29 PM
window with printing preference too small =?Utf-8?B?VGlt?= Windows XP Help 3 21st May 2005 03:21 PM
Window.Opener Creating A Refresh In Parent Window Steve Wark Microsoft ASP .NET 3 28th Oct 2004 10:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:27 AM.