动态生成的表单 [英] Dynamically Generated Forms

查看:65
本文介绍了动态生成的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个应用程序我正在努力,遇到了一个我无法找到答案的问题。



我创建了一个从SQL Server表动态创建表单的子例程。



在BindingNavigator的Save按钮中,我有以下代码:

Hi Everyone,
I have an application I''m working on and have come across a problem for which I can''t find an answer.

I have created a subroutine that dynamically creates a form from a SQL Server table.

In the Save button on the BindingNavigator I have the following code:

Public Sub ImageButton_Click(ByVal sender As System.Object, _
                             ByVal e As System.EventArgs)
    For Each frm In My.Application.OpenForms
        MsgBox("Form.Tag: " & frm.Tag & " Form Name:" & frm.Name)
    Next
End Sub



代码通过AddHandler方法激活。所有这些代码都是在我的应用程序中显示所有打开的表单。



当您在我动态生成的任何表单上按保存按钮时,您将看到一个消息框,显示Form.Tag和Form.Name属性按照从我的LogIn表单开始生成的顺序。



LogIn表单是使用GUI设计器创建的,Name属性是设置为frmLogIn。根据我在互联网上找到的内容,你不能在dymanically创建的表单上使用Name属性;您必须使用Tag属性。所以我使用他们的表名标记了客户表格,类别表格和员工表格。



这让我想到了我遇到的问题:我怎么能单击Employee动态生成表单上BindingNavigator控件上的Save按钮,它只显示Employees Dynamic Form上包含Form.Tag和Form.Name属性的消息框?



我对生成的表单和从ImageButton_Click事件代码返回的消息框进行了屏幕截图,但是目前没有办法将它们合并到这个问题中。



我感谢您提供的任何帮助,

MRM256


The code is activated though the AddHandler method. All this code does is show all open forms in my application.

When you press the Save button on ANY of my dynamically generated forms you will get a message box showing the Form.Tag and Form.Name properties in the order they were generated starting with my LogIn form.

The LogIn form was created using the GUI designer and the Name property was set to frmLogIn. According to what I found on the Internet you cannot use the Name property on dymanically created forms; you must use the Tag property. So I tagged the Customers Form, Categories Form, and the Employees Form using their table names.

Which brings me to the problem I''m having: How can I click on the Save button on the BindingNavigator control on the Employee dynamically generated form and it show me only the message box containing the Form.Tag and Form.Name properties on the Employees Dynamic Form?

I took screen captures of the generated forms and the Message Boxes returned from the ImageButton_Click event code, but currently there is no way to incorporate them into this question.

I thank you for any assistance you can offer,
MRM256

推荐答案

首先:设计师使用Name属性并且对于在设计师之外生成的控件没有意义。如果有人告诉不要使用 Name ,这只是意味着你不能依赖这些值。但是你知道设计师没有触及那些属性并自己为它们赋值,为什么不呢? 标签会更好。



但是我怀疑你是否需要这些属性。您不应该尝试通过名称识别任何内容,因为这是不可支持的。您已经创建了每个表单的引用,并在创建表单时获得对它的引用。使用引用的表单:它是最好的性能和最简单的。



另一个建议是:不要在一个应用程序中创建多个表单,这对可用性不利。相反,只有一个主要形式(我不算数模态形式在这里;它们可以用于某些适当的目的)。如何处理现在作为单独表格的内容?然后制作不同的面板或标签页。你可以将它们全部放在一个表格中然后隐藏/显示,然后用手风琴式设计;在 TabControl 中使用它们;毕竟,你甚至可以制作一个可停靠的设计,就像Visual Studio一样。



-SA
To start with: Name property is used by the designer and makes no sense for controls generated beyond the designer. If someone tells "don''t use Name", it simply means that you cannot rely on those values. But it you know that those properties are not touched by the designer and assign values to them by yourself, why not? it would be even better that Tag.

But I doubt you ever need those properties. You should not try to identify anything by name, as this is not supportable. You already have a reference to each form you create and get a reference to it when you create one. Use the referenced to the form: it''s the best for performance and the easiest.

Another advice is: don''t create multiple forms in one application, this is not good for usability. Instead, have only one main form (I don''t count few modal forms here; they would be fine for some adequate purposes). What to do with what you have now as separate forms? Make then different panels, or tab pages. You can put them all in one form and hide/show then, use then in accordion-like designs; use them in a TabControl; after all, you can even make a dockable design, pretty much as the one of Visual Studio.

—SA


private void button1_Click(object sender, EventArgs e)
{
    Form form = new Form();
    form.Location = new Point(350, 350);
    form.Width = 200;
    form.Height = 200;
    form.SuspendLayout();
    int usedHeight = 0;
    Label label = new Label();
    label.Text ="lable1";
    label.Top = usedHeight + 7; label.Left = 5;
    form.Controls.Add(label);
    TextBox textBox = new TextBox();
    textBox.Text =" ";
    textBox.Top = usedHeight + 5; textBox.Left = 80;
    textBox.Width = 115;
    textBox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
    form.Controls.Add(textBox);
    usedHeight += textBox.Height + 5;
    form.ShowDialog();
}


这篇关于动态生成的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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