从UserControl将控件添加到表单

从UserControl将控件添加到表单

问题描述:

:rose:

我有一个用户控件并将其添加到我的工具箱中.当您将此控件添加到窗体中时,用户控件会显示一个带有1个按钮的窗体.
我要单击该按钮,然后在主窗体(Form1)上添加一个文本框.
那时候如何从我的用户控件中获取表单?
这是设计时间!
谢谢.
:rose:

:rose:
Hi.
I have a User Control and i added it to my Tool Box.When you add this control to your form, user control shows a form with 1 button.
I want to click the button and one Text Box be added on the Main Form(Form1).
How can get the form in that time(from my User Control)??
This is Design time!!
Thank you.
:rose:

考虑到用户控件位于表单上,执行此操作应该非常简单.用户控件应在其内部的按钮被单击时公开一个事件.您可能可以将其称为TextBoxAddRequest 之类的(因为它正在执行此操作).在用户控件内,处理按钮的Click 事件并触发此自定义TextBoxAddRequest 事件.父表单将处理此事件,并在处理程序中创建一个新的TextBox 并将其添加到其控件集合中.
Considering that the user-control is on your form, it should be fairly straightforward to do this. The user-control should expose an event for when the button inside it gets clicked on. You can probably call it something like TextBoxAddRequest (since that''s what it is doing). Inside the user-control, handle the button''s Click event and fire this custom TextBoxAddRequest event. The parent form will handle this event and in the handler create a new TextBox and add it to its control collection.