VB.NET:SelectedIndexChanged多次触发 [英] VB.NET: SelectedIndexChanged firing multiple times

查看:226
本文介绍了VB.NET:SelectedIndexChanged多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



问题是,SelectedIndexChanged事件完全不正常地触发。有时两次,有时3次等,但从不只是一次。无论多少次将组合框的SelectedIndex设置为-1,它将至少一次使用SelectedIndex为0.有时Itemselected事件会在SelectedIndexChanged事件之间触发多次。



InvoiceEntry.vb片段:

 公共事件ItemSelected As EventHandler 
Private Sub cboItem_SelectedIndexChanged(sender As System.Object,_
e As System.EventArgs)处理cboItem.SelectedIndexChanged
RaiseEvent ItemSelected(Me,EventArgs.Empty)
End Sub

Invoice.vb片段:

 私有numEntries为整数= 1 

Public Sub invEntry1_ItemSelected()处理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

pnlEntrie s.Controls.Add(newEntry)

结束子


$ b $我完全失去了什么是错的。如果您需要更多信息,请通知我,因为我将监视此线程,直到我或其他人将其显示出来。

解决方案

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



如果要在用户从组合框中选择某项后生成控件,请尝试使用

  ComboBox.SelectionChangeCommitted 

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


I am trying to programmatically add an unspecified amount of new UserControls to a form. One will be added every time an entry is selected in a ComboBox contained within the UserControl.

Problem is, the SelectedIndexChanged event fires completely erratically. Sometimes twice, sometimes 3 times, etc., but never just once. No matter how many times I set the combobox's SelectedIndex to -1, it fires at least once with a SelectedIndex of 0. Sometimes the Itemselected event fires multiple times inbetween SelectedIndexChanged events.

InvoiceEntry.vb snippet:

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

Invoice.vb snippet:

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)

End Sub

I'm at a complete loss as to what is wrong. Please let me know if you need more information, as I will be monitoring this thread until I or someone else figures it out.

解决方案

As far as I know, when you add new combo box the selected index is changing at this time (this is when it triggers first time). It will also trigger every time you programatically change a value.

If you want to generate controls after user has selected something from the combo box try using

ComboBox.SelectionChangeCommitted

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

这篇关于VB.NET:SelectedIndexChanged多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆