动态创建Windows表单 [英] Create windows forms dynamically
问题描述
亲爱的,
我正在使用c Sharp来创建一个小型应用程序,以便能够动态创建Windows窗体...我真的陷在该应用程序中了...有人可以帮帮我吗我们如何创建c动态地生成尖锐的表格,并在该表格上添加控件....
请帮帮我,我将非常感谢您的这种举动...
问候,
哈桑·塔里克
Hi dear,
I am using c sharp to create a small application by using which so that i am able to create windows forms dynamically...I am really stuck in that application...Can somebody do help me out that how we can create c sharp forms dynamically and also add controls on that forms....
Please do help me out i will be very thankful for your this kind act...
Regards,
Hassan Tariq
Thanks alot for your help and time... but dear actually i have a wizard that takes all the requirements from the user that how much buttons he want, how much text-boxes he wants etc...and when user completes that wizard the end result is that my project create the windows form for my user...but <b>the problem is that when user again run the same project executable file it again runs the wizard but instead that i just want that when user again runs the project it shows its own created form instead of my wizard</b>....
Thanks for your support.....
Waiting for a suitable reply......
Best Regards,
Hassan Tariq
推荐答案
以下是创建键/值对编辑器的简单示例:
Here''s a simple example for creating key/value pair editors:
Form form = new Form();
form.Location = new Point(x, y);
form.Width = 200;
form.SuspendLayout();
int usedHeight = 0;
foreach(KeyValuePair<string,> kvp in someDictionary){
Label label = new Label();
label.Text = kvp.Key;
label.Top = usedHeight + 7; label.Left = 5;
form.Controls.Add(label);
TextBox textBox = new TextBox();
textBox.Text = kvp.Value;
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.ResumeLayout();
form.Height = usedHeight + 10;
您可能想要做的是动态填充一个面板,并在窗体的边缘放置其他面板,并设置其Dock属性.这样,您可以使用设计器制作确定"/取消"按钮栏,菜单,工具栏等,并且只在中间有一个面板,其中包含动态内容(并适当设置表单的大小).动态创建整个表单可能没什么用.
我用类似的方法打开了一个选择对话框,在设计对话框中,要检查的选项数量是未知的(这是要操作的数据集的列表,可由用户添加).在那种情况下,我有一个动态调整大小的GroupBox,面板中的其他几个控件停靠在设计器中的顶部或底部.
What you might want to do is dynamically fill a panel, and have other panels around the edges of your form with their Dock property set. That way you can use the designer to make an OK/Cancel button bar, menus, toolbar etc, and just have a panel in the middle with dynamic content (and set the size of the form appropriately). Dynamically creating entire forms is probably less useful.
I used something similar to this to bring up a selection dialog where the number of options to check wasn''t known at design time (it was a list of data sets to operate on, which can be added by the user). In that case I had a GroupBox which was dynamically sized and several other controls in a panel docked to the top or the bottom made in the designer.
请尝试以下Codeproject文章:
创建动态表单并使其内容合适 [
Try this Codeproject article:
Creating Dynamic form and making the content fit[^]
If that isn''t applicable, try learning how to use google. When I googled with your question as the search phrase, I got back 3.9 MILLION results.
您还可以在设计器中创建表单,然后查看设计器的生成代码.
例如,如果将没有任何控件的窗体的代码与带有TextBox
的窗体的代码进行比较,则从本质上您将知道如何将该控件添加到窗体中.
动态生成的表单的主要区别在于,您将必须提供一种访问控件和挂钩事件的方法(最终取决于应用程序的代码将它们取消挂钩).
You can also create a form in the designer and then look at the generate code by the designer.
For example, if you compare the code of a form without any control with the code of a form with oneTextBox
, you would essentially know how to add that control to your form.
The main difference with dynamically generated forms is that you would have to provide a way to access controls and hook events (and eventually unhook them depending how your code is done).
这篇关于动态创建Windows表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!