By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,077 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,077 IT Pros & Developers. It's quick & easy.

Properties dialog.

P: n/a
I'm fairly new to c#, and I am just trying to work out how a
'properties' dialog works.

Currently in my code, I have an object represented by the class 'Dog'.
The dog object has several properties.

I have added a new windows form, class 'DogProperties'. I'm attempting
to use this to allow the user to edit the properties of an instance of
'Dog'.

To do this, I'm passing an instance of 'Dog' as a ref parameter to the
constructor of 'DogProperties'. The problem is, updated properties can
not be sent back, as the ref is only valid for the constructor.

An example of the existing code I have would be

// Create an instance of our properties dialog
DogProperties propertyDialog = new DogProperties(ref myDog);
// Any changes made to myDog must have occured in the constructor, but
// we haven't even displayed the dialog yet!
properyDialog.ShowDialog;

I understand that this isn't the correct way of doing this, as the
constructor doesn't modify myDog, so passing a ref is a waste of time.
What I'm wondering is, how do other people go about achieving this
task?

I'm guessing that you would derive the Dog class form a Windows.Form
class, with a method of this class used to display the form element? If
so, would it be good practise to make this method static (only one
properties dialog can be open at a time anyway)?

As I said earlier, I'm fairly new to c#, so please excuse me if I'm
missing the obvious.

Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Okay, I think it's starting to make sense now. I've just merged my two
classes together, and I use the derived Windows.Form ShowDialog()
method to launch the dialog. I've made the form controls static
objects, to save memory, as there will only ever be a single instance
of the form.

Nov 17 '05 #2

P: n/a
I would say to create a dialog that is "Dog-aware" in the sense that it
knows how to visually display the elements of a Dog object. Then create a
property on the DogProperties dialog that holds a Dog object. Create an
instance of the DogProperties dialog, assign an instance of the Dog class to
the appropriate property, and then display the DogProperties dialog using
ShowDialog. This would be similar to how the PageSetupDialog is set up.
http://msdn.microsoft.com/library/de...tingstopic.asp

--
Tim Wilson
..Net Compact Framework MVP

<br*********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I'm fairly new to c#, and I am just trying to work out how a
'properties' dialog works.

Currently in my code, I have an object represented by the class 'Dog'.
The dog object has several properties.

I have added a new windows form, class 'DogProperties'. I'm attempting
to use this to allow the user to edit the properties of an instance of
'Dog'.

To do this, I'm passing an instance of 'Dog' as a ref parameter to the
constructor of 'DogProperties'. The problem is, updated properties can
not be sent back, as the ref is only valid for the constructor.

An example of the existing code I have would be

// Create an instance of our properties dialog
DogProperties propertyDialog = new DogProperties(ref myDog);
// Any changes made to myDog must have occured in the constructor, but
// we haven't even displayed the dialog yet!
properyDialog.ShowDialog;

I understand that this isn't the correct way of doing this, as the
constructor doesn't modify myDog, so passing a ref is a waste of time.
What I'm wondering is, how do other people go about achieving this
task?

I'm guessing that you would derive the Dog class form a Windows.Form
class, with a method of this class used to display the form element? If
so, would it be good practise to make this method static (only one
properties dialog can be open at a time anyway)?

As I said earlier, I'm fairly new to c#, so please excuse me if I'm
missing the obvious.

Nov 17 '05 #3

P: n/a
Tim,

I continued with my Dog class that was derived from
System.Windows.Forms as it seemed to be working and because I didn't
fully understand what you were explaining. But now I've hit a problem
with serialization. Because my Dog class is now derived from
system.windows.forms, it can't be serialized. So it looks like I'm
going to have to take your advice. I'm not entirely sure how to
implement it, but I've been thinking about this and I suspect I need to
use something like this:
// Create an instance of the DogProperties dialog
DogProperties dogProperties = new DogProperties();

// Populate the dog property
dogProperties.dog = myDogs[x];

// Show the dialog
dogProperties.ShowDialog();

// Retrieve the updated properties and store them back in the array
myDogs[x] = dogProperties.dog;
Is it really this simple, or am I barking (sorry) up the wrong tree?

Nov 17 '05 #4

P: n/a
It seems that it really is that simple. For some reason I never thought
of using the Dog object as a property. Seems totally obvious now.
(blush)

Thanks Tim.

Nov 17 '05 #5

P: n/a
Your Dog class is derived from System.Windows.Forms.Form? What is the
intended purpose of the Dog class? Your code looks good other than the fact
that you should not need to push the dog information back into the array -
if it's a reference type then, unless something tricky is being done at the
other end such as cloning the dog instance, the information should already
be in the proper location. So the following line *shouldn't* be necessary.
// Retrieve the updated properties and store them back in the array
myDogs[x] = dogProperties.dog;
Here's some code that may be along the lines of what you're looking for.

[SerializableAttribute()]
public class DogInformation
{
private string breedValue = null;
private int ageValue = -1;

public string Breed
{
get
{
return breedValue;
}
set
{
if (breedValue != value)
{
breedValue = value;
}
}
}

public int Age
{
get
{
return ageValue;
}
set
{
if (ageValue != value)
{
ageValue = value;
}
}
}

public DogInformation()
{
}

public DogInformation(string breed, int age)
{
this.Breed = breed;
this.Age = age;
}
}

....

// Place this code in the definition for the Form that displays
// the DogProperties dialog.
public void DisplayDogProperties()
{
DogInformation dog = new DogInformation("Terrier", 6);
DogProperties dialog = new DogProperties();
dialog.Dog = dog;
if (dialog.ShowDialog() == DialogResult.OK)
{
// The user has changed the "dog" instance.
}
dialog.Dispose();
}

....

// Place this code in the definition for the DogProperties dialog.
private DogInformation dogInformationValue = null;
public DogInformation Dog
{
get
{
return dogInformationValue;
}
set
{
if (dogInformationValue != value)
{
dogInformationValue = value;
}
}
}
// Place something similar to the code below in the definition for
// the DogProperties dialog. The "txtBreed" TextBox and the
// "txtAge" TextBox were used to allow the user to provide the
// Breed and Age - these should be changed to the appropriate
// controls or values.
private void btnOK_Click(object sender, System.EventArgs e)
{
if (this.Dog != null)
{
this.Dog.Breed = this.txtBreed.Text;
this.Dog.Age = Int32.Parse(this.txtAge.Text);
}
}

You'll also need to ensure that the DogProperties dialog contains two
Buttons - one to OK the changes and one to Cancel the changes. The Click
event of the OK Button should be connected to the "btnOK_Click" handler that
I've defined above. The AcceptButton property of the DogProperties dialog
should be set to the OK Button and the CancelButton property of the
DogProperties dialog should be set to the Cancel Button. The DialogResult
property of the OK Button should be set to "OK" and the DialogResult
property of the Cancel Button should be set to "Cancel". I think that's
pretty much everything. Anyways, take a look at the code above and hopefully
this will give you a better idea of which direction to go.

--
Tim Wilson
..Net Compact Framework MVP

<br*********@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com... Tim,

I continued with my Dog class that was derived from
System.Windows.Forms as it seemed to be working and because I didn't
fully understand what you were explaining. But now I've hit a problem
with serialization. Because my Dog class is now derived from
system.windows.forms, it can't be serialized. So it looks like I'm
going to have to take your advice. I'm not entirely sure how to
implement it, but I've been thinking about this and I suspect I need to
use something like this:
// Create an instance of the DogProperties dialog
DogProperties dogProperties = new DogProperties();

// Populate the dog property
dogProperties.dog = myDogs[x];

// Show the dialog
dogProperties.ShowDialog();

// Retrieve the updated properties and store them back in the array
myDogs[x] = dogProperties.dog;
Is it really this simple, or am I barking (sorry) up the wrong tree?

Nov 17 '05 #6

P: n/a
> Your code looks good other than the fact that you should not need to
push the dog information back into the array - if it's a reference type
I initially tried using ref in the constructor (which obviously doesn't
work), I haven't tried using ref since I've got the dog object passed
as a property, hence the reason why I'm currently reloading the object
into the array. However, I suppose giving the parameter as a ref would
be a neater solution to what I have at present.
Your Dog class is derived from System.Windows.Forms.Form?


My Dog class is no longer derived from System.Windows.Forms, although I
did try this for a short time (see my second post in the thread).

It's now working well, thanks to the help you have given me.

Thanks.
Bry.

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.