C#UserControl,多个事件/代理 [英] C# UserControl, multiple events/delegates

查看:143
本文介绍了C#UserControl,多个事件/代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是"VB Spoiled",并尝试将应用程序转换为C#。 VB破坏了我,因为我可以从另一种形式处理每个表单,知道控件和内容是什么,以及它必须提供的所有其他伟大的东西。 C#一直是这个"单一应用程序"的挑战。现在变成了一个基于DLL的多项目程序,并且在事物之间进行通信正在让我的思想想要在另一个方向上漫游。

从我在一个项目中的主窗体,我使用菜单项打开另一种形式,由两个位置的单选按钮组框和一个FlowLayoutPanel组件组成。在同一个项目中,我的用户控件当然可以在工具栏中选择。当表单加载时,它可以访问包含缩写,描述和文件名列表的字典变量。

代码将运行"foreach(KeyValuePair ....)"。创建用户控件的新实例(uscEmoteControl)的例程,将控件名称指定为"uscEmote"。 + X.ToString()" (其中X是计数器变量),将控件上的文本和描述字段设置为字典对象中的值,并将背景图像设置为文件名(是的,表情符号),然后使用以下命令将控件添加到流面板:

FlowLayoutPanel1.Controls.Add(eControl);

我创建了一个委托,我确实创建了一个在调用委托时运行的事件。我想要做的是传回主窗体上的文本框,双击单个用户控件时用户控件的缩写值。问题是这些自定义控件中有大约175个被放置在流程面板中,并且随着更多的表情被创建,该值可能随时(在路上)发生变化。从我的理解到关于代表和事件处理程序的这一点,我看不到......甚至如何将......处理程序的175个实例放到主窗体上。

很明显,我是处理代表和活动时非常环保。我毕竟是VB Spolied。 (叹气)

显示表情的表格DOES可以访问主表单变量,因为我在加载表情形式时传入了对主表单的引用。然而,用户控制中的代码根本没有关于主要形式的线索(不同于知道所有内容的VB方面),也没有关于"显示表情"的其他字段的任何线索。通过代码添加控件的形式,所以我编写用户控件以便直接访问当前表单上的主窗体或字段/控件的旧方法是吐司。

如何解决问题这个困境?我确实已经阅读并从 http://www.akadia.com/services/dotnet_user_controls.html但它并没有真正给我我需要的东西。我曾经想过,也许我可以在显示器表格上添加另一个隐藏文本框,当用户双击流程面板上的一个自定义控件时,他们点击的值将被传递到隐藏文本框,导致事件发生主要形式可能会响应... ...但这看起来很复杂,当它在VB中很容易。

任何帮助我理解这一点都将深表感谢。在此先感谢!

加里

I am "VB Spoiled", and trying to convert an application into C#.  VB spoiled me because I could address each form from another form, know what the controls and contents were, and all of the other great things it had to offer.  C# has been quite the challange as this "single app" is now turning into a multi-project DLL based program and getting the communications between things is making my mind want to wander off in another direction.

From my main form in one project, I use a menu item to open another form which consists of a two position radio button group box, and a FlowLayoutPanel component.  Within the same project, is my user control which is of course, available in the tool box to select.   When the form loads, it has access to a dictionary variable that contains a list of abbreviations, descriptions, and file names.

The code will run "foreach (KeyValuePair....)" routine that creates a new instance of the user control (uscEmoteControl), assign the control name as being "uscEmote" + X.ToString()"  (where X is a counter variable), set the text and description fields on the control to the values from the dictionary object, and set the background image to the filename (yes, emoticons) amd then add the control to the flow panel using:

FlowLayoutPanel1.Controls.Add(eControl);

I did create a delegate, and I did create an event to run when the delegate is called.  What I want to do is to pass back to a text box on the main form, the abbreviation value from the user control when the individual user control is double clicked.  The problem is there are about 175 of these custom controls placed into the flow panel, and that value could change at any time (down the road) as more emotes are created.  From what I understand to this point about delegates and event handlers, I can't see putting... or even how to put... 175 instances of the handlers onto the main form.

Clearly, I am VERY green when it comes to dealing with delegates and events.  I am after all, VB Spolied.  (sigh)

The form to display the emotes DOES have access to the main form variables because I passed in a reference to the main form during the loading of the emotes form.  The code in the user control however, has NO clue about the main form at all (unlike the VB side which knew everything) nor does it really have any clue about the other fields that are on the "display emotes" form that the controls were added to by code, so my old way of coding the user control to directly access the main form or the fields/controls on the current form, is toast.

How do I go about solving this dilema?  I did read over and learnt a fair amount from http://www.akadia.com/services/dotnet_user_controls.html but it didn't really give me what I needed.  I had thought that maybe I could add another hidden text box on the display emotes form, and when the user doubleclicked one of the custom controls on the flow panel that the value they clicked on would be passed into hidden textbox, causing an event to be tripped which the main form could respond to...  but this seems SO complex, when it was SO easy in VB.

Any assistance in getting me to understand this will be deeply appreciated.  Thanks in advance!

Gary

推荐答案

是的,您想要使用一个事件。不是175个,只有一个。在ComboBox之后对其进行建模,它可以在下拉列表中显示任意数量的项目。并且只需要一个事件SelectedIndexChanged来告诉表单选择已更改。
Yes, you want to use an event.  Not 175 of them, just one.  Model it after ComboBox, it can display an arbitrary number of items in the dropdown list.  And only needs one event, SelectedIndexChanged, to tell the form that the selection changed.


这篇关于C#UserControl,多个事件/代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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