动态调用表单上的用户控件 [英] Calling a user control on form dynamically

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

问题描述

嗨专家,



我需要动态调用表单上的用户控件。表单包含一个列表网格,其中包含从数据库中提取的数据。单击特定行时,应将详细信息填充到用户控件中,并应在表单上调用。用户控件包含其他控件,如文本框,标签,组合框等。



提前致谢,

Ankit

Hi experts,

I need to call a user control on to a form dynamically. The form contains a list grid with data which is being fetched from database. On click of a particular row the details should be populated into user control and it should be called on form. The user control contains other controls like text boxes, labels, combo box etc.

Thanks in advance,
Ankit

推荐答案

没有调用控件之类的东西。控件不是方法,函数,过程,子例程,属性或运算符。

此外,没有添加控件的动态或静态方式。它总是动态的。当你使用设计师时,它只是编写用于在表格中添加控件的代码。



所以,秘密武器很简单:如果你不知道如何在代码中执行某些操作,尝试在设计器中执行此操作,只需编写代码示例。完成后,查看自动生成的代码(在Forms中,它将作为窗体节点的子节点放在Visual Studio的解决方案资源管理器中),并学习如何操作。



例如:
There is no such thing as "call a control". A control is not a method, function, procedure, subroutine, property or operator.
Also, there are not "dynamic" or "static" ways of adding a control. It''s always "dynamic". When you use a designer, it simply writes the code for adding controls in forms.

So, the "secret weapon" is simple: if you don''t know how to do something in code, try to do it in designer, just to make a code sample. When it is done, look at the auto-generated code (in Forms, it is placed as a child node of the form node in the Solution Explorer of Visual Studio), and learn how to do it.

For example:
Panel parentPanel = new Panel();

//... let's assume the panel is already added

TextBox myTextBox = new TextBox();
Button myButton = new Button();
// take care about layout, name, etc.

// now, events:
myButton.Click += (sender, eventArgs) => { DoSomethingOnButtonClick(); };
// equivalent way good for old C# v.2:
myButton.Click += delegate(object sender, System.EventArgs eventArgs) { DoSomethingElseOnButtonClick(); };

// add them:
myButton.Parent = parentPanel;
// another equivalent way to add
parentPanel.Controls.Add(myTextBox);

// the panel itself was added in the same way;
// all the way to the Form, which is also a Control...









现在,操作顺序:



您需要以相对于TAB导航顺序的相反顺序插入控件。您还可以调整 TabOrder 值。



如果表格已经显示在屏幕上,您仍然可以添加控件,但显然,最后的操作应该是 Controls.Add ,以避免可能的闪烁。如果在添加控件时没有显示表单,则此规则并不重要,但是,对于通用方法和更好的维护,我建议在添加布局时在最后步骤中添加控件。



防止布局闪烁的常规方法是将代码夹在顶级父控件的 SuspendLayout / ResumeLayout 中,通常为表格

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms .control.resumelayout.aspx [ ^ ]。



[END EDIT]



再次,如果您不知道其他事情是如何完成的,请与设计师一起尝试,看看它是如何在自动生成的代码中完成的。但是不要复制编码风格,不要使用自动生成的名称,它们违反了Microsoft命名约定(因为没有办法在代码生成中观察它们而不知道你的语义),总是使用语义敏感的名称。要添加事件处理程序,最好使用匿名方法。



-SA





Now, the order of operation:

You need to insert controls in reverse order relative to the order of TAB navigation. You can also adjust TabOrder values.

If the form is already shown on screen, you still can add controls, but apparently, the last operations should be Controls.Add, to avoid possible flicker. If the form is not shown at the moment of adding control, this rule is not important, but, for the universal approach and better maintenance, I would advice to add controls in last steps, when layout is added.

The regular way to prevent layout flicker is sandwiching code in SuspendLayout/ResumeLayout of the top-level parent control, usually Form:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.resumelayout.aspx[^].

[END EDIT]

Again, if you don''t know how something else is done, try it with the designer and see how it is done in auto-generated code. But don''t copy coding style, end never use auto-generated names, they violate Microsoft naming conventions (because there is no way to observe them in code generation which does not "know" your semantic), always use semantically sensitive names. For adding event handlers, better use anonymous methods.

—SA


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

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