当我使用列表框运行我的程序时,一切都可以在选择项目并按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();
}
}
有人可以告诉我为什么会这样吗?
答案 0 :(得分: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;
设置标签的文字不会使其可见。如果您使用自己的代码隐藏它,您也应该明确显示标签。