Tim*_*ond 5 c# user-controls winforms
我正在构建一个小的选项卡式c#表单,我希望每个标签页都有一些共同的功能,特别是一个OK按钮和一个错误消息,并为特定的表单字段留出空间.
有没有其他人做过类似的事情,你是如何做到的?
如果不扩展TabControl/TabPage,这很容易做到.
定义一个UserControl,并在每个TabPage上放置所需的公共元素.
在窗体上:继续为每个TabPage设计所需的TabPage特定控件:确保在添加UserControl后它们不会与公共控件在视觉上重叠.
在主表单的表单加载事件中执行以下操作:
// form scoped variable to hold a referece to the current UserControl
private UserControl1 currentUserControl;
private void Form1_Load(object sender, EventArgs e)
{
foreach(TabPage theTabPage in tabControl1.TabPages)
{
currentUserControl = new UserControl1();
theTabPage.Margin = new Padding(0);
theTabPage.Padding = new Padding(0);
theTabPage.Controls.Add(currentUserControl);
currentUserControl.Location = new Point(0,0);
currentUserControl.Dock = DockStyle.Fill;
currentUserControl.SendToBack();
}
}
Run Code Online (Sandbox Code Playgroud)
尽管这里并不真正需要"SendToBack",但是使用"Okay"按钮和TextBox获取错误消息的UserControl被放置在您分配给每个TabPage的各个控件后面是"保险".
| 归档时间: |
|
| 查看次数: |
6374 次 |
| 最近记录: |