【我设置的属性值怎么没了?】

拿棵草 2006-04-25 03:41:21
麻烦大家按我下面的步骤试一下。很奇怪,为什么我设置了属性值,但是运行时却没有了,又变成默认值了。

第一步,新建一个C# Windows Control Project,引用System.Globalization命名空间

第二步,将下面代码替换自动生成的代码:

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

namespace WindowsControlLibrary1
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;

private Person p = null;

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

public UserControl1()
{
p = new Person( "", "", 0 );
InitializeComponent();
}

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()
{
components = new System.ComponentModel.Container();
}
#endregion
}

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

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;}
}
}

internal class PersonConverter : ExpandableObjectConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
{
if (t == typeof(string))
return true;

return base.CanConvertFrom(context, t);
}
public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo info,object value)
{
if (value is string)
{
try
{
string s = (string) value;
int comma = s.IndexOf(',');
if (comma != -1)
{
string last = s.Substring(0, comma);
int paren = s.LastIndexOf('(');
if (paren != -1 && s.LastIndexOf(')') == s.Length - 1)
{
string first = s.Substring(comma + 1, paren - comma - 1);
int age = Int32.Parse(s.Substring(paren + 1,s.Length - paren - 2));
Person p = new Person();
p.Age = age;
p.LastName = last.Trim();
p.FirstName = first.Trim();
return p;
}
}
}
catch {}
throw new ArgumentException("Can not convert '" + (string)value + "' to type Person");
}
return base.ConvertFrom(context, info, value);
}
public override object ConvertTo(ITypeDescriptorContext context,CultureInfo culture,object value,Type destType)
{
if (destType == typeof(string) && value is Person)
{
Person p = (Person)value;
return p.LastName + ", " + p.FirstName + " (" + p.Age.ToString() + ")";
}
return base.ConvertTo(context, culture, value, destType);
}
}
}

第三步: 在一个窗体上使用这个控件,然后在属性页面里,设置这个控件的PEOPLE属性,然后运行,你会发现,刚刚给PEOPLE属性赋的值消失了。

就是不懂这是为什么。
请高手指点一二。

注:这段程序是引用网上的一个例子。
...全文
80 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
拿棵草 2006-04-26
  • 打赏
  • 举报
回复
To xzhunter(xz):
谢谢你的回答。你能把你修改后,没有问题的代码发给我看看么?我照你说的试了,还是不行啊。
MyLf 2006-04-25
  • 打赏
  • 举报
回复
帮顶。。

还没做过这样的东东。应该少了什么东西,以至于不能保存设置。
xzhunter 2006-04-25
  • 打赏
  • 举报
回复
把private Person p = null;

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

=============================
或者把
p = new Person( "", "", 0 );
InitializeComponent();

改成
InitializeComponent();
p = new Person( "", "", 0 );


==================================
你这个问题很简单,实际上设计器帮你初始化了属性,你看看Design代码就知道了
拿棵草 2006-04-25
  • 打赏
  • 举报
回复
To copico(苦涩):
那你知道是什么问题么? 谢谢。
copico 2006-04-25
  • 打赏
  • 举报
回复
是控件的问题吧

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧