如何重用表格? [英] How to reuse a form?

查看:67
本文介绍了如何重用表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种类似于旧表格的表格.如何使用较旧的表单创建新表单以与数据库的不同实体进行交互(如果创建了新表单,则应使用两个以上的控件)?

正如我们继承类一样,我们也可以继承Windows窗体吗?

如果是,那么该怎么办?如何?与其他任何类完全相同.

假设您从EntityForm开始,然后要创建表单ExtendedEntityForm,该表单向EntityForm添加了两个控件.为此,您只需执行以下操作:


 // 这是您的基本表单
公共  EntityForm:表格
{

}

// 这是您的扩展形式
公共  ExtendedEntityForm:EntityForm
{
   
} 


好吧,如果您不想创建派生表单,而是希望在运行时扩展现有表单,则可以配置网格(或其他控件) ),然后将其添加到表单的Controls集合中.

 // 这是您现有表单的一个实例(带有两个文本框)
EntityForm form =  EntityForm();

// 创建新的网格(或其他控件)
System.Windows.Forms.DataGrid grid =  DataGrid();

// 配置控件以适合您的需求
grid.Visible =  true ;
grid.Location = 点( 100  100 );
//  ... 

// 您可能要在此处更改表单大小以容纳新控件
//  ... 

// 将网格添加到表单实例的Controls集合中
form.Controls.Add(grid);

// 显示对话框(您将在表格中看到网格).
form.ShowDialog(); 


这就是您的想法吗?


I need a form similar to an older one. How to use the older form to create a new form to interact with different entity of the database (the new form if created, shall be using two more controls) ?

Just as we inherit classes, can we inherit windows forms too?

If yes, then how to?

解决方案

Yes, you can derive a form from another form (as long as it is not marked as sealed). How? Exactly the same way as you do with any other class.

Suppose you start with EntityForm and then you want to create form ExtendedEntityForm which adds, say, a couple of controls to the EntityForm. To do this, you just have to do the following:


// This is your base form
public class EntityForm: Form
{

}

// This is your extended form
public class ExtendedEntityForm: EntityForm
{
   
}


Well, if you don''t want to create a derived form and prefer to extend the existing form at runtime, you can configure your grid(or some other control) and then add it to the Controls collection of your form.

// This is an instance of your existing form (with your two text boxes)
EntityForm form = new EntityForm();

// Create a new grid (or some other control)
System.Windows.Forms.DataGrid grid = new DataGrid ();

// Configure the control to fit your needs
grid.Visible = true;
grid.Location = new Point(100, 100);
// ...

// You might want to change form size here to accommodate the new control 
// ...

// Add the grid to the Controls collection of the form instance
form.Controls.Add(grid);

// Show the dialog  (you'll see your grid in the form).
form.ShowDialog();


Is this what you had in mind?


这篇关于如何重用表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆