在vb(wpf)中单击按钮动态创建ComboBox [英] Dynamically create ComboBox on click of button in vb (wpf)
问题描述
如何动态创建ComboBox,以及如何将SelectionChanged事件的事件处理程序添加到动态创建的ComboBox.
我尝试过此代码
How to Create ComboBox dynamically and add event handler for SelectionChanged event to that dynamically created ComboBox.
I tried this code
Private Sub CreateWPFComboBox()
Dim cbox As New ComboBox()
cbox.Width = 120
cbox.Height = 25
Dim cboxitem1 As New ComboBoxItem()
Dim textitem1 As New TextBox()
cboxitem1.Content = "C# Corner"
cbox.Items.Add(cboxitem1)
Dim cboxitem2 As New ComboBoxItem()
Dim textitem2 As New TextBox()
cboxitem2.Content = "VB.NET Heaven"
cbox.Items.Add(cboxitem2)
textitem2.Text = cboxitem2.ToString()
Dim cboxitem3 As New ComboBoxItem()
Dim textitem3 As New TextBox()
cboxitem3.Content = "MSDN"
cbox.Items.Add(cboxitem3)
textitem3.Text = cboxitem3.ToString()
cbox.SelectionChanged += New AddressOf CreateWPFComboBox
parent.Children.Add(cbox)
End Sub
但是事件处理程序无法正常工作会给我错误
因此任何人都可以帮助我如何在选择更改列表项后将事件处理程序添加到此动态创建的combobx中.
But Event handler not working it''s give me error
so anyone help me how to add event handler to this dynamically created combobx on selection changed of list items
推荐答案
添加事件处理程序的代码
The code for adding eventhandler
cbox.SelectionChanged += New AddressOf CreateWPFComboBox
看起来像c#kode :)
它应该看起来像这样:
looks like c# kode :)
it should look like this:
AddHandler(cbox.SelectionChanged),AddressOf CreateWPFComboBox
此外,为了能够将子CreateWPFComboBox用作此事件的事件处理程序,它应如下所示:
Furthermore, to be able to use the sub CreateWPFComboBox as an eventhandler for this event, it should look like this:
Private Sub CreateWPFComboBox(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
这篇关于在vb(wpf)中单击按钮动态创建ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!