gpt4 book ai didi

VB.NET:SelectedIndexChanged 多次触发

转载 作者:行者123 更新时间:2023-12-04 05:49:01 27 4
gpt4 key购买 nike

我正在尝试以编程方式将未指定数量的新用户控件添加到表单中。每次在包含在 UserControl 中的 ComboBox 中选择一个条目时,都会添加一个。

问题是,SelectedIndexChanged 事件完全不规律地触发。有时是两次,有时是 3 次,等等,但绝不是一次。无论我将组合框的 SelectedIndex 设置为 -1 多少次,它都会至少触发一次,且 SelectedIndex 为 0。有时 Itemselected 事件会在 SelectedIndexChanged 事件之间触发多次。

InvoiceEntry.vb 片段:

Public Event ItemSelected As EventHandler
Private Sub cboItem_SelectedIndexChanged(sender As System.Object, _
e As System.EventArgs) Handles cboItem.SelectedIndexChanged
RaiseEvent ItemSelected(Me, EventArgs.Empty)
End Sub

发票.vb 片段:
Private numEntries As Integer = 1

Public Sub invEntry1_ItemSelected() Handles invEntry1.ItemSelected
numEntries += 1

Dim newEntry As InvoiceEntry = invEntry1
Dim pt As Point = newEntry.Location
pt.Y += 30

newEntry.Location = pt
newEntry.Name = "invEntry" + numEntries.ToString

pnlEntries.Controls.Add(newEntry)

结束子

我完全不知道出了什么问题。如果您需要更多信息,请告诉我,因为我将监控此线程,直到我或其他人弄清楚为止。

最佳答案

据我所知,当您添加新组合框时,所选索引此时正在发生变化(这是它第一次触发的时候)。每次您以编程方式更改值时,它也会触发。

如果您想在用户从组合框中选择某些内容后生成控件,请尝试使用

ComboBox.SelectionChangeCommitted

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted.aspx

关于VB.NET:SelectedIndexChanged 多次触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10324679/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com