用户操作后从用户控件返回值 [英] Return value from usercontrol after user action

查看:68
本文介绍了用户操作后从用户控件返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我需要与Form.ShowDialog()提供的东西相同,但是需要一个UserControl.

Essentially I need the same thing that Form.ShowDialog() offers, but with a UserControl.

在winform中,我加载了一个UserControl,该控件应允许用户从列表中选择一个项目,然后将其返回给调用方.

Inside a winform, I load a UserControl, which should allow a user to select an item from a list, and return it back to the caller.

例如:

var item = myUserControl.SelectItem();

显然,从控件的方法返回非常简单.但是如何让它等到用户使用控件执行所需的操作?

Obviously, returning from a control's method is very simple. But how can I make it wait until user performs the required action with the control?

我可以订阅控件的事件,但是此路径并不理想.

I can subscribe to an event of the control, but this path is not ideal.

简单地说,我希望在用户单击特定控件按钮后返回UserControl的方法.

Put simply, I want a UserControl's method to return after user clicks a specific button on it.

推荐答案

简而言之,UserControl实际上只是一个自定义控件,就像您放下 TextBox ListBox 在WinFrom上,将UserControl放到窗体上.

Simply put, a UserControl is really just a custom control and just like you drop a TextBox or a ListBox on your WinFrom, you drop your UserControl on the form.

对待您的UserControl就像对待其他任何控件一样,例如 TextBox ListBox .

Treat your UserControl just like you would treat any other control, like TextBox or ListBox.

因此,就像您通过 TextBox.Text SelectedValue SelectedItem TextBox 中获取值一样>从 ListBox 中,您可以从UserControl中调用一个方法以返回SelectedItem.

So, just like you get the value from a TextBox through TextBox.Text or the SelectedValue or SelectedItem from a ListBox, you would call a method from your UserControl to return the SelectedItem.

通常在单击确定"按钮或关闭表单时,是在代码中通过每个表单控件获取其值的时候.大概,您将进行一些验证以确保也输入了正确的值.

Often times when the OK button is clicked or the form is closed is when in your code you would go through each of your form's controls getting their values. Presumably, you would do some validation to make sure proper values were entered, too.

因此,当您的表单被接受时,您将调用UserControl的方法来获取所选项目.您无需订阅事件即可等待事件发生.再次,就像对待普通的 ListBox 一样对待它.

Therefore, when your form is accepted is when you would call your UserControl's method to get the selected item. You don't have to subscribe to an event to wait for that to happen. Again, just treat it like you would treat a normal ListBox.

现在更多地了解您问题的性质,这就是我的答案:

Knowing now more about the nature of your question this is my answer:

假设您有一个如下所示的UserControl:

Say you have a UserControl that looks like this:

在后面的代码中,您将必须设置一个事件来监视何时在UserControl中单击确定"按钮.此事件还将通知订阅者该用户在您的列表中选择的选择:

In the code behind you are going to have to set up an Event to monitor when the the OK button has been clicked inside the UserControl. This event will also notify a subscriber what the choice was that the user selected in your list:

public partial class SelectFromListUserControl : UserControl
{
    public class SelectedItemEventArgs : EventArgs
    {
        public string SelectedChoice { get; set; }
    }

    public event EventHandler<SelectedItemEventArgs> ItemHasBeenSelected;

    public SelectFromListUserControl()
    {
        InitializeComponent();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        var handler = ItemHasBeenSelected;
        if (handler != null)
        {
            handler(this, new SelectedItemEventArgs 
                { SelectedChoice = listBox1.SelectedItem.ToString() });
        }
    }
}

在主窗体上,您将具有与以下代码相似的模式代码.

On your main form you will have code patterned similar to the following.

  1. 应该有一个例程可以创建或显示此特殊用户控件.
  2. 它将钩住用户控件中的事件,以便通知主表单.
  3. 它将绘制用户控件.
  4. 事件处理程序将检索在用户控件中选择的值,然后清除该用户控件和/或调出另一个用户控件.

  1. There should be a routine to create or make visible this special user control.
  2. It will hook the event in the user control so that the main form will be notified.
  3. It will draw the user control.
  4. The event handler will retrieve the value selected in the user control and then clear the user control and/or bring up another user control.

private void ShowSelectFromListWidget()
{
    var uc = new SelectFromListUserControl();
    uc.ItemHasBeenSelected += uc_ItemHasBeenSelected;

    MakeUserControlPrimaryWindow(uc);
}

void uc_ItemHasBeenSelected(object sender, 
                     SelectFromListUserControl.SelectedItemEventArgs e)
{
    var value = e.SelectedChoice;

    ClosePrimaryUserControl();
}

private void MakeUserControlPrimaryWindow(UserControl uc)
{
    // my example just puts in in a panel, but for you
    // put your special code here to handle your user control management 
    panel1.Controls.Add(uc);
}

private void ClosePrimaryUserControl()
{
    // put your special code here to handle your user control management 
    panel1.Controls.Clear();
}

这篇关于用户操作后从用户控件返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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