获取子类窗体id?

jinsuo_1986 2009-03-30 03:43:20
我定义了baseForm,然后用mainform去继承这个baseform,然后现在我想获取mainform的name,
我在mainform窗体按钮事件中用this.Name结果得到的是baseform的name,
请问我该如何做才能获得mainform的name??
注:这里说name指的是窗体的id
...全文
109 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjhgzx 2009-03-31
  • 打赏
  • 举报
回复
你可以在你的父类Name属性上加virtual
jinsuo_1986 2009-03-31
  • 打赏
  • 举报
回复
问题已解决!
wartim 2009-03-30
  • 打赏
  • 举报
回复
不过你这个例子好像没什么意义
wartim 2009-03-30
  • 打赏
  • 举报
回复
晕,你属性名字也叫Name,然后
get
{
return this.Name;;
}
里又在取属性自己的值,那不就堆栈溢出了么


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

namespace WindowsApplication3
{

public class BaseForm : Form
{
public BaseForm()
{
this.Name = "baseform";
}

public virtual string FormName
{
get
{
return this.Name; ;
}
set
{
this.Name = value;
}
}
}

public partial class Form1 : BaseForm
{
public Form1()
{
InitializeComponent();
this.Name = "Form1";

MessageBox.Show(FormName);
}

public override string FormName
{
get { return this.Name; }
set { this.Name = value; }
}
}
}


Form1
jinsuo_1986 2009-03-30
  • 打赏
  • 举报
回复
还是不行,会出现异常的!
我现在的代码:

baseform:
public virtual string Name
{
get
{
return this.Name;;
}
set
{
this.Name = value;
}
}
mainform:
public override string Name
{
get { return this.Name; }
set { this.Name = value; }
}


异常:'System.StackOverflowException' のハンドルされていない例外が INGsolution.exe で発生しました。
baiyunyinv 2009-03-30
  • 打赏
  • 举报
回复
this.Name是继承自baseForm,所以你得到的是baseForm的值
zgke 2009-03-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ojekleen 的回复:]
你可以重载这个Name属性,如果不让重载你就覆盖也行。不过这样对多态有些影响,如果你的面向接口编程之类的建议你不要覆盖。
好在你的baseForm也是你定义的,你可以在你的Name属性上加virtual


C# code//在你的mainForm中重写
public override string Name
{
get{return this.id.tostring();}
set{this.id=Convert.Toint32(value);}
}
[/Quote]
覆盖了.
public new string Name
{
get{return this.id.tostring();}
set{this.id=Convert.Toint32(value);}
}
yangqidong 2009-03-30
  • 打赏
  • 举报
回复
你给mainform.Name赋值没有
ojekleen 2009-03-30
  • 打赏
  • 举报
回复
你可以重载这个Name属性,如果不让重载你就覆盖也行。不过这样对多态有些影响,如果你的面向接口编程之类的建议你不要覆盖。
好在你的baseForm也是你定义的,你可以在你的Name属性上加virtual

//在你的mainForm中重写
public override string Name
{
get{return this.id.tostring();}
set{this.id=Convert.Toint32(value);}
}

110,502

社区成员

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

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

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