Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 您能在列表框中添加新行吗?_C#_Listbox - Fatal编程技术网

C# 您能在列表框中添加新行吗?

C# 您能在列表框中添加新行吗?,c#,listbox,C#,Listbox,我有3个文本框,当您单击“添加”时,这些文本框需要传递到列表框上的1行 但当我在列表框中单击add时,它每次都会在新行上显示信息 我用richtextbox试过,但效果不太好。我也做了一些研究,发现在列表框上也可以,但你需要在我的代码中添加一些东西 到目前为止,我的代码是: private void btnAdd_Click(object sender, EventArgs e) { decimal iQuantity; decimal iPrice; decimal

我有3个文本框,当您单击“添加”时,这些文本框需要传递到列表框上的1行

但当我在列表框中单击add时,它每次都会在新行上显示信息

我用richtextbox试过,但效果不太好。我也做了一些研究,发现在列表框上也可以,但你需要在我的代码中添加一些东西

到目前为止,我的代码是:

private void btnAdd_Click(object sender, EventArgs e)
{
    decimal iQuantity;
    decimal iPrice;
    decimal Fullprice = 0;

    string Product = cmbItem.Text;
    string Quantity = txtQuantity.Text;
    string Price = txtPrice.Text;

    decimal.TryParse(Quantity, out iQuantity);
    decimal.TryParse(Price, out iPrice);

    if (iQuantity <= 0)
    {
        MessageBox.Show("Please enter Quantity larger than 0", "Quantity", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else if (iPrice <= 0)
    {
        MessageBox.Show("Please enter a valid price above 0", "Price", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        Fullprice = iPrice * iQuantity;
        rtbBasket.AppendText(string.Format("{0} {1} £{2} £{3}",Product,iQuantity,iPrice,Fullprice + Environment.NewLine));
    }
}
private void btnAdd\u单击(对象发送者,事件参数e)
{
十进制分类学;
十进位价格;
十进制全价=0;
字符串Product=cmbItem.Text;
字符串数量=txtQuantity.Text;
字符串价格=txtPrice.Text;
十进制.色氨酸(数量,不含液体);
十进制。特里帕色(价格,不含iPrice);
如果(液体)
您缺少上面的一些代码

private void btnAdd_Click(object sender, EventArgs e)
{
    decimal iQuantity;
    decimal iPrice;
    decimal Fullprice = 0;

    string Product = cmbItem.Text;
    string Quantity = txtQuantity.Text;
    string Price = txtPrice.Text;

    decimal.TryParse(Quantity, out iQuantity);
    decimal.TryParse(Price, out iPrice);

    if (iQuantity <= 0)
    {
        MessageBox.Show("Please enter Quantity larger than 0", "Quantity", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else if (iPrice <= 0)
    {
        MessageBox.Show("Please enter a valid price above 0", "Price", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        Fullprice = iPrice * iQuantity;
                                                                                    £{2}                           £{3}",Product,iQuantity,iPrice,Fullprice + Environment.NewLine));
    }

rtbBasket.AppendText(Product + " " + Quantity + " " Price);


}
private void btnAdd\u单击(对象发送者,事件参数e)
{
十进制分类学;
十进位价格;
十进制全价=0;
字符串Product=cmbItem.Text;
字符串数量=txtQuantity.Text;
字符串价格=txtPrice.Text;
十进制.色氨酸(数量,不含液体);
十进制。特里帕色(价格,不含iPrice);

如果(iQuantity您可以构建一个字符串,然后将其传递给listbox的items集合

退房: 及


希望这有帮助,顺便说一句,下次试着解决这个问题,并寻找参考资料(可能是谷歌)。

好的,你只需要这样做就行了

ListBox listBox1=新建ListBox()


对于(int x=1;x这是一个开始面向对象编程的好例子。您可以通过
listbox1.items.add(string)
将项目作为字符串添加到列表框中

但最好的做法是使用自定义项目类来存储项目信息。
在BindingList中使用这些项并将其绑定到ListBox。这将使删除或编辑项更加容易

 public class CustomItem 
 {

    private string sProduct;
    public string Product
    {
      get { return sProduct; }
      set { sProduct = value; }
    }

    // i used int instead of string here
    private int nQuantity;
    public int Quantity
    {
      get { return nQuantity; }
      set { nQuantity = value; }
    }
    // i used double instead of string here    
    private double dPrice;
    public double Price
    {
      get { return dPrice; }
      set { dPrice = value; }
    }

    // simple constructor
    public CustomItem(string sProduct, int nQuantity, double dPrice)
    {
      this.Product = sProduct;
      this.Quantity = nQuantity;
      this.Price = dPrice;
    }

    // the listbox will use this method to show the item
    public override string ToString()
    {
      return sProduct + " " + nQuantity.ToString() + " " + dPrice.ToString() ;
    }
  }
示例如何在带有列表框和要添加的按钮的简单表单中使用

using System.ComponentModel;    
//...
public partial class Form1 : Form
{

    // declare your custom binding list 
    BindingList<CustomItem> myList = null;

    public Form1()
    {
      InitializeComponent();

      // initialize your list.
      myList = new BindingList<CustomItem>();    
      // binding your list to your listbox                 
      listBox1.DataSource = myList;
    }


    // a simple button to add items, parse your values here
    private void button1_Click(object sender, EventArgs e)
    {
      myList.Add(new CustomItem("Productname",10, 24.99));           
    }


  }
使用System.ComponentModel;
//...
公共部分类Form1:Form
{
//声明自定义绑定列表
BindingList myList=null;
公共表格1()
{
初始化组件();
//初始化您的列表。
myList=新绑定列表();
//将列表绑定到列表框
listBox1.DataSource=myList;
}
//一个简单的按钮来添加项目,在这里解析你的值
私有无效按钮1\u单击(对象发送者,事件参数e)
{
添加(新的CustomItem(“Productname”,10,24.99));
}
}

更好的方法是使用listview或datagridview。datagridview也可以使用,类似的listview有点棘手。

最聪明的方法是用你的三个属性创建一个
itemclass
。使用
List
来添加和删除你的项目。使用listbox并将它的
datasource属性设置到你的列表中。@Koryu I am qu这是新的,你有没有参考资料来帮助这一点,或者你可以提供一些帮助?谢谢。检查我的答案,使用列表和数据绑定是我的最佳实践。这也将使编辑和删除项目更加容易。谢谢你做了一些代码,如果你滚动,你可以看到我已经在标签中移动数据哈哈,还有你的代码bottom对我不起作用,它说“只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句”。谢谢你的参考,我检查了谷歌,但那里的答案说我无法完成。
 public class CustomItem 
 {

    private string sProduct;
    public string Product
    {
      get { return sProduct; }
      set { sProduct = value; }
    }

    // i used int instead of string here
    private int nQuantity;
    public int Quantity
    {
      get { return nQuantity; }
      set { nQuantity = value; }
    }
    // i used double instead of string here    
    private double dPrice;
    public double Price
    {
      get { return dPrice; }
      set { dPrice = value; }
    }

    // simple constructor
    public CustomItem(string sProduct, int nQuantity, double dPrice)
    {
      this.Product = sProduct;
      this.Quantity = nQuantity;
      this.Price = dPrice;
    }

    // the listbox will use this method to show the item
    public override string ToString()
    {
      return sProduct + " " + nQuantity.ToString() + " " + dPrice.ToString() ;
    }
  }
using System.ComponentModel;    
//...
public partial class Form1 : Form
{

    // declare your custom binding list 
    BindingList<CustomItem> myList = null;

    public Form1()
    {
      InitializeComponent();

      // initialize your list.
      myList = new BindingList<CustomItem>();    
      // binding your list to your listbox                 
      listBox1.DataSource = myList;
    }


    // a simple button to add items, parse your values here
    private void button1_Click(object sender, EventArgs e)
    {
      myList.Add(new CustomItem("Productname",10, 24.99));           
    }


  }