Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have form1 and form 2 .. and listview1 on form1 , listview2 on form2 when listview1 in form1 and listview2 in form2 and both listview contain : itemcode - itemcount . and button1 in form2

on form2 if user click button1 values in listview2 on form2 be populated in listview1 on form1 and form2 close how to do such action in code thanks
Posted
Comments
BillWoodruff 17-Oct-15 22:39pm    
If this is Windows Forms, what is the main form in this application: form1 ? Does form1 create form2 ?

Once the items in the ComboBox on form2 replace the ComboBox items on form1, do you want the two ComboBoxes to be synchronized where they show the same selected item ?

Modify your Program.cs as below

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace SampleApplicationLV
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 Form1Obj = new Form1();
            Application.Run(new Form2(ref Form1Obj));

            Application.Run(Form1Obj);
        }
    }
}


Then Make the Modifier property of Listview1 of Form1 as Public from private

Form2.cs sample code

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleApplicationLV
{
    
    public partial class Form2 : Form
    {
        Form1 objForm1;
        public Form2(ref Form1 Form1Obj)
        {
            InitializeComponent();

            objForm1 = new Form1();

            objForm1 = Form1Obj;
        }

       

        private void Form2_Load(object sender, EventArgs e)
        {
            listView1.View = View.Details;

            listView1.Columns.Add("Item Code");
            listView1.Columns.Add("Item Count");

            for (int i = 1; i <= 5; i++)
            {
                string[] row = { "Item"+i.ToString(), i.ToString() };
                var listViewItem = new ListViewItem(row);
                listView1.Items.Add(listViewItem);              
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Make listview1 of Form1 Modifiers from Private to Public in Properties
            objForm1.listView1.View = View.Details;
            objForm1.listView1.Columns.Add("Item Code");
            objForm1.listView1.Columns.Add("Item Count");

            foreach (ListViewItem item in listView1.Items)
            {
                objForm1.listView1.Items.Add((ListViewItem)item.Clone());
            }           

            this.Close();
        }
    }
}
 
Share this answer
 
v3
Comments
BillWoodruff 18-Oct-15 3:45am    
until the OP clarifies what they are doing and answers the question i asked them, writing code like this is a waste of time.
Ahmed Zoeil 18-Oct-15 13:36pm    
thanks sreeyush sudhakaran it worked
sreeyush sudhakaran 19-Oct-15 1:03am    
You are welcome , thanks for new Information :)
Pass the listview1 from Form1 as a parameter to the Form2. Then access it in Form2.
Something like this,
C#
// on Form1 any button click or other event to open the form2
new Form2(listView1).Show();

// on Form2 
public Form2(ListView lv)
{
    InitializeComponents();
    listView2 =lv;
}


-KR
 
Share this answer
 
Comments
sreeyush sudhakaran 18-Oct-15 2:50am    
Will this work if Form1 close itself inside it's button click
Krunal Rohit 18-Oct-15 4:42am    
Didn't get you.

-KR
sreeyush sudhakaran 18-Oct-15 6:45am    
It seems your idea doesnot work if this.close() is called after new Form2(listView1).Show(); , I have tried but not worked for me
Krunal Rohit 18-Oct-15 13:27pm    
Don't close Form1. Hide it.

-KR
sreeyush sudhakaran 19-Oct-15 1:05am    
For hiding I think there is no need to do like above just create a Form2 object,equate list and call it...Am I right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900