Hello Everyone,

I have two form. First is main form which is display the data. Second is a another form which users input the values to the textboxes. I would like to make that When a user type something in textboxes in second form, these data will be shown in listView in the mainForm. How I can make this?

Cheers,

Yobot

Recommended Answers

All 5 Replies

Here's a way of doing it. It involves creating a public function to update the list data in your main form, and creating a constructor overload in the second form to accept a parameter of the type of the main form. Then on a button click event on the second form, call the public function of the main form.

Form1.cs:

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

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

        public void UpdateListBox(string data)
        {
            listBox1.Items.Add(data);
        }
    }

Form2.cs

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

        public Form2(Form1 parentForm1): this()
        {
            this.ParentForm1 = parentForm1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != string.Empty && this.ParentForm1 != null)
            {
                this.ParentForm1.UpdateListBox(textBox1.Text);
                textBox1.Text = string.Empty;
                textBox1.Focus();
            }
        }

        private Form1 ParentForm1 { get; set; }
    }

Thanks for your response,

When I compile the above code, I got an error message like that "must declare a body because it is not marked abstract or extern " for set; and get; blocks. How can I fix it?

Cheers

Thanks for your response,

When I compile the above code, I got an error message like that "must declare a body because it is not marked abstract or extern " for set; and get; blocks. How can I fix it?

Cheers

It doesn't sound like you are on a version of C# prior to 2008. (C# added auto-implemented properties starting with VS 2008.) Simply rewrite the ParentForm1 property something like the following.

private Form1 _parentForm1  = null;
private Form1 ParentForm1
{
    get { return _parentForm1; }
    set { _parentForm1 = value; }
}

But I would also encourage you to upgrade to the most recent version of C# and .NET. Microsoft makes Express versions of the software for free, so you can visit here to try the products.

And the Visual Studio 2010 official release is next month, so you don't want to get too far behind!

i need to know if i can retrieve data from listview in form2 and put it in another listview in form1
i'm new in c# >>please help me

Hi ebrahemzregat, welcome at DaniWeb!
Your question is explained in this thread, who is not been marked solved, but it should have!
If you have further questions, please post them in a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.