【问题标题】:How does Custom User Control send data to Hosting Form自定义用户控件如何将数据发送到托管表单
【发布时间】:2014-10-11 19:44:11
【问题描述】:

我已经使用 c# 实现了一个自定义用户控件。这个控件有一个按钮。 然后将该控件添加到具有列表框的 WinForm Form1 中。

问题是:当我单击用户控件中的按钮时,如何在列表框中添加一些值?

【问题讨论】:

  • 从用户控件引发事件

标签: c# winforms


【解决方案1】:

当您单击按钮时,您必须从用户控件中引发一个事件,然后在您的表单中捕获它1。

你可以这样做:

用户控制

public event EventHandler CLickFromUserControl;

private void click_event_on_the_button()
{
    //Null check makes sure the main page is attached to the event
    if (this.CLickFromUserControl != null)
       this.CLickFromUserControl(new object(), new EventArgs());
}

Form1

public MyApp()
{
     //USERCONTROL = your control with the CLickFromUserControl event
     this.USERCONTROL.CLickFromUserControl += new EventHandler(MyEventHandlerFunction_CLickFromUserControl);
}

public void MyEventHandlerFunction_CLickFromUserControl(object sender, EventArgs e)
{
         //add the value here
}

您可以向事件传递更多参数:

this.CLickFromUserControl(new object(), new EventArgs(), param1, param2);

然后是形式:

public void MyEventHandlerFunction_CLickFromUserControl(object sender, EventArgs e, string param 1, string param2)
{
         //add the value here
}

您可以在用户控件上创建属性:

public string Value {
  get { return textBox1.Text; }
  set { textBox1.Text = value; }
}

然后在表单的事件中从发送者那里访问它。

您可以查看如何构建活动:msdn

【讨论】:

  • 非常感谢 Vinc P。我会试试看 :)
  • Vs 无法识别 Form1 中的“CLickFromUserControl”。就像在 this 下未定义 -> this.USERCONTROL.CLickFromUserControl += new EventHandler(MyEventHandlerFunction_CLickFromUserControl);
  • 你放了吗:公共事件EventHandler CLickFromUserControl;在用户控件中?
  • 100% 有效,非常感谢。小问题请。如果现在我的自定义控件中有几个组件。现在我在 Forms 1 中,我想知道单击了哪个控件,也许还阅读了它的 Text 属性!!我该如何修改它?
  • 您可以将属性传递给事件!我会为你更新答案。
【解决方案2】:

正如@Jonsey 和@Vinc P. 所建议的,您应该使用Event 来完成您的工作。

基本上,工作流程是:

  1. 用户在按钮上触发了点击事件
  2. 自定义用户控件获取事件,对其进行处理,然后将其传递给其父级,即表单;
  3. Form 获取事件并处理它。

要实现工作流,我们可以做到以下几点:

在用户控制中 我们需要为要注册的表单定义委托和事件:

public delegate void ButtonClickEventDelegate(object sender, EventArgs e);
public event ButtonClickEventDelegate ButtonClick;

接下来我们需要注册Button Click Event Handler

// in the custom user control constructor
public CustomUserControl()
{
    InitializeComponent();
    Button.Click += ButtonClickHandler;
}

接下来我们需要在用户控件中定义按钮点击的行为:

private void ButtonClickHandler(object sender, EventArgs e)
{
    // do some handling if you have
    // now the important part, call the delegate function here, it will pass the handle to
    // the behavior defined in the main form:
    if (ButtonClick != null) ButtonClick(this, e);
}

现在我们已经完成了用户控制部分,接下来是关于托管表单。 我们需要做的事情很简单:注册并定义ButtonClick的行为:

// in main form's constructor
public Form1()
{
    InitializeComponent();
    CustomUserControl.ButtonClick += UserControlButtonClickHandler();
}

private void UserControlButtonClickHandler(object sender, EventArgs e)
{
    // add the value here
}

好的,事情完成了:)

【讨论】:

  • 非常感谢 Nevents 。我会试试的:)
猜你喜欢
  • 1970-01-01
  • 2014-08-25
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 2011-11-12
相关资源
最近更新 更多