简体   繁体   English

如何检查所选项目是否已从先前选择的项目更改?

[英]How do I check if a selected item has changed from the previously selected item?

I have a listbox in my winform, when an item in the listbox is selected, a value is placed into a textbox in the same Form. 我的winform中有一个列表框,当在列表框中选择一个项目时,会将值放入同一窗体的文本框中。 There my many items in my listbox which when selected, i want the text box to be empty so i can pass in the new value. 在我的列表框中有很多项目,选中它们后,我希望文本框为空,以便可以传递新值。 How do i check is if the user has clicked on something other their initial selected item? 我如何检查用户是否还单击了其他最初选择的项目? i get the currently selected item like this below: 我得到如下所示的当前选择的项目:

var selectedItem = (ReportItems)listbox.selectedItem

You can add a global variable for your ReportItems and call it 'selItem'. 您可以为ReportItems添加一个全局变量,并将其称为“ selItem”。

After the user changed the selected Item you check the "new" selectedItem with the 'selItem'-variable.. i don't think that a listbox has a method that can check if the selection has changed from the previous one.. 用户更改选定的项目后,请使用“ selItem”变量检查“新的” selectedItem。.我认为列表框没有一种方法可以检查选择项是否与上一个有所更改。

You can use the SelectedIndexChanged event on your ListBox . 您可以在ListBox上使用SelectedIndexChanged事件。 You can create an event handler for this event to determine when the selected index in the ListBox has been changed. 您可以为此事件创建一个事件处理程序,以确定何时更改了ListBox的所选索引。 This can be useful when you need to display information in other controls based on the current selection in the ListBox. 当您需要基于列表框中的当前选择在其他控件中显示信息时,此功能很有用。 You can use the event handler for this event to load the information in the other controls. 您可以使用事件处理程序将此事件加载到其他控件中。

See MSDN documentation: link 请参阅MSDN文档: 链接

I'm not sure if there is a reason you're not leveraging the SelectionChanged event of the ListBox or not but you should be if you're not. 我不确定是否有原因您没有利用ListBoxSelectionChanged事件,但如果没有,则应该这样做。 Further, determining if it's different than the initially selected item should be pretty straight forward because you can save the initially selected item in a private variable in your form and compare every time the method handler for SelectionChanged fires. 此外,确定它是否与初始选择的项目不同应该很简单,因为您可以将初始选择的项目保存在表单的private变量中,并在每次触发SelectionChanged的方法处理程序时进行比较。

Other than this, there's not much more I can suggest because your questions isn't terribly clear and there is no code to look at. 除此之外,我无法提供更多建议,因为您的问题并不十分清楚,也没有代码可看。

My solution was to always clear the textbox first. 我的解决方案是始终先清除文本框。 So as soon as a user selects an item in the listview, rather than populating the textbox straight away, clear the textbox before populating it. 因此,只要用户在列表视图中选择一个项目,而不是立即填充文本框,请在填充文本框之前先清除它。

clearText(); clearText(); is called soon as a listbox item is clicked. 单击列表框项目后立即调用。

    public void clearText()
    {
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox5.Clear();
        textBox6.Clear();
        textBox7.Clear();
        textBox8.Clear();
        textBox9.Clear();
        textBox10.Clear();
        textBox11.Clear();
        textBox12.Clear();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM