我读了一整章关于c# windows应用程序的书(Barbara Doyle是作者),并尝试了其中的一个问题。我肯定没有正确理解列表框和复选框的基本原理。
我的问题是,如何清除列表框。我不是指pastyBox.Items.Clear();,因为该方法清空了整个列表框,但是我该如何……“重置它”,如果你想在一个新的订单上开始新的工作(我觉得重置这个词可能更合适),你会这样想吗?此外,无论我做什么,我都无法清除文本框,即使我将其设置为txtBoxTotal = "";或尝试将值设置为零。
我完全遗漏了一些东西,希望有人能指出。也许这是一个非常简单的错误,或者只是一些我不知道的知识。以下是清除复选框的方式的代码,我删除了试图解决文本框和列表框的几行代码
我想也许在最后它是一个选择的索引,因为我运行了一个数组……我似乎不再有任何线索了。
编辑::到目前为止,当我加载我的应用程序时,假设我点击了baklava和tea,它在文本框中显示3.80,当我点击clear时,它移除了tea前面的箭头,baklava仍然被选中,txtbox不会改变我希望文本框被清除,被选中的baklava被取消选中
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
decimal[] pastryPrice = { 3.00m, 2.85m, 3.25m, 4.35m, 3.40m, 2.30m, 2.90m,
1.75m,2.00m, 1.50m };
decimal pastry;
decimal total;
decimal drinkCost = 0;
private void pastryBox_SelectedIndexChanged(object sender, EventArgs e)
{
switch (pastryBox.SelectedIndex)
{
case 0:
pastry = pastryPrice[0];
break;
case 1:
pastry = pastryPrice[1];
break;
case 2:
pastry = pastryPrice[2];
break;
case 3:
pastry = pastryPrice[3];
break;
case 4:
pastry = pastryPrice[4];
break;
case 5:
pastry = pastryPrice[5];
break;
case 6:
pastry = pastryPrice[6];
break;
case 7:
pastry = pastryPrice[7];
break;
case 8:
pastry = pastryPrice[8];
break;
case 9:
pastry = pastryPrice[9];
break;
}
}
private void txtBoxTotal_TextChanged(object sender, EventArgs e)
{
total = pastry + drinkCost;
txtBoxTotal.Text = Convert.ToString(total);
}
private void ComputeDrinkCost_CheckedChanged(object sender, EventArgs e)
{
if (this.Coffee.Checked)
{
drinkCost = 2.00m;
}
if (this.Tea.Checked)
{
drinkCost = 1.80m;
}
if (this.Water.Checked)
{
drinkCost = 0.00m;
}
if (this.Cream.Checked)
{
if ((this.Coffee.Checked) || (this.Tea.Checked))
{
drinkCost += 0m;
}
else
{
drinkCost += .50m;
}
}
if (this.Sugar.Checked)
{
if ((this.Coffee.Checked) || (this.Tea.Checked))
{
drinkCost += 0m;
}
else
{
drinkCost += .30m;
}
}
if (this.WhippedCream.Checked)
{
drinkCost += .60m;
}
if (this.Cocoa.Checked)
{
drinkCost += .90m;
}
if (this.Cinnamon.Checked)
{
drinkCost += .25m;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void clearOrderToolStripMenuItem_Click(object sender, EventArgs e)
{
this.clearDrinks();
}
public void clearDrinks()
{
Tea.Checked = false;
Coffee.Checked = false;
Water.Checked = false;
Cream.Checked = false;
Sugar.Checked = false;
Cinnamon.Checked = false;
WhippedCream.Checked = false;
Cocoa.Checked = false;
}
}
}发布于 2013-09-10 07:40:25
我不知道你说的“重置”列表框是什么意思,但是你可以使用
pastryBox.Items.Clear();
pastryBox.Items.AddRange(pastryPrice)这将使listBox处于设置状态(如果对其进行了编辑)。
您还可以使用
txtBoxTotal.Text = string.Empty;或
txtBoxTotal.Text = "0";数据绑定会更好,但您需要仔细阅读它。HTH。
发布于 2013-09-10 07:40:36
清除Textbox的步骤
txtBoxTotal.Text = ""对于listbox
我不明白你想问什么?只要把你的问题弄清楚就行了..
https://stackoverflow.com/questions/18708542
复制相似问题