C# 带有windows应用程序的列表框和文本框
我读了一整章关于c#windows应用程序的书(作者是芭芭拉·道尔),并试图在书中解决一个问题。我一定没有正确理解列表框和复选框的基本原理 我的问题是,如何清除列表框。我不是说C# 带有windows应用程序的列表框和文本框,c#,winforms,C#,Winforms,我读了一整章关于c#windows应用程序的书(作者是芭芭拉·道尔),并试图在书中解决一个问题。我一定没有正确理解列表框和复选框的基本原理 我的问题是,如何清除列表框。我不是说pastyBox.Items.Clear(),因为该方法清除了整个列表框,但我如何。。。“重置它”,如果你想在新订单上开始新的工作,你会这么想(我觉得重置这个词可能更合适)?此外,无论我做什么,我都无法清除文本框,即使我将其设置为txtBoxTotal=“”或尝试将值设为零 有些东西我完全不知道,希望有人能指出。也许这是一
pastyBox.Items.Clear()代码>,因为该方法清除了整个列表框,但我如何。。。“重置它”,如果你想在新订单上开始新的工作,你会这么想(我觉得重置这个词可能更合适)?此外,无论我做什么,我都无法清除文本框,即使我将其设置为txtBoxTotal=“”代码>或尝试将值设为零
有些东西我完全不知道,希望有人能指出。也许这是一个很容易犯的错误,或者只是一些我不知道的知识。下面是我清除复选框的代码,我删除了我试图解决文本框和列表框的几行代码
我想也许在最后,它是一个选中的索引,因为我运行了一个数组通过它。。。。我似乎再也没有任何线索了
编辑::现在,当我加载我的应用程序时,假设我单击baklava和tea,它在文本框中显示3.80,当我点击clear时,它删除了tea前面的箭头,baklava仍然处于选中状态,txtbox没有改变
我想清除文本框,取消选中选定的文本框
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;
}
}
}
我不确定“重置”列表框是什么意思,但您可以使用
pastryBox.Items.Clear();
pastryBox.Items.AddRange(pastryPrice)
如果对列表框进行了编辑,则会使其处于设置状态
你也可以使用
txtBoxTotal.Text = string.Empty;
或
最好是数据绑定,但需要仔细阅读。HTH.清除文本框
txtBoxTotal.Text = ""
对于列表框
我不明白你想问什么?
请把你的问题说得更清楚………我没有真正理解你的问题,但请尝试将空列表项绑定到列表中。fyi,这里有一个额外的右括号。我之所以保留它,是因为我不相信在编辑中对代码进行实质性的更改,尤其是对一个问题,但在正确格式化代码后,它变得明显了(我的代码编辑器确认:22个大括号开始,23个大括号结束)。我编辑了你的标题。请看“”,其中的共识是“不,他们不应该”。我现在正在阅读@johnsaunderset SelectedItem=null?进行了编辑以使其更清楚,我尝试了您先前提供的解决方案,但它不起作用。txtbox中的数据仍然保持不变
txtBoxTotal.Text = ""