【全国软件开发交流群】131627487 【全国软件测试交流群】144758063 【全国计算机技术交流】131628626

ListView简单应用

代码如下:

  

        private void button1_Click(object sender, EventArgs e)
        {
            listView1.LargeImageList = imageList1;
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入要添加的数据", "提示");
            }
            else
            {
                if (listView1.Items.Count > 0)
                {
                    for (int i = 0; i < listView1.Items.Count;i++ )
                    {
                        if(textBox1.Text==listView1.Items[i].Text.ToString())
                        {
                            MessageBox.Show("你要添加的数据已存在","提示");
                            textBox1.Text = "";
                            textBox1.Focus();
                            return;//注意体会加上与去掉return的区别
                        }
                    }
                    listView1.Items.Add(textBox1.Text, 0);
                    MessageBox.Show("添加成功", "提示");
                    textBox1.Text = "";
                    textBox1.Focus();
                }
                else
                {
                    listView1.Items.Add(textBox1.Text, 0);
                    MessageBox.Show("添加成功", "提示");
                    textBox1.Text = "";
                    textBox1.Focus();
                }
            }
        }

 

运行后如下图:

posted @ 2011-05-18 16:36  编程中国  阅读(164)  评论(0)    收藏  举报