简体   繁体   English

菜鸟与事件; 击中F5后引起动作

[英]Rookie With Events; Causing Action after hitting F5

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." 我有一个读取.TSV的应用程序,该应用程序根据其“读取”的对象数填充控件。 So for instance: I have a file that contains 10 people objects and my code populates a form with controls for each person. 因此,例如:我有一个包含10个人对象的文件,我的代码用每个人的控件填充了一个表单。 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". 现在,最初,此文本框未启用,因为ComboBox的默认值为“ 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. 我不是很了解事件,而是通过示例学习,但是MSDN示例并没有完全消除它。

Any help (especially an awesome Step-by-Step guide) would be greatly appreciated. 任何帮助(尤其是很棒的分步指南)将不胜感激。

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. 对对象使用AddHandler命令。 Something to the effect of: 效果:

AddHandler NameOfFormObject.TypeOfAction, AddressOf HowToHandle

Private Sub HowToHandle(ByVal sender as System.Object, ByVal e As System.EventArgs)
   DropDownMenu.enabled = True
End Sub

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 在您的情况下,听起来您将要使用Josaph建议的操作,并最终将所提供的两种解决方案都纳入其中,例如

AddHandler ComboBox1.SelectedIndexChanged, AddressOf HowToHandle

Private Sub HowToHandle(ByVal sender as System.Object, ByVal e As System.EventArgs)

    If DirectCast(sender, ComboBox).SelectedIndex = 0 'Alive 
        DirectCast(DirectCast(sender, ComboBox).Tag, TextBox).enabled = True
    Else
        DirectCast(DirectCast(sender, ComboBox).Tag, TextBox).enabled = False
    End If
End Sub

You will want to use the ComboBox_SelectedIndexChanged() event to capture that the combo box item has been changed. 您将需要使用ComboBox_SelectedIndexChanged()事件来捕获组合框项目已被更改。 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. 此时,您将需要检查以查看选择了哪个组合框项目,并决定是否应启用TextBox。 Here is an example. 这是一个例子。 Note: This example assumes that "Alive" is the first item in your combobox at the 0 index. 注意:此示例假定“ Alive”是组合框中索引为0的第一项。


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If ComboBox1.SelectedIndex = 0 Then 'Alive 
            TextBox1.Enabled = True
        Else
            TextBox1.Enabled = False
        End If
    End Sub

Dynamically generate the combobox and add handler. 动态生成组合框并添加处理程序。

Dim cmb as New ComboBox
AddHandler cmb.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
Me.Controls.Add(cmb)

I suppose that as you will have 10 comboboxes... in the same way you'll have 10 textboxes. 我想您将拥有10个组合框...以同样的方式,您将拥有10个文本框。

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. 在这种情况下...一旦以AndyPerfect和Joseph身份附加并处理了该事件...,就需要使用该方法编写一些代码,以了解需要启用/禁用哪些文本框。

First you need to know which combobox triggered the event: this is done using the "sender" parameter. 首先,您需要知道哪个组合框触发了该事件:这是使用“ sender”参数完成的。 ctype(sender, Combobox) to access the methods and properties of the ComboBox. ctype(sender, Combobox)访问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. 为此,您需要在创建组合框时在组合框的“ TAG”属性中添加对文本框的引用。

Dim txt as new TextBox
Dim cmb as new ComboBox
cmb.Tag = txt

Then... you simple use: 然后...简单使用:

ctype(ctype(sender, Combobox).Tag, TextBox).Enable = true

Here is how I ended up writing it in the end. 这就是我最终编写它的方式。 I appreciate all the help! 我感谢所有的帮助! Thank you! 谢谢!

    If DirectCast(sender, ComboBox).SelectedIndex = 2 Then
        DirectCast(Me.Controls.Item(DirectCast(sender, ComboBox).Tag), TextBox).Enabled = True
    Else
        DirectCast(Me.Controls.Item(DirectCast(sender, ComboBox).Tag), TextBox).Enabled = False
    End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM