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

Properties dialog.

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
6 3822
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
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
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rick Austin | last post by:
I recently had to perform a reinstalltion of Windows XP (my registry seems to have become corrupt). After this completed I had to reinstall all applications since most use the registry for settings,...
0
by: David Lindgren | last post by:
I am wondering if it is possible to pop up the fileproperties dialog from inside the code. I've found old methods for doing it which includes ShellExecuteEx and passing a SHELLEXECUTEINFO struct...
2
by: Nick | last post by:
A lot of people say to use properties to pass variables between a main dialog and a subdialog like so: Form form = new Form(); if (form.ShowDialog()==DialogResult.OK) { string str =...
1
by: Jay | last post by:
I am trying to edit a dialog created with msvc 6.0. If I select a control and look in the properties window, there are no properties shown. The drop-down list has 'Selected Object' multiple times...
1
by: DaveS | last post by:
Hi! I am creating a Windows app in VS2005 which will allow the user to choose from different SQL data sources. I could hardcode a selection of connection strings, but I'd rather not (in case a...
4
by: cashdeskmac | last post by:
I have created a control and added a few properties. One of these is an array of DateTime objects, looking like this: public DateTime MyDates { get { return theDate;} set {theDate =...
2
by: Morten Fagermoen | last post by:
Hi! How can I get the printer properties (the same as right click-properties in the Printer menu) in a VB.NET 2005 application. I have the collection of the printers but need to get to the...
1
by: gdgass | last post by:
Hi, I'm in the process of developing an app to scan and archive photos based upon the EXIF/Photo Keyword property. One of the requirements is that the Photo's keywords be editable via Digital...
1
by: =?Utf-8?B?VGVlcmF2ZWU=?= | last post by:
I try to invoke Connection Properties dialog box that Visual Studio 2005 use. The dialog that I said is used in many places such as when developers build connection string in Settings.setting file...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.