当前位置:编程学习 > C#/ASP.NET >>

新手,关于listView的问题





namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //定义商品名称
        public string goods1 = "牙膏", goods2 = "方便面", goods3 = "香烟";
        //定义商品价格
        public int goods1P = 10, goods2P = 5, goods3P = 15;
        //定义总价
        public int totalP = 0;


        private void Form1_Load(object sender, EventArgs e)
        {
            //清空lv1和lv2
            listView1.Items.Clear();
            listView2.Items.Clear();

            //清空txt1
            textBox1.Text = "";
        }

        //点击按键“看看有什么”
        private void bt1_Click(object sender, EventArgs e)
        {
            /*   怎么把定义的goods1,goods2,goods3
                 和goods1P, goods2P, goods3P,
                 一一对应的放入listView1         */
        }

        //点击按键“购买”,实现将listView1中选中的商品,价格,数量放入listView2,并计算总价,将总价显示在txt1中
        private void bt2_Click(object sender, EventArgs e)
        {
            /*  问题:1、如何将listView1中选中的商品,价格,放入listView2? 注:已设置为一次只能选择一个
                      2、因可能不是购买一种商品,购买的数量如何定义比较好?
                      3、如何弹出一个新窗口,让消费者填入购买数量?          */
        }

        //点击按键“退货”,实现将listView2中选中的商品删除,并更新总价,将总价显示在txt1中
        private void bt3_Click(object sender, EventArgs e)
        {
            /*  问题:4、如何弹出一个新窗口,让消费者填入退货数量?          
                      5、如何读出listView中某一行某一列显示的string?        */
        }


    }
}
listview C# --------------------编程问答--------------------

            ListViewItem item = listView1.FocusedItem;
            ListViewItem nitem = new ListViewItem();
            nitem.Text = item.Text;

            nitem.SubItems.Add(item.SubItems[0].Text);
......
            listView2.Items.Add(nitem);
--------------------编程问答--------------------
引用 1 楼 bdmh 的回复:

            ListViewItem item = listView1.FocusedItem;
            ListViewItem nitem = new ListViewItem();
            nitem.Text = item.Text;

            nitem.SubItems.Add(item.SubItems[0].Text);
......
            listView2.Items.Add(nitem);


谢谢你的回复,说实话,我没有看明白,能加点注释吗?谢谢 --------------------编程问答-------------------- 复制listView选中的项:
            foreach (var item in listView1.SelectedItems)
            {
                var lvi = new ListViewItem;
                lvi.Text = item.Text;
                lvi.SubItems.Add(item.SubItems[0].Text);
                lvi.SubItems.Add(item.SubItems[1].Text);
                ...
                listView2.Item.Add(lvi);
            }

关于数量的设计,最简单的,用户点一次购买,就增加一个,如果多于一个,就归并到现有的条目中。

如何读出listView中某一行某一列显示的string?

listView.Items[m].Text m行第一列
listView.Items[m].SubItems[n] m行的n-2列 --------------------编程问答--------------------

            ListViewItem item = listView1.FocusedItem;//初始化一个新行item,并指向lv1的当前焦点那行?
            ListViewItem nitem = new ListViewItem();// 初始化一个新行nitem?
            nitem.Text = item.Text;//新行nitem的txt设置为item的txt?

            nitem.SubItems.Add(item.SubItems[0].Text);//这行不懂,添加的txt的坐标是什么?x,y x=行  y=列
......
            listView2.Items.Add(nitem);
--------------------编程问答-------------------- 大概这样子 无聊实现了下


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        //定义商品名称
        public string goods1 = "牙膏", goods2 = "方便面", goods3 = "香烟";
        //定义商品价格
        public int goods1P = 10, goods2P = 5, goods3P = 15;
        //定义总价
        public int totalP = 0;
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //清空lv1和lv2
            listView1.Items.Clear();
            listView2.Items.Clear();
 
            //清空txt1
            textBox1.Text = "";
        }
 
        //点击按键“看看有什么”
        private void bt1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            listView1.Items.Add(new Goods(goods1, goods1P, totalP));
            listView1.Items.Add(new Goods(goods2, goods2P, totalP));
            listView1.Items.Add(new Goods(goods3, goods3P, totalP));
        }
 
        //点击按键“购买”,实现将listView1中选中的商品,价格,数量放入listView2,并计算总价,将总价显示在txt1中
        private void bt2_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection lvic = listView1.SelectedItems;
            IEnumerator ienum = lvic.GetEnumerator();
            while (ienum.MoveNext())
            {
                Goods goods = ienum.Current as Goods;
                if (goods == null)
                {
                    //logger.error("");
                    continue;
                }

                List<ListViewItem> relatedItem = listView2.Items.Cast<ListViewItem>().Where(x => x.Text == goods.Text).ToList() as List<ListViewItem>;
                if (relatedItem.Count == 0)
                {
                    Goods newgood = goods.GetGoods(1);
                    if(newgood!=null)
                        listView2.Items.Add(newgood);
                }
                if (relatedItem.Count == 1)
                {
                    if(goods.GetGoods(1)!=null)
                        (relatedItem[0] as Goods).Count++;
                }
            }
        }
 
        //点击按键“退货”,实现将listView2中选中的商品删除,并更新总价,将总价显示在txt1中
        private void bt3_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection lvic = listView2.SelectedItems;
            if (lvic.Count != 1)
                return;
            Goods goods = lvic[0] as Goods;
            if (goods != null)
            {
                int backcount = 1;//默认1,可以自己设置
                if (backcount <= 0)
                {
                    //退还数量不对
                }
                List<ListViewItem> relatedItem = listView1.Items.Cast<ListViewItem>().Where(x => x.Text == goods.Text).ToList() as List<ListViewItem>;
              
                if (relatedItem.Count == 0)
                {
                    //找不到,出错!
                }
                if (relatedItem.Count == 1)
                {
                    Goods basegood = relatedItem[0] as Goods;
                    if (backcount > goods.Count)
                    {
                        //退还数量太大
                    }
                    if (goods.GetGoods(backcount) != null)
                        basegood.Count += backcount;
                }
            }
        }
 
 
    }

/// <summary>
    /// 商品类
    /// </summary>
public class Goods : ListViewItem 
    {

        string m_name = "";
        int m_price = 0;
        int m_count = 0;

        public Goods(string name,int price, int count)
        {
            m_name = name;
            m_price = price;
            m_count = count;

            loadDefault();
        }

        public int Count
        {
            get { return m_count; }
            set 
            { 
                m_count = value;
                SubItems.Remove(SubItems[SubItems.Count - 1]);
                SubItems.Add(m_count.ToString());
            }
        }

        private void loadDefault()
        {
            Text = m_name;
            SubItems.Add(m_price.ToString());  
            SubItems.Add(m_count.ToString());
        }


        /// <summary>
        /// 取出货物
        /// </summary>
        /// <param name="backcount"></param>
        /// <returns></returns>
        public Goods GetGoods(int backcount)
        {
            Goods regood = null;
            if (Count >= backcount)
            {
                regood = new Goods(m_name, m_price, backcount);
                Count -= backcount;
            }
            else
            {
                //数量不够
            }

            return regood;
        }
    }
}
--------------------编程问答-------------------- 按键“购买”的选取功能已经实现,谢谢楼上两位



现在问题是:
1,点击完购买后,如何让listView1.FocusedItem释放焦点?
2,点击完购买后如何弹出一个新窗口,让消费者填入购买数量?
3,储存购买“每个商品的数量”的变量该如何设计?
4,caozhy说归并到现有的条目中,如何实现?

谢谢。 --------------------编程问答-------------------- 楼上问题1:listView1.SelectedItems.Clear();
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,