c# - 将数据从 UserControl 传递到另一个

标签 c# winforms

我有一个表单和两个自定义 UserControl我自己做的。一个控件有一些按钮,每个按钮都有自己的按钮 Tag属性设置为 PointF 的数组.我有另一个 UserControl,它有一个 ObservableCollection<PointF[]>我将其事件处理程序设置为在向其添加数据时绘制线条。这工作正常如果我把数据点放在它自己的类上......只要确保它工作。

不,我的问题是,将这两个控件放在一个窗体中,如何设置第一个控件中按钮的单击事件,以将数据点添加到第二个控件?

这两个控件在我的解决方案中都在两个不同的项目中。以及这些控件的显示形式,也在不同的项目中(它是解决方案的启动项目)

最佳答案

向第一个控件添加一个事件。

public event EventHandler<EventArgs> Control1ButtonClicked;
private void OnClicked()
{
    var handler = Control1ButtonClicked;
    if (handler != null)
    {
        handler(this, new EventArgs());
    }
}

private void Button1_Click(object sender, EventArgs e)
{
    OnClicked();     
}

给第二个控件添加一个属性

public ObservableCollection<PointF[]> MyPoints{ get; set;};

然后在你的主应用中添加一个监听器

userControl1.Control1ButtonClicked += new EventHandler<EventArgs>(userControl1_Control1ButtonClicked);

void userControl1_Control1ButtonClicked()
{
    //Do Something to control 2
    userControl2.MyPoints.Add() = //Whatever
}

关于c# - 将数据从 UserControl 传递到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11530264/

相关文章:

c# - 如何在不同的 CPU 内核上生成线程?

c# - 将项目添加到 PopupMenu

c# - 将标签添加到 WinForm 中的文本框自定义用户控件

c# - 在二十一点中计算手牌值

vb.net - 格式化文本框以始终显示 $ 符号

c# - 如何在 Winforms 项目中绘制笛卡尔点?

c# - 热仅在 datagridview 中更改特定列标题颜色?

c# - 改进枚举到组合框的绑定(bind)(使用 MarkupExtension)

c# - 如何将 Excel 文件读入矩阵?

c# - 如何允许我的应用程序像 Visual Studio 一样具有拖放工具箱?