活动新秀;按 F5 后引发操作

发布于 2024-09-13 01:48:24 字数 429 浏览 5 评论 0原文

所以,是的,我对创建自己的自定义事件非常陌生。当我将控件放在表单上时,我可以完成基本操作,但这个有点复杂。我有一个应用程序,它读取 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

§对你不离不弃 2024-09-20 01:48:24

根据我从评论中收集到的信息,您希望将事件添加到在运行时创建的表单对象。对对象使用 AddHandler 命令。这样做的效果是:

AddHandler NameOfFormObject.TypeOfAction, AddressOf HowToHandle

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

这样做,您将能够修改在运行时创建的对象的事件。就您而言,听起来您会想要使用约瑟夫推荐的操作,并最终合并提供的两种解决方案,如下所示

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

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:

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

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
旧城烟雨 2024-09-20 01:48:24

您将需要使用 ComboBox_SelectedIndexChanged() 事件来捕获组合框项目已更改。此时,您需要检查已选择哪个组合框项目,并决定是否应启用文本框。这是一个例子。注意:此示例假设“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

动态生成组合框并添加处理程序。

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

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.


    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)
笑叹一世浮沉 2024-09-20 01:48:24

我想您将有 10 个组合框...同样,您将有 10 个文本框。

在这种情况下...一旦您以 AndyPerfect 和 Joseph 的身份附加并处理事件...在该方法中,您将需要编写一些代码来了解需要启用/禁用哪些文本框。

首先,您需要知道哪个组合框触发了事件:这是使用“sender”参数完成的。 ctype(sender, Combobox) 访问 ComboBox 的方法和属性。

一旦您知道哪个组合,您需要激活/停用正确的文本框。
为此,您需要在创建组合框时在组合框的“TAG”属性中添加对文本框的引用。

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

然后......你简单地使用:

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

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.

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
南街女流氓 2024-09-20 01:48:24

这就是我最后如何写的。我感谢所有的帮助!谢谢你!

    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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文