首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

有难度的自定义控件TabControl有关问题,这个有关问题解决了,你在下海月薪可达8800

2012-10-24 
有难度的自定义控件TabControl问题,这个问题解决了,你在上海月薪可达8800!如果你能解决这个问题,那么就能

有难度的自定义控件TabControl问题,这个问题解决了,你在上海月薪可达8800!
如果你能解决这个问题,那么就能证明你是一个骨灰级的大鸟了!
所以依你的能力在上海一定可以拿8K+以上
切入正题:
1.winform里大家都用过tabcontrol控件吧,当我在设计的时候,拖放一个TabControl到窗体上去,设计器会自动给这个TabControl加两个TabPage,这是什么原因?

2.如果解决了上面这个问题,那么下面这个就比较简单了.
我继承TabControl类写了一个自定义控件叫做MyTabControl,
同时我继承了TabPage类写了一个自定义控件叫做MyTabPage,给他加了一个属性Menu
我的问题是:在我进行界面设计的时候,我拖放一个MyTabControl,设计器会自动给我添加两个标准的TabPage,从而导致TabPage到MyTabPage类型的转换错误,因为我在MyTabControl类里调用TabPage的类型都是MyTabPage,我现在想要实现的效果是,在设计界面里,拖一个MyTabControl到窗体上去如何能默认加入MyTabPage,同时通过TabPage集合编辑器加入的也是MyTabPage而不是标准的TabPage?

[解决办法]
HI不做winform ,只能帮你顶下了。
[解决办法]
看了我的工资在上海这么高啊?可实际咋没呢?555555
[解决办法]
第一个问题直接问微软,为什么要默认加2个tabpage
[解决办法]
既然它自己加了2个tabpage你在加载自定义控件的时候,先清除所有page,然后自己加载你的mytabpage

TabPage集合编辑器--自己写属性吧
[解决办法]
这是它的设计期行为规定的
Tabcontrol中声明
[ClassInterface(ClassInterfaceType.AutoDispatch), Designer("System.Windows.Forms.Design.TabControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), SRDescription("DescriptionTabControl"), DefaultEvent("SelectedIndexChanged"), DefaultProperty("TabPages"), ComVisible(true)]

而TabControlDesigner.InitializeNewComponent实现
public override void InitializeNewComponent(IDictionary defaultValues)
{
base.InitializeNewComponent(defaultValues);
try
{
this.addingOnInitialize = true;
this.OnAdd(this, EventArgs.Empty);
this.OnAdd(this, EventArgs.Empty);
}
finally
{
this.addingOnInitialize = false;
}
MemberDescriptor member = TypeDescriptor.GetProperties(base.Component)["Controls"];
base.RaiseComponentChanging(member);
base.RaiseComponentChanged(member, null, null);
TabControl component = (TabControl) base.Component;
if (component != null)
{
component.SelectedIndex = 0;
}
}
其中this.OnAdd(this, EventArgs.Empty);就是加入两个 tabpage
再看看OnAdd的实现
private void OnAdd(object sender, EventArgs eevent)
{
TabControl component = (TabControl) base.Component;
IDesignerHost service = (IDesignerHost) this.GetService(typeof(IDesignerHost));
if (service != null)
{
DesignerTransaction transaction = null;
try
{
try
{
transaction = service.CreateTransaction(SR.GetString("TabControlAddTab", new object[] { base.Component.Site.Name }));
}
catch (CheckoutException exception)
{
if (exception != CheckoutException.Canceled)
{
throw exception;
}
return;
}
MemberDescriptor member = TypeDescriptor.GetProperties(component)["Controls"];
TabPage page = (TabPage) service.CreateComponent(typeof(TabPage));
if (!this.addingOnInitialize)
{
base.RaiseComponentChanging(member);
}
page.Padding = new Padding(3);
string str = null;
PropertyDescriptor descriptor2 = TypeDescriptor.GetProperties(page)["Name"];
if ((descriptor2 != null) && (descriptor2.PropertyType == typeof(string)))
{
str = (string) descriptor2.GetValue(page);
}
if (str != null)
{


PropertyDescriptor descriptor3 = TypeDescriptor.GetProperties(page)["Text"];
if (descriptor3 != null)
{
descriptor3.SetValue(page, str);
}
}
PropertyDescriptor descriptor4 = TypeDescriptor.GetProperties(page)["UseVisualStyleBackColor"];
if (((descriptor4 != null) && (descriptor4.PropertyType == typeof(bool))) && (!descriptor4.IsReadOnly && descriptor4.IsBrowsable))
{
descriptor4.SetValue(page, true);
}
component.Controls.Add(page);
component.SelectedIndex = component.TabCount - 1;
if (!this.addingOnInitialize)
{
base.RaiseComponentChanged(member, null, null);
}
}
finally
{
if (transaction != null)
{
transaction.Commit();
}
}
}
}


这就是为什么会默认加两个tabPage的原因
如果你想改变它的设计时行为,可以自定义设计器以供MyTabControl使用
[解决办法]
第一个问题。。如果只需要一个tab。。还有必要用tab吗??????
[解决办法]
1、为什么
你去问微软吧,他的类库的默认就是两个了
2、你都自己说了“我继承TabControl类”
你自己继承别人的东西,别人的标准你要遵照吧
你可以不继承tabcontrol,你继承control,然后全部自己写tabcontrol,你可以把微软的那个用reflector的filegenerate插件反一下c#代码,看看源代码,然后自己写一个,

这种问题解决了就可以8k吗
8k有那么容易挣吗,不觉得不会有公司根据这个问题给你8k的,除非他疯了,或者不在乎钱
[解决办法]
MyTabPage可以继承一下TabPage看看,其实最好是你自己重新写,不要继承tabcontrol,直接继承control然后重新写,就不会收到tabcontrol的限制了
[解决办法]
拿了8k的未必能解决你的问题,能解决你的问题的,不见得就能拿8k
[解决办法]
纯属炒作
[解决办法]
1、如楼上兄弟所说,你要继承就要符合规范,否则你继承它做什么呢??
2、请不要动不动就用这种标题——这是“标题”党行为,在技术论坛很让人讨厌。有事说事就是了
3、你这个问题的难度还8K,熟悉自定义控件的,3K水平也给你解决了,不喜欢自定义控件的,10K也未必可以。

有难度么??没难度。。。


││博客空间:http://blog.csdn.net/lovingkiss
││资源下载:http://download.csdn.net/user/lovingkiss
││Email:loving-kiss@163.com [MSN也是这个]
││联系方式:Q66840199 项目、聊天、咨询、探讨、统统收费,呵呵......
╚---------------------------------------------------------------------ō*︻$▅▆▇◤
[解决办法]
原来是标题党啊
[解决办法]
顶一下
[解决办法]
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;



namespace MyTabControl
{
/// <summary>
/// 自定义MyTabControl继承自TabControl
/// 使用自定义设计器MyTabControlDesigner
/// </summary>
[Designer("MyTabControl.MyTabControlDesigner")]
public class MyTabControl:TabControl
{

}
/// <summary>
/// 自定义MyTabPage继承自TabPage
/// </summary>
public class MyTabPage : TabPage {
public Menu menu { get; set; }
}

/// <summary>
/// 自定义设计器
/// </summary>
public class MyTabControlDesigner : System.Windows.Forms.Design.ScrollableControlDesigner
{


public override void InitializeNewComponent(IDictionary defaultValues)


{
//InitializeNewComponent方法中调用4次OnAdd
base.InitializeNewComponent(defaultValues);
this.OnAdd(this, EventArgs.Empty);
this.OnAdd(this, EventArgs.Empty);
this.OnAdd(this, EventArgs.Empty);
this.OnAdd(this, EventArgs.Empty); 
}

private void OnAdd(object sender, EventArgs eevent) {
//这次我们不再Controls.Add TablePage而是新增MyTabPage
MyTabControl component = (MyTabControl)base.Component;
IDesignerHost service = (IDesignerHost)this.GetService(typeof(IDesignerHost));
//MemberDescriptor member = TypeDescriptor.GetProperties(component)["Controls"];
MyTabPage page = (MyTabPage)service.CreateComponent(typeof(MyTabPage));
component.Controls.Add(page); 
}
}
}

热点排行