-1

I am having a problem adding text from my Form 2, to the List item in Form 1. Could anyone help please, Sorry if it's a bit jumbled, I'm new with coding :) What I am trying to achieve is a to-do list application, in the 2nd form, text should be entered in a text box - when the user clicks enter, this text should be entered into the list item.

Another question is, in this case would I be better using a list item or a list box?

Here is the code: FORM 2

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ToDoList
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public void button2_Click(object sender, EventArgs e, string tbtext)
        {
            //Form1 form1 = new Form1();
            //form1.listBox1.Items.Add(tbtext);
        }

        public void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {


        }

        private void button2_Click(object sender, EventArgs e)
        {
            ListViewItem item = new ListViewItem(textBox1.Text);
            Form1 f1 = new Form1();
            f1.listView1.Items.Add(item);
        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }
    }
}

FORM 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ToDoList
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void filesToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {



        }

        public void button2_Click(object sender, EventArgs e)
        {


        }

        public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void F1Button1_Click(object sender, EventArgs e)
        {

        }

        private void tB1_TextChanged(object sender, EventArgs e)
        {

        }

        public void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }
    }
}
Yrneh
  • 63
  • 3
  • 9
  • 1
    What have you actually tried? The code makes it seem like you haven't tried anything at all and just expect us to "fill in the blanks" for you. – Anthony Mar 26 '15 at 20:10

1 Answers1

-1

You create different object Form1, and the object is destroyd after you quit button2_Click. Is it local variable.

You need to create the datasource for your TODO list and propaget it to both forms, for example by method Set DataSource. Then you have one object accesible by both forms.

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • I'm really new to windows forms so what is a datasource and how do I make one? Thanks for your help – Yrneh Mar 26 '15 at 20:38
  • DataSource is the object which keeps your TODO list. For example List todoListDatasource = new List. – Tomas Kubes Mar 26 '15 at 20:43