活动新秀;按 F5 后引发操作
所以,是的,我对创建自己的自定义事件非常陌生。当我将控件放在表单上时,我可以完成基本操作,但这个有点复杂。我有一个应用程序,它读取 .TSV 并根据它“读取”的对象数量使用控件填充表单。举例来说:我有一个包含 10 个人员对象的文件,我的代码使用每个人的控件填充一个表单。简单的东西!
现在假设我有一个包含以下项目的组合框:“活着”、“已故”、“未出生”。旁边有一个年龄文本框。现在,该文本框最初未启用,因为 ComboBox 的默认值为“Unborn”。但是假设当用户选择“Alive”时,我希望该文本框启用,以便可以输入年龄。
显然,从我问这个问题和这个问题的标题来看,我不知道如何去做。我不太了解事件,我通过示例学习,但 MSDN 示例并不能完全解决问题。
任何帮助(尤其是很棒的分步指南)将不胜感激。
So yes I'm very new to creating my own custom events. I can do the basics when I put controls on a form but this one is a little more complex. I have an application that reads in a .TSV and populates a form with controls based on the number of objects it "reads." So for instance: I have a file that contains 10 people objects and my code populates a form with controls for each person. Easy stuff!
Now lets say I have a ComboBox with the items: "Alive", "Deceased", "Unborn". Right next to this I have a textbox for age. Now originally, this textbox is not enabled because the default value for the ComboBox is "Unborn". But lets say when the user selects "Alive", I want that textbox to become enabled so an age can be entered.
Obviously from me asking this and the title of this question, I don't know how to go about doing this. I don't really understand events and I learn by example but the MSDN examples don't quite cut it.
Any help (especially an awesome Step-by-Step guide) would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据我从评论中收集到的信息,您希望将事件添加到在运行时创建的表单对象。对对象使用 AddHandler 命令。这样做的效果是:
这样做,您将能够修改在运行时创建的对象的事件。就您而言,听起来您会想要使用约瑟夫推荐的操作,并最终合并提供的两种解决方案,如下所示
From what I gather from the comments, you want to add events to a form object that is created at runtime. Use the AddHandler command to the object. Something to the effect of:
Doing it this way, you will be able to modify the events of an object created at runtime. In your case, it sounds like you'll want to use the action that Josaph recommended, and end up incorporating both solutions offered, like so
您将需要使用 ComboBox_SelectedIndexChanged() 事件来捕获组合框项目已更改。此时,您需要检查已选择哪个组合框项目,并决定是否应启用文本框。这是一个例子。注意:此示例假设“Alive”是组合框中索引 0 处的第一项。
动态生成组合框并添加处理程序。
You will want to use the ComboBox_SelectedIndexChanged() event to capture that the combo box item has been changed. At that point, you will need to check to see which combo box item has been selected and make a decision as to whether the TextBox should be enabled or not. Here is an example. Note: This example assumes that "Alive" is the first item in your combobox at the 0 index.
Dynamically generate the combobox and add handler.
我想您将有 10 个组合框...同样,您将有 10 个文本框。
在这种情况下...一旦您以 AndyPerfect 和 Joseph 的身份附加并处理事件...在该方法中,您将需要编写一些代码来了解需要启用/禁用哪些文本框。
首先,您需要知道哪个组合框触发了事件:这是使用“sender”参数完成的。
ctype(sender, Combobox)
访问 ComboBox 的方法和属性。一旦您知道哪个组合,您需要激活/停用正确的文本框。
为此,您需要在创建组合框时在组合框的“TAG”属性中添加对文本框的引用。
然后......你简单地使用:
I suppose that as you will have 10 comboboxes... in the same way you'll have 10 textboxes.
In that case... once you attach and handle the event as AndyPerfect and Joseph... in that method you will need code something to know which of the Textboxs you need to enable/disable.
First you need to know which combobox triggered the event: this is done using the "sender" parameter.
ctype(sender, Combobox)
to access the methods and properties of the ComboBox.Once you know which combo, you need to activate/deactivate the correct textbox.
To accomplish this, you'll need to add a reference to the TextBox in the "TAG" property of the Combobox at the moment of creating it.
Then... you simple use:
这就是我最后如何写的。我感谢所有的帮助!谢谢你!
Here is how I ended up writing it in the end. I appreciate all the help! Thank you!