Hi. I have been figuring out how to deal with my dilemma. I'm developing a mobile application in C# in Visual Studio 2005. And I have a question regarding the adding of listview items from a form to another listview in another form.

I have got my application running but it occured an error in the process. Here's my code:

This is the code from violationsForm1 form

public ListViewItem _listViewFrequent
        {
            get { return listViewFrequent.FocusedItem; }
        }

        private void mnuSelect_Click(object sender, EventArgs e)
        {  
            mainForm frm = new mainForm();
            frm._listViewFreq = _listViewFrequent;
            frm.Show();
            this.Close();         
        }

...and this code is from mainForm form.

public ListViewItem _listViewFreq
        {
            set 
            { listViewNewData.Items.Add(value); }
        }

Then when I trigger the Select Click Event an error shows like this:

MobileTApp.exe
ArgumentException
Value does not fall within the expected range.

at
ListViewItemCollection.Insert()
at

When I tried to make some tests like messagebox.show() in the select clck event. The selected item outputs with no problem. It's just when i export the item to the other form which is another listview. Also, listview does not allow adding of STRINGS. Please help.

I need to make listview selected items add to the other listview. ='(

The ListviewItem is an object in a collection that belongs to a different listview.
To copy it to the other list, you have to clone it, or remove it (decouple) from its current collection and post it into its new parent.

public ListViewItem _listViewFrequent        
{            
    get { return (ListViewItem)listViewFrequent.FocusedItem.Clone(); }        
}

private void mnuSelect_Click(object sender, EventArgs e)
        {  
            mainForm frm = new mainForm();
            frm._listViewFreq = _listViewFrequent;
            frm.Show();
            this.Close();         
        }

??? Also, listview does not allow adding of STRINGS. Please help. ???
Explain ... maybe I can show you how to do what you want with strings.

// Jerry

Thanks! It worked! However, it only adds single selections. How can you add another without overwriting the previous one?

Maybe you should send more code so I can see what you are trying to do. Everytime that you recreate that form, it calls initializecomponents which gives you a new blank listview.

I see. Well here's the complete code for violationsForm1

public partial class violationsForm1 : Form
    {
        private Form parentForm;

        public violationsForm1(Form parentForm)
        {
            InitializeComponent();
            this.parentForm = parentForm;
            this.Size = new System.Drawing.Size(parentForm.Size.Width, parentForm.Size.Height);
        }

        public ListViewItem _listViewFrequent
        {
            get { return (ListViewItem)listViewFrequent.FocusedItem.Clone(); }
        }

        private void violationsForm1_Load(object sender, EventArgs e)
        {
            //Cursor.Current = Cursors.Default;
            this.parentForm.Hide();
        }

        private void mnuCancel_Click(object sender, EventArgs e)
        {
            this.parentForm.Show();
            this.Close();
        }

        private void mnuSelect_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(listViewFrequent.FocusedItem.Text);
            
            mainForm frm = new mainForm();
            frm._listViewFreq = _listViewFrequent;
            frm.Show();
            this.Close();         
        }

...And this is the main form:

public partial class mainForm : Form
    {
        //public Form frm1;

        public mainForm()
        {
            InitializeComponent();
        }
        
        private void mainForm_Load(object sender, EventArgs e)
        {
            //this.subForm.Hide();
            //listViewNewData.Items.Add(((Form)frm1).listViewFrequent.FocusedItem);
        }

        public ListViewItem _listViewFreq
        {
            set 
            {listViewNewData.Items.Add(value); }
        }

        private void mnuFreqVio_Click(object sender, EventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;

            violationsForm1 violateform1 = new violationsForm1(this);
            violateform1.ShowDialog();
        }

        private void mnuAllVio_Click(object sender, EventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;

            violationsForm2 violateform2 = new violationsForm2(this);
            violateform2.ShowDialog();
        }

        private void mnuViewInfo_Click(object sender, EventArgs e)
        {
            viewInfoForm viewInfo = new viewInfoForm(this);
            viewInfo.ShowDialog();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void mnuSend_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtBoxPlate.Text != "")
                {
                    MessageBox.Show("Message sent to database!", "New Infringement Request",
                        MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    MessageBox.Show("Plate number does not exist!", "Data Mismatch",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            }
            catch
            {
                MessageBox.Show("Plate number does not exist!", "Data Mismatch",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
            }
        }

        private void mnuClear_Click(object sender, EventArgs e)
        {
            txtBoxPlate.Text = "";
            listViewNewData.Items.Clear();
        }

;)

private void mnuSelect_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(listViewFrequent.FocusedItem.Text);
            
            mainForm frm = new mainForm();
            frm._listViewFreq = _listViewFrequent;
            frm.Show();
            this.Close();         
        }

Why are you doing this ? mainForm frm = new mainForm();
Should'nt it be (mainForm)parentForm._listViewFreq = _listViewFrequent;

// Jerry

When I do that, "_listViewFreq" is not recognized and I don't know why. The error says it does not exist. But I do have it in mainForm.

Here is what I did in the violationsForm1 class that is a bit different than yours.

namespace harcaype
{
    public partial class violationsForm1 : Form
    {
        mainForm parentForm = null;
        public violationsForm1(mainForm parentForm)
        {
            InitializeComponent();
            this.parentForm = parentForm;
        }

        public ListViewItem _listViewFrequent
        {
            get { return (ListViewItem)listViewFrequent.FocusedItem.Clone(); }
        }


        private void selectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            parentForm._listViewFreq = _listViewFrequent;
            Close();
        }
    }
}

and here is the mainForm

namespace harcaype
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }

        public ListViewItem _listViewFreq
        {
            set
            { listViewNewData.Items.Add(value); }
        }

        private void fileVioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            violationsForm1 vform1 = new violationsForm1(this);
            vform1.ShowDialog();
            vform1.Dispose();
        }

    }
}

I don't see where you are populating the violations listview, so I just through in some dummy data.
It moves data back to the main without any issues...
The only thing I did was change your constructor and parentForm property to be specifc to the mainForm class. If you really intend on using the violations form for multiple form classes, then I suggest you create an interface class, and use that in the constructor and form classes to allow dot-net to address properties without concern for the actual form class it is using.

// Jerry

Thanks! It worked finally!!!! Uhmm.. sorry for the hassle but I have one last question. I was also thinking of only allowing the items to be added once. No repetitions of the added items. Also, is there a way to multi-select items in listview?

One more thing, your solution worked fine with the 2 forms. However, when I declared mainForm parentForm = null; my viewInfoForm.cs produce another error. Supposed to be, this form will also invoke violationsForm1. Here's its code:

namespace MobileTApp
{
    public partial class viewInfoForm : Form
    {
        private mainForm parentForm;
        //mainForm parentForm = null;

        public viewInfoForm(mainForm parentForm)
        {
            InitializeComponent();

            this.parentForm = parentForm;
        }

        private void viewInfoForm_Load(object sender, EventArgs e)
        {
            this.parentForm.Hide();
        }

        private void mnuFreq_Click(object sender, EventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;

            /*violationsForm1 violateform1 = new violationsForm1(this);
            violateform1.ShowDialog();*/
        }

        private void mnuAll_Click(object sender, EventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;

            violationsForm2 violateform2 = new violationsForm2(this);
            violateform2.ShowDialog();
        }

        private void mnuReturn_Click(object sender, EventArgs e)
        {
            this.parentForm.Show();
            this.Close();
        }
    }
}

I get this error from violationsForm1 violateform1 = new violationsForm1(this); and says

The best overloaded method match for 'MobileTApp.violationsForm1.violationsForm1(MobileTApp.mainForm)' has some invalid arguments
Error	2 Argument '1': cannot convert from 'MobileTApp.viewInfoForm' to 'MobileTApp.mainForm'	C:\Documents and Settings\-'rALpHy'-\My Documents\Visual Studio 2005\Projects\MobileTApp\MobileTApp\viewInfoForm.cs	32	64	MobileTApp

This is the only way I know how I can call other forms... I instantiate a new object. And I think that's where I am having problems with. As for your suggestion in creating an interface class, I still have a vague understanding on how to use those, but I'm still studying more about it and hope to deem things eventually. Lol.

This question has already been answered. Start a new discussion instead.