views:

21

answers:

3

I am wondering how I can update my listview in form1 by entering data via textboxes in form2. My code works fine if i put all the text boxes on the same form etc.

I figured I needed some reference to the first form on 2nd but can't get it working.

Any tips for putting me in the right direction would be nice, also any tips for any better way of doing this.

This is the code I have so far:

Form1:

public partial class form1 : Form
    {
        public form1()
        {
            InitializeComponent();
        }

        public ListView MyListView
        {
            get
            {
                return taskList;
            }
        }

Form2:

public partial class form2 : Form
    {
        public form2()
        {
            InitializeComponent();
        }

        form1 f;

        public add(form1 f)
        {
            this.f = f;
        }        

        public void AddToList()
        {
                ListViewItem item1 = new ListViewItem(txtName.Text);
                item1.SubItems.Add(txtEmail.Text);
                item1.SubItems.Add(txtPhone.Text);
                f.MyListView.Items.AddRange(new ListViewItem[] { item1 });
        }
A: 

the listview control should be private, instead add a public method to your form that contains the listview control, which receives the data you want to insert and inserts it into the listview.

IceCoder
A: 

If form2 is not created by and displayed by form1, you're not going to have a reference to call. In that case, things are going to get a bit more interesting from a communication standpoint. When that happens, you'll need to use an eventing model to get the information from one place to another.

Robaticus
+2  A: 

The most straight forward way of doing things would be to use events. You could add an event on form2 that would fire each time an item is added, and includes the text to be inserted (you have multiple pieces of information, so a custom data type would be appropriate). You can then add a handler method to form2 which adds the item to its ListView. You then tie the two together in the code that is creating the two forms, and life should be good.

So, to provide some code, First up is the data structure for the event:

public delegate void HandleItemAdded(object sender, ItemAddedEventArgs e);    

public struct ItemAddedEventArgs : EventArgs
{
    public string Name;
    public string Email;
    public string Phone;

    public ItemAddedEventArgs(string name, string email, string phone)
    {
        Name = name;
        Email = email;
        Phone = phone;
    }
}

Then we have the event code on form2

public event HandleItemAdded ItemAdded;
// .. some other stuff
public void RaiseItemAdded(ItemAddedEventArgs e)
{
    if(ItemAdded != null)
        ItemAdded(this,e);
}

// ... now for your AddToList
public void AddToList()
{

    RaiseItemAdded(new ItemAddedEventArgs(txtName.Text,txtEmail.Text, txtPhone.Text);
}

And now we can add a handler in form1

public void HandleItemAdded(object sender, ItemAddedEventArgs e)
{
    ListViewItem item1 = new ListViewItem(txtName.Text);
    item1.SubItems.Add(txtEmail.Text);
    item1.SubItems.Add(txtPhone.Text);
    MyListView.Add(item1);
}

And last but not least we need to tie them together

//...not sure what your code looks like, but we'll assume we have instances of the two forms named form1Form and form2Form
form2Form.ItemAdded += form1Form.HandleItemAdded
ckramer
Been spending a few hours reading and poking around with events with my C# book, just to figure out how it all works. This was just what I needed, thanks.
Zubirg