473,398 Members | 2,088 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,398 software developers and data experts.

Creating a preference window

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?

May 10 '07 #1
11 1745
On Thu, 10 May 2007 13:36:57 -0700, melon <el*****@gmail.comwrote:
[...]
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
May 10 '07 #2
PS

"melon" <el*****@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
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
May 10 '07 #3
On Thu, 10 May 2007 14:31:58 -0700, PS <ec***********@hotmail.comwrote:
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.
May 11 '07 #4
"melon" <el*****@gmail.comschrieb im Newsbeitrag
news:11**********************@h2g2000hsg.googlegro ups.com...
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
May 11 '07 #5
On Fri, 11 May 2007 07:22:40 -0700, Christof Nordiek <cn@nospam.dewrote:
>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
May 11 '07 #6
PS

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 10 May 2007 14:31:58 -0700, PS <ec***********@hotmail.comwrote:
>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)
{
}
}
May 12 '07 #7
On Sat, 12 May 2007 05:46:11 -0700, PS <ec***********@hotmail.comwrote:
>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
May 12 '07 #8
PS

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sat, 12 May 2007 05:46:11 -0700, PS <ec***********@hotmail.comwrote:
>>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
May 12 '07 #9
On Sat, 12 May 2007 11:54:46 -0700, PS <ec***********@hotmail.comwrote:
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.
May 13 '07 #10

"Peter Duniho" <Np*********@nnowslpianmk.comschrieb im Newsbeitrag
news:op***************@petes-computer.local...
On Fri, 11 May 2007 07:22:40 -0700, Christof Nordiek <cn@nospam.dewrote:
<snip>
>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)
Surely you can implement it in that way. But there are fundamental reason,
why you shouldn't.
First: Making the field public, allows other classes not only to access the
control, but even to assign another control to that variable.
Second and more important: The PreferenceForm has to know, where to store
the results in its calling form. This strongly reduces the reusability of
that form and the maintainability of the code.

PreferenceForm should only be responsible for getting some input of the user
and the calling form should represent that changes in its controls.

Christof
May 14 '07 #11
On Mon, 14 May 2007 00:24:33 -0700, Christof Nordiek <cn@nospam.dewrote:
Surely you can implement it in that way. But there are fundamental
reason, why you shouldn't.
First: Making the field public, allows other classes not only to access
the control, but even to assign another control to that variable.
Second and more important: The PreferenceForm has to know, where to store
the results in its calling form. This strongly reduces the reusability of
that form and the maintainability of the code.
Note that when I wrote that statement, I was under the incorrect
impression that the "private CustomClass" was a class declaration rather
than a field declaration. I have no good explanation for why I misread
the code in such an obviously wrong way. But I did. :)

I certainly agree that a field should not be public, as well as with most
of the rest of what you wrote. I agree that the PreferenceForm should not
be strongly tied to the calling form's class.

That said, to some extent a "preferences" dialog form is always going to
be custom to at least some class of forms, if not to a specific form. So
while I agree that fields shouldn't be made public, I don't think it's
terrible for the PreferenceForm to have *some* knowledge of the calling
form (such as using a specific shared class to communicate the data). As
an example, one idea I've been kicking around is having the PreferenceForm
public an interface that the calling form is required to implement, and
which the calling form passes to the PreferenceForm.

I don't have any great reasons for thinking this is better than having the
data flow the other direction (it has the "preferences" dialog manage the
data flow, while the alternative has the calling form manage the data
flow), but I also don't have any great reasons for thinking it's worse in
any way. One way or the other, the calling form has to implement some
sort of "glue" to connect to the "preferences" dialog form, so it seems to
me that whether it implements an interface or accesses properties on the
dialog form or whatever, it's all the same in the end. :)

Pete
May 14 '07 #12

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

Similar topics

5
by: Keiron Waites | last post by:
<script language="JavaScript" type="text/javascript"> <!-- var array1 = new Array(); var array1i = new Array(); array1 = array1i; alert(array1); // --> </script>
22
by: Tom Moroow | last post by:
Hi, I'm pretty new to javascript and was wondering how you would piece together a variable name and then assign it a value. I want to create a hidden field and assign it a value based on the value...
4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
24
by: jonathon | last post by:
Hi all, I have a web app with a popup window for entering data. I don't want to access the web every time this window is opened, as most of the app is AJAX. But I can't figure out how to open...
8
by: David W. Simmonds | last post by:
Is there an easy way to create a new browser window from C# and ASP.NET? I would just like to have a popup window without any menus or toolbars that would contain a high-res image. The low-res...
3
by: Trex | last post by:
My question is on the design of a web app...let's say I was tracking requests for something and I wanted my web app to show information about the different requests in the system. A list of...
13
by: Grace | last post by:
Hi, How do I create/access an INI file in VB .NET? I appreciate any help you can give me... Thanks!
5
by: John Scott | last post by:
Ok..this a rather odd question/problem. I haven't really found a straight forward answer to how to handle this scenario, so I hope someone here can help. Here it is: I have an application...
53
by: Gianni Mariani | last post by:
Do you have a preference on maximum line width for C++ code? I've seen the craziest debates on this most silly of topic. I have witnessed engineers spent oodles of time fiddling with line...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.