列表视图中只有一项

列表视图中只有一项

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

问题描述

查看链接,以查看我的课程
http://imageshack.us/f/641/shit2.png/ [ ^ ]
 

当我编译程序时,出现我的客户框架,当我在其文本框中写入值并单击确定"时,我的MainForm出现,并带有从客户框架中插入的值.但是当单击MainForm中的add函数来编写时 另一个项目的客户框架文本框中的值不会在我的列表视图(MainForm)中插入任何项目. 为什么?

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);

        }


推荐答案

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

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()调用有什么作用?


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

08-06 13:30