2010-02-13 60 views
1

嗨,我還是相當新的VB.NET ... 我有以下代碼的窗體有一個按鈕(Button1)。 當我按下此按鈕時,它會添加一些帶有某些值的組合框(每次按下按鈕時都會添加一個新組合框)。 如何設置和事件,以便當組合框更改時,文本框將出現在它的右側? 我基本上是根據每個組合框中選擇的內容來查看具有不同的行爲。當我通過VB.NET代碼插入組合框時,如何讓它有事件?

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 

回答

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處理器確實有點神奇,以不打破我的URL重寫。

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

嗯是的,我現在想通了。謝謝。 我添加了: AddHandler cbo.SelectedValueChanged,AddressOf ChangeCombo 到newAction方法並創建了一個名爲ChangeCombo的新方法 – 2010-02-13 01:47:57

+0

太棒了...很高興工作。 – 2010-02-13 17:51:12

相關問題