Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c# - How can I navigate between forms

I am a newbiest in c# and window form i am doing a project and i meet some problem

  1. how can i navigate forms within the window( i have a menu strip, when click it will show a item "Brand", so when i click it, it should open up within the window , i don't want something using the mdiparent/container, i have form1 and form2, then i put the menu strip in form1, which there is some thing inside form1, if use the mdiparent/container, the form1 content/thing will block the form2 )

2.i use the below code and the problem is i want to close the form1 which i click on " Brand" in the menu strip...but how???

public partial class Form1 : Form
{
    //  i put the menu strip in form1 design
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void Check_Click(object sender, EventArgs e)
    {
        Form2 Check = new Form2();
        Check.Show();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot just close the Form1 as it is the main form, but you can hide it. Use this.Hide().

private void Check_Click(object sender, EventArgs e)
{
    Form2 Check= new Form2();
    Check.Show();
    Hide();
}

[EDIT]

Not sure if this is what is asked. But...

There are many ways to implement navigation between forms, for example:

In Form1:

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

In Form2:

private void button1_Click(object sender, EventArgs e)
{
    var form1 = (Form1)Tag;
    form1.Show();
    Close();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...