-1

当我使用列表框运行程序时,在选择项目并按 Enter 后一切正常,但是当我按清除按钮并再次选择项目并按 Enter 时,没有任何反应。我已经为清除按钮尝试了以下操作,它们清除了我的标签文本和选定的列表框,但是当我在选择项目后再次尝试按 Enter 按钮时,我无法再产生另一个输出。

 public partial class frmLabSix : Form
{
    public string strCakes;
    public int cakeCost;


    public frmLabSix()
    {
        InitializeComponent();
    }

    private void lstCakes_SelectedIndexChanged(object sender, EventArgs e)
    {   
        for (int index = 0; index < lstCakes.SelectedItems.Count; index++)
        {
            strCakes += Environment.NewLine + lstCakes.SelectedItems[index].ToString();

            if (lstCakes.SelectedIndices[index] == 0) cakeCost += 18;
            if (lstCakes.SelectedIndices[index] == 1) cakeCost += 25;
            if (lstCakes.SelectedIndices[index] == 2) cakeCost += 40;
            if (lstCakes.SelectedIndices[index] == 3) cakeCost += 30;
        }

    }

    private void lblOrdered_Click(object sender, EventArgs e)
    {

    }

    private void btnEnter_Click(object sender, EventArgs e)
    {
        double tax = 1.13;
        lblOrdered.Text = "You have ordered: " + strCakes + '\n' + "Total Cost: " + (tax * cakeCost).ToString("C");
        lblOrdered.Visible = true;
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        lstCakes.SelectedItems.Clear();
        lblOrdered.Visible = false;
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

有人能告诉我这是为什么吗?

4

1 回答 1

1

这行代码隐藏了您的标签。

lblOrdered.Visible = false;

您在清除按钮单击时使标签不可见。之后你会重置它的可见性吗?我不知道,你的代码的其他部分会发生什么,但它可能应该是这样的:

// if the label is not visible, the next line won't make it visible implicitly
lblOrdered.Text = ... 
//you should set label's visibility explicitly
if (!lblOrdered.Visible)
    lblOrdered.Visible = true;

设置标签的文本不会使其可见。如果你用自己的代码隐藏它,你也应该让标签显式可见。

于 2012-10-28T01:58:19.763 回答