Sim*_*mon 4 .net c# custom-controls winforms
我想创建一个包含一些控件的TabPage子类,我想通过设计器控制这些控件的布局和属性.但是,如果我在设计器中打开我的子类,我就无法像在UserControl上那样定位它们.我不想创建一个带有UserControl实例的TabPage,我想直接设计TabPage.
我怎么做?我已经尝试更改Designer和DesignerCategory属性,但我没有找到任何有用的值.
我过去也遇到过类似的问题.
我先做的是从继承Usercontrol切换到tabpage,就像这样
class UserInterface:UserControl //设计器位然后将其更改为
class UserInterface:TabPage
第二个我只需将所有控件和内容放在usercontrol中,并将其停靠在一个标签页中.
第三,我做了一个通用类,它接受任何用户控制并自动进行对接.
所以你可以拿你的'UserInterface'类,然后得到一个你可以添加到System.Windows.Forms.TabControl的类型
public class UserTabControl<T> : TabPage
where T : UserControl, new ()
{
private T _userControl;
public T UserControl
{
get{ return _userControl;}
set
{
_userControl = value;
OnUserControlChanged(EventArgs.Empty);
}
}
public event EventHandler UserControlChanged;
protected virtual void OnUserControlChanged(EventArgs e)
{
//add user control docked to tabpage
this.Controls.Clear();
UserControl.Dock = DockStyle.Fill;
this.Controls.Add(UserControl);
if (UserControlChanged != null)
{
UserControlChanged(this, e);
}
}
public UserTabControl() : this("UserTabControl")
{
}
public UserTabControl(string text)
: this( new T(),text )
{
}
public UserTabControl(T userControl)
: this(userControl, userControl.Name)
{
}
public UserTabControl(T userControl, string tabtext)
: base(tabtext)
{
InitializeComponent();
UserControl = userControl;
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// UserTabControl
//
this.BackColor = System.Drawing.Color.Transparent;
this.Padding = new System.Windows.Forms.Padding(3);
this.UseVisualStyleBackColor = true;
this.ResumeLayout(false);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7407 次 |
| 最近记录: |