在用户控件之间传递数据

编程入门 行业动态 更新时间:2024-10-28 10:31:45
本文介绍了在用户控件之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在winForm中的两个用户控件之间传递数据? 例如,如果我有两个名为ucA和ucB的用户控件. ucA包含一个文本框和一个按钮. ucB包含一个标签,单击该按钮时,文本框中的文本应显示在ucB的标签中. 在此先感谢C#newbee. :)

How do I pass data between two usercontrols in a winForm? For example if I have two usercontrols named ucA and ucB. ucA contains one textbox and one button. ucB contains a label, and when clicking the button the text in the textbox should appear in the label in ucB. Thanks in advance from a C# newbee. :)

推荐答案

ucA会引发其主机控件(Form?)所预订的事件. ucB应该具有由设置员设置Label文本的公共属性. 实例化ucB时,宿主控件应维护对其的私有引用,当它处理ucA中引发的事件时,可以设置ucB的属性. 看看我的简化的事件 [ ^ ]文章. 该死的,今天早上我心情很好-这是一个完整的(WinForms)示例. ucA should raise an event that it''s host control (Form?) subscribes to. ucB should have a public property who''s setter sets the Label''s Text. The host control when instanciating ucB should maintain a private reference to it, when it handles the event raised in ucA it can then set the property of ucB. Have a look at my Events Made Simple[^] article. What the heck, I''m in a good mood this morning - here''s a full working (WinForms) example. using System.Drawing; using System.Windows.Forms; namespace UserControlInteraction { public partial class Form1 : Form { private UCA ucA; private UCB ucB; public Form1() { InitializeComponent(); ucA = new UCA(); ucA.Size = new Size(150, 50); ucB = new UCB(); ucB.Location = new Point(0, 50); Controls.AddRange(new Control[] { ucA, ucB }); ucA.UpdateText += new EventHandler<TextEventArgs>(ucA_UpdateText); } void ucA_UpdateText(object sender, TextEventArgs e) { ucB.LabelText = e.Text; } } public class UCA : UserControl { public event EventHandler<TextEventArgs> UpdateText; private TextBox textBox; private Button button; public UCA() { textBox = new TextBox(); textBox.Location = new Point(0, 0); button = new Button(); button.Location = new Point(0, 24); button.Text = "&Update"; button.Click += new EventHandler(button_Click); Controls.AddRange(new Control[] { textBox, button }); } private void button_Click(object sender, EventArgs e) { OnUpdateText(new TextEventArgs(textBox.Text)); } protected virtual void OnUpdateText(TextEventArgs e) { EventHandler<TextEventArgs> eh = UpdateText; if (eh != null) eh(this, e); } } public class UCB : UserControl { private Label label; public UCB() { label = new Label(); label.Size = new Size(100, 13); Controls.Add(label); } public string LabelText { get { return label.Text; } set { label.Text = value; } } } public class TextEventArgs : EventArgs { private string text; public TextEventArgs(string text) { this.text = text; } public string Text { get { return text; } } } }

这与我尝试过的类似,但事件在用户控件代码的后面. 我将测试您的解决方案并稍后再返回,但是我认为这可以解决我的问题. 非常感谢您的代码,当我查看您的代码时,更容易理解所有技术术语. :thumbsup: 编辑. 效果很好,而且由于您的代码,我什至认为我知道它为什么起作用. :-D That is similar to what I have tried, but I had the event in the usercontrols codebehind. I will test your solution and get back later, but I think that this will solve my problem. And big thanks for the code, much easier to understand all technical terms when I can look at your code. :thumbsup: Edit. Works great, and thanks to your code I even think that I understand why it works. :-D

更多推荐

在用户控件之间传递数据

本文发布于:2023-11-06 15:58:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   数据   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!