如果选择了列表框中的项目,如何启用按钮 [英] How to enable a button if an item in a listbox is selected

查看:60
本文介绍了如果选择了列表框中的项目,如何启用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这个,但是没有用.即使我选择了东西,它们仍然显示为灰色.

I tried this but it doesn't work. They're still greyed out even when I select stuff.

btnVoirFiche.Enabled = false;
btnEchangerJoueur.Enabled = false;
if (lstJoueurs.SelectedIndex != -1)
        {
            btnVoirFiche.Enabled = true;
            btnEchangerJoueur.Enabled = true;
        }
        else
        {
        }

推荐答案

您将要处理ListBox.SelectedIndexChanged事件,并在处理程序中检查特定值是否为选定值,然后相应地设置按钮的enable属性.

You'll want to handle the ListBox.SelectedIndexChanged event, and within your handler you're going to check if the specific value is the selected one, and then set you button's enable property accordingly.

类似这样的东西:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if(listBox1.SelectedIndex != -1)
    {
        btnVoirFiche.Enabled = true;
        btnEchangerJoueur.Enabled = true;
    }
    else
    {
       //whatever you need to test for
    }
}

欢呼

我不太确定您对按钮的enabled属性的逻辑是什么,所以我的回答很笼统.如果您在问题中添加了详细信息,我会进行相应调整.

I'm not too sure what your logic for button's enabled property is, so my answer is pretty generic. If you add details to you question, I'll adapt accordingly.

这篇关于如果选择了列表框中的项目,如何启用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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