Saving Class Object Properties in a UserControl

G

Guest

If you change the font property on a Form, it adds/changes the code in the
form to something like this:
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));

I've created a new property on a usercontrol that stores/returns a class
object called PersonDetails. I want the property value to be stored simular
to the Font property settings.
e.g.
this.myControl.PersonDetails = new PersonDetails(10, "My Name", 54,
PersonSex.Male);

What do I need to do to the usercontrol and/or PersonDetails class to make
it save the property this way?

Thank you for your help.

Regards
Mark
 
G

Grande

I assume you have your private data already set up in your
PersonDetails class?

private int _personID;
private string _name;
etc, etc.

To do what you're expecting, set up a constructor in PersonDetails to
take in all the necessary data.

public PersonDetails(int NewPersonID, string NewUserName...)
{
_personID = NewPersonID;
_name = NewUserName;
etc, etc.
}

Hope this helps,
- Matt
 
G

Guest

I've done that, but it still doesn't work. I was wondering if there was an
attribute I had to add to the class?

Regards
Mark
 
G

Grande

Can you paste your get/set code for myControl.PersonDetails? I think
the problem might be there.
 
G

Guest

I thought I'd take a step back and create a simple example using the Person
usercontrol code example on the MSDN website
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/vsnetpropbrow.asp).

The PersonControl has a property called Person that returns/stores a Person
object. I still couldn't get it to save the value in the form as
this.personControl.Person = new Person(21, "John", "Smith");

I've paste the code for each file into seperator posts.

Thanks for your help.

Regards
Mark
 
G

Guest

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace PersonDetails
{
/// <summary>
/// Summary description for PersonDetailsControl.
/// </summary>
public class PersonControl : System.Windows.Forms.UserControl
{

private Person p = new Person(0,"","");

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public PersonControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitializeComponent call

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// PersonControl
//
this.Name = "PersonControl";

}
#endregion

public Person Person
{
get
{
return p;
}
set
{
this.p = value;
}
}
}
}
 
G

Guest

using System;
using System.ComponentModel;

namespace PersonDetails
{
[TypeConverter(typeof(PersonConverter))]
public class Person
{
private string firstName = "";
private string lastName = "";
private int age = 0;

public Person(int age,
string firstName,
string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

public int Age
{
get
{
return age;
}
set
{
age = value;
}
}

public string FirstName
{
get
{
return firstName;
}
set
{
this.firstName = value;
}
}

public string LastName
{
get
{
return lastName;
}
set
{
this.lastName = value;
}
}
}
}
 
G

Guest

using System;
using System.ComponentModel;

namespace PersonDetails
{
internal class PersonConverter : TypeConverter
{
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context,
object value,
Attribute[] filter)
{
return TypeDescriptor.GetProperties(value, filter);
}

public override bool GetPropertiesSupported(
ITypeDescriptorContext context)
{
return true;
}
}
}
 
G

Guest

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PersonDetails
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private PersonDetails.PersonControl personControl1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.personControl1 = new PersonDetails.PersonControl();
this.SuspendLayout();
//
// personControl1
//
this.personControl1.Location = new System.Drawing.Point(112, 48);
this.personControl1.Name = "personControl1";
this.personControl1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.personControl1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top