[英]When I insert a combobox through VB.NET code how do I make it have an event?
嗨,我對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
您需要添加一個處理程序
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.