Properties dialog.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bryanhobson@gmail.com

    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(r ef myDog);
    // Any changes made to myDog must have occured in the constructor, but
    // we haven't even displayed the dialog yet!
    properyDialog.S howDialog;

    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.

  • bryanhobson@gmail.com

    #2
    Re: Properties dialog.

    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.

    Comment

    • Tim Wilson

      #3
      Re: Properties dialog.

      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.


      --
      Tim Wilson
      ..Net Compact Framework MVP

      <bryanhobson@gm ail.com> wrote in message
      news:1117978458 .734936.270460@ g49g2000cwa.goo glegroups.com.. .[color=blue]
      > 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(r ef myDog);
      > // Any changes made to myDog must have occured in the constructor, but
      > // we haven't even displayed the dialog yet!
      > properyDialog.S howDialog;
      >
      > 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.
      >[/color]


      Comment

      • bryanhobson@gmail.com

        #4
        Re: Properties dialog.

        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.d og = myDogs[x];

        // Show the dialog
        dogProperties.S howDialog();

        // Retrieve the updated properties and store them back in the array
        myDogs[x] = dogProperties.d og;


        Is it really this simple, or am I barking (sorry) up the wrong tree?

        Comment

        • bryanhobson@gmail.com

          #5
          Re: Properties dialog.

          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.

          Comment

          • Tim Wilson

            #6
            Re: Properties dialog.

            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.
            [color=blue]
            > // Retrieve the updated properties and store them back in the array
            > myDogs[x] = dogProperties.d og;[/color]

            Here's some code that may be along the lines of what you're looking for.

            [SerializableAtt ribute()]
            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 DisplayDogPrope rties()
            {
            DogInformation dog = new DogInformation( "Terrier", 6);
            DogProperties dialog = new DogProperties() ;
            dialog.Dog = dog;
            if (dialog.ShowDia log() == DialogResult.OK )
            {
            // The user has changed the "dog" instance.
            }
            dialog.Dispose( );
            }

            ....

            // Place this code in the definition for the DogProperties dialog.
            private DogInformation dogInformationV alue = null;
            public DogInformation Dog
            {
            get
            {
            return dogInformationV alue;
            }
            set
            {
            if (dogInformation Value != value)
            {
            dogInformationV alue = 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(obj ect sender, System.EventArg s e)
            {
            if (this.Dog != null)
            {
            this.Dog.Breed = this.txtBreed.T ext;
            this.Dog.Age = Int32.Parse(thi s.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_Clic k" 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

            <bryanhobson@gm ail.com> wrote in message
            news:1118094906 .766957.59000@g 43g2000cwa.goog legroups.com...[color=blue]
            > 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.d og = myDogs[x];
            >
            > // Show the dialog
            > dogProperties.S howDialog();
            >
            > // Retrieve the updated properties and store them back in the array
            > myDogs[x] = dogProperties.d og;
            >
            >
            > Is it really this simple, or am I barking (sorry) up the wrong tree?
            >[/color]


            Comment

            • bryanhobson@gmail.com

              #7
              Re: Properties dialog.

              > Your code looks good other than the fact that you should not need to[color=blue]
              > push the dog information back into the array - if it's a reference type[/color]

              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.
              [color=blue]
              > Your Dog class is derived from System.Windows. Forms.Form?[/color]

              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.

              Comment

              Working...