列表视图中只有一项? [英] only one item to listview?

查看:58
本文介绍了列表视图中只有一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://imageshack.us/f/641/shit2.png/ ^
为什么?

Look at the link down to see my classes
http://imageshack.us/f/641/shit2.png/[^]
 

When I compile my program, my customerframe appears, and when I write the values on its textboxes and clicking "OK", my MainForm appears with its inserted value from the customerframe. But when clicking on the add function in MainForm to write values in customerframe textboxes for another item no item will be inserted in my listview (MainForm). Why?

MainForm

private void MainForm_Load(object sender, EventArgs e)
        {
             ListViewItem listviewitem;
            // Allow the user to edit item text.
            listView1.LabelEdit = true;
            listView1.GridLines = true;
            listView1.Sorting = SortOrder.Ascending;
            // Display grid lines.
            listView1.GridLines = true;
            listView1.BorderStyle = BorderStyle.FixedSingle;
            // Allow the user to rearrange columns.
            listView1.AllowColumnReorder = true;
            // Select the item and subitems when selection is made.
            listView1.FullRowSelect = true;
 

 
            // Ensure that the view is set to show details.
            listView1.View = View.Details;            
 
            // Create column headers for sorting the listview data. 
            ColumnHeader IDHeader, FirstNameHeader, LastNameHeader, HomePhoneHeader, CellPhoneHeader, CountryHeader, ZipCodeHeader, CityHeader, StreetHeader, EmailHeader;
            IDHeader = new ColumnHeader();
            FirstNameHeader = new ColumnHeader();
            LastNameHeader = new ColumnHeader();
            HomePhoneHeader = new ColumnHeader();
            CellPhoneHeader = new ColumnHeader();
            CountryHeader = new ColumnHeader();
            ZipCodeHeader = new ColumnHeader();
            CityHeader = new ColumnHeader();
            StreetHeader = new ColumnHeader();
            EmailHeader = new ColumnHeader();          
            
 
            IDHeader.Text = "ID";
            this.listView1.Columns.Add(IDHeader);
            IDHeader.Width = 50;
 
            FirstNameHeader.Text = "First name";
            this.listView1.Columns.Add(FirstNameHeader);
            FirstNameHeader.Width = 110;
 
            LastNameHeader.Text = "Last name";
            this.listView1.Columns.Add(LastNameHeader);
            LastNameHeader.Width = 110;
 
            HomePhoneHeader.Text = "Home phone";
            this.listView1.Columns.Add(HomePhoneHeader);
            HomePhoneHeader.Width = 120;
 
            CellPhoneHeader.Text = "Cell phone";
            this.listView1.Columns.Add(CellPhoneHeader);
            CellPhoneHeader.Width = 110;
 
            CountryHeader.Text = "Country";
            this.listView1.Columns.Add(CountryHeader);
            CountryHeader.Width = 110;
 
            ZipCodeHeader.Text = "Zip-code";
            this.listView1.Columns.Add(ZipCodeHeader);
            ZipCodeHeader.Width = 100;
 
            CityHeader.Text = "City";
            this.listView1.Columns.Add(CityHeader);
            CityHeader.Width = 110;
 
            StreetHeader.Text = "Street";
            this.listView1.Columns.Add(StreetHeader);
            StreetHeader.Width = 110;
 
            EmailHeader.Text = "Email";
            this.listView1.Columns.Add(EmailHeader);
            EmailHeader.Width = 150;
 
            using(var customerframe = new CustomerFrame())
            {
                //if button OK is clicked then value will be inserted
                if (customerframe.ShowDialog() == DialogResult.OK) 
                {
 
                    CustomerFiles.Contact contact = customerframe.GetContact();
                    CustomerFiles.Address address = customerframe.GetAddress();
                    CustomerFiles.Phone phone = customerframe.GetPhone();
                    CustomerFiles.Email email = customerframe.GetEmail();
 
                    //Items in my listview
                     listviewitem = new ListViewItem();
                    listviewitem.SubItems.Add(contact.FirstName);
                    listviewitem.SubItems.Add(contact.LastName);
                    listviewitem.SubItems.Add(phone.Home);
                    listviewitem.SubItems.Add(phone.Mobile);
                    listviewitem.SubItems.Add(address.Country);
                    listviewitem.SubItems.Add(address.ZipCode);
                    listviewitem.SubItems.Add(address.City);
                    listviewitem.SubItems.Add(address.Street);
                    listviewitem.SubItems.Add(email.Personal);
                    this.listView1.Items.Add(listviewitem);
                    
                }
            }
        }

主窗体中的添加功能

private void addToolStripMenuItem_Click_1(object sender, EventArgs e)
{
//when trying to add more then two times i get error "Cannot access a disposed object."
    customerframe.Show();
 
}
 
CustomerFrame class
 
<pre>        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

CustomerFrame

internal CustomerFiles.Contact GetContact()
        {
            CustomerFiles.Contact contact = new CustomerFiles.Contact();
            contact.FirstName = tbFirstName.Text;
            contact.LastName = tbLastName.Text;
            
            return contact;
 
        }
        internal CustomerFiles.Address GetAddress()
        {
            address.City = tbCity.Text;
            address.Street = tbStreet.Text;
            address.ZipCode = tbZipCode.Text;
            address.country = cbCountry.Text;
 
            return address;
        }
        internal CustomerFiles.Phone GetPhone()
        {
            CustomerFiles.Phone phone = new CustomerFiles.Phone();
            phone.Home = tbHomePhone.Text;
            phone.Mobile = tbCellPhone.Text;
 
            return phone;
            
        }
        internal CustomerFiles.Email GetEmail()
        {
            CustomerFiles.Email email = new CustomerFiles.Email();
            email.Personal = tbEmail.Text;
 
            return email;
        }
        public override string ToString()
        {
            return string.Format("[{0}, {1}, {2}]", contact.ToString(), address.ToString(), country.Name);
 
        }


推荐答案

 ,但是在MainForm中单击添加功能以在customerframe文本框中为另一个项目写值时,没有项将插入到我的列表视图中"

哪些添加功能?这段代码吗?

What add function?  This code?

私有 无效 addToolStripMenuItem_Click_1 ( 对象 发​​件人 EventArgs e )
{
//当尝试添加两次以上时,出现错误无法访问已处置的对象."
    customerframe
. 显示 ();

}

CustomerFrame

< pre>        private void btnOk_Click ( object 发件人 EventArgs e )
      
{
          
DialogResult = DialogResult . 确定 ;
          
关闭 ();
      
}

private void addToolStripMenuItem_Click_1(object sender, EventArgs e)
{
//when trying to add more then two times i get error "Cannot access a disposed object."
    customerframe
.Show();

}

CustomerFrame class

<pre>        private void btnOk_Click(object sender, EventArgs e)
       
{
           
DialogResult = DialogResult.OK;
           
Close();
       
}

它抱怨什么对象? Close()调用有什么作用?


这篇关于列表视图中只有一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆