组合框,事件,最佳实践 [英] Combo boxes, events, best pratice

查看:61
本文介绍了组合框,事件,最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


单击/填充组合框时,最好使用哪个事件
我有两个组合框-都绑定了数据.

我填充组合框1,然后根据用户的选择填充我组合框2

当我填充组合框1时,显示列表中的第一项,但我不希望显示它,因此我将selectedindex设置为-1,问题是它会触发两次被选中的索引更改事件. > 一旦selectedindex = 0,则selectedindex = -1

因此,问题在于第二个combox加载了. (所选索引的数据为0)
我可以阻止第二个组合框加载的唯一方法是在填充第一个组合框时轻按开关-即blnloading = true等.

这是最好的方法还是更好的方法,还是我应该使用其他事件

干杯

Hi
Which is the best event to use when clicking / Filling a combo box
I have two combo boxes - both data bound.

I fill combo box 1 and then depending on what the user selects I fill combo box 2

When I fill combo box 1 the first item in the list is displayed, but I would prefer that not to display, so I set the selectedindex to -1 the problem with this is that it fires the selected index changed event twice.
Once with selectedindex = 0 then selectedindex = -1

So the problem is that the 2nd combox loads. (with the data for the selected index of 0)
The only way I can stop the 2nd combo box from loading is to flick a switch when filling the first combo box - Ie blnloading = true etc..

Is this the best way to do it or is there a better way or should I use a different event

Cheers

推荐答案

亲爱的朋友
我认为
在填充组合框时,添加一个虚拟项目,说"--select--",以便默认情况下选中此选项,因此用户可以选择该项目
我认为这可以解决问题
Dear Friend
In my view
While populating the combo box add one dummy item say "--select--" so that by default this option is selected and thus user can select the item
I think this will solve the issue


有很多方法可以解决这个问题.

我过去完成此操作的一种方法是,在我调用类似pauseEvents之类的表单上有一个private bool变量.

在开始用值填充ComboBox之前,将pauseEvents设置为true. (或者更面向对象的方式可能是调用一个名为PauseEvents的函数,即this.PauseEvents()将该布尔值设置为true,然后调用另一个名为ResumeEvents的函数.)然后,在您之后''完成填充并将其设置成SelectedIndex为-1,将pauseEvents设置为true.

然后,在您的SelectedIndexChanged事件中,您首先拥有的是if语句...
There are a number of ways to get around this.

One way that I have done this in the past is to have a private bool variable on the form that I call something like pauseEvents.

Before you start to fill the ComboBox with values, you set pauseEvents to true. (or a more object-oriented way might be to have a function called PauseEvents that you call ie. this.PauseEvents() that sets the boolean value to true and then call another function called ResumeEvents.) Then, after you''ve finished filling it and setting the SelectedIndex to -1, set pauseEvents to true.

Then, in your SelectedIndexChanged event, the very first thing you have is an if statement...
if(pauseEvents)
  return;



这将阻止其他任何事情的发生. IMO,这样做没有错.

您也可以按照Vipin所说的做.我通常将两者结合在一起.



That will prevent anything else from running. IMO, there''s nothing wrong with doing this.

You can also do as Vipin said. I generally do a combination of the two.


这篇关于组合框,事件,最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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