当我通过VB.NET代码插入组合框时,如何让它有一个事件?

时间:2010-02-13 01:06:27

标签: vb.net events combobox

您好我还是VB.NET的新手...... 对于具有按钮(Button1)的表单,我有以下代码。 当我按下这个按钮时,它会添加一个带有一些值的组合框(每次按下按钮时它会在下面添加一个新的组合框)。 如何设置和事件,以便在更改组合框时,文本框将显示在其右侧? 我基本上是根据每个组合框中选择的内容来考虑不同的行为。

Public Class frmEditor
    Private Const rowHeight = 25
    Dim datarows() As Action
    Dim currentrow As Integer
    Dim starttop As Integer
    Private Sub frmEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        currentrow = 1
        starttop = 20
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CreateRow()
        currentrow = currentrow + 1
        starttop = starttop + rowHeight
    End Sub

    Private Sub CreateRow()
        Dim newrow As Action = New Action()
        ReDim Preserve datarows(currentrow)
        datarows(currentrow) = newrow
        datarows(currentrow).newAction(15, starttop, currentrow)
    End Sub

End Class

Public Class Action
    Private cbo As New ComboBox()

    Public Sub newAction(ByVal xleft As Integer, ByVal ytop As Integer, ByVal nrow As Integer)
        cbo.Top = ytop
        cbo.Left = xleft
        cbo.Visible = True
        cbo.Items.Add("Test1")
        cbo.Items.Add("Test2")
        frmEditor.Controls.Add(cbo)

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您需要添加处理程序

AddHandler cbo.newAction, AddressOf newAction

编辑:

这是我建立的一个例子。我希望能够在每个页面的底部添加一行链接,而无需将HTML添加到每个页面。所以我建立了一个控件来为我做。部分原因是添加了LoginStatus控件

        Protected Overrides Sub CreateChildControls()
           Dim lb As New LoginStatus
           With lb
               .ID = "LoginStatus1"
               AddHandler .LoggingOut, AddressOf LoginStatus1_LoggingOut
           End With
           Me.Controls.Add(lb)
        End Sub

然后我的LoggingOut处理程序有点神奇,不会破坏我的URLRewriting。

        Private Sub LoginStatus1_LoggingOut(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)
        'sign out the logged in user
        End Sub