Show a new form when click a button

Viewed 110581

I am new in c#. I am trying to show a new form (form2) when click button in form1.

this is my code.

        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 SliceEngine
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

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

the error show

the type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?)

this is my code for form2.

    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 SliceEngine
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

for form2, i just making the design interface.

all i know when using java, i only need to declare the object first. what should i do for this?

9 Answers

My solution:

In click event of Form1 include your button:

string foobar = "Hello world";
Form2 frm2 = new Form2(foobar);
frm2.ShowDialog();

In Form2:

public Form2(string foobar)
{
    InitializeComponent();
    textbox1.Text = foobar;
}

For anybody still looking for an answer:

At the top of your code, add this namespace:

using YourProjectName;

Then, when you wish to show your form, type this:

var form = YourProjectName.YourFormName();
form.Show(); // Show form using new variable
Related