登录表单,其中包含“connect”按钮 带有“断开连接”按钮的主要表单。
每次点击登录表单中的“连接”,我都要转到主表单。
每次点击“断开连接”,我想返回登录表单。
以下是按钮的代码:
private void buttonConnect_Click(object sender, EventArgs e)
{
this.Hide();
ShowMessage("Successfully connected.");
Form MainForm = new MainForm();
MainForm.Show();
}
private void buttonDisconnect_Click(object sender, EventArgs e)
{
this.AddOwnedForm(LoginForm);
LoginForm.Show();
LoginForm.BringToFront();
this.Close();
}
请注意我的代码中的第一行:
this.Hide();
Login.Show();
Login.BringToFront();
任何帮助表示赞赏......浪费了将近一整天的时间来试图解决这个问题。
答案 0 :(得分:1)
试试这个
private void buttonDisconnect_Click(object sender, EventArgs e)
{
LoginForm frmLogin= (LoginForm)Application.OpenForms["LoginForm"];
if (frmLogin != null)
{
//frmLogin.Visible = true; Uncomment this line if form still not get visible.
frmLogin.Show();
}
else
{
frmLogin = new LoginForm();
frmLogin.Show();
}
this.Close();
}
你的连接代码应该是这样的
private void buttonConnect_Click(object sender, EventArgs e)
{
this.Hide();
ShowMessage("Successfully connected.");
MainForm frmMain = new MainForm();
frmMain.Show();
}
答案 1 :(得分:0)
在登录表单中,订阅MainForm.FormClosed
事件:
this.Hide();
ShowMessage("Successfully connected.");
Form MainForm = new MainForm();
MainForm.FormClosed += (s, args) => this.Show();
MainForm.Show();
答案 2 :(得分:0)
试试这个。
Class MainForm
{
LoginForm login_form;
public MainForm()
{
}
public MainForm(LoginForm log)
{
login_form = log;
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void buttonConnect_Click(object sender, EventArgs e)
{
this.Hide();
ShowMessage("Successfully connected.");
if(login_form == NULL)
{
login_form = new LoginForm(this);
}
login_form.Show();
}
}
Class LoginForm
{
MainForm main_form;
public LoginForm()
{
}
public LoginForm(MainForm man)
{
main_form = man;
}
private void LoginForm_Load(object sender, EventArgs e)
{
}
private void buttonDisConnect_Click(object sender, EventArgs e)
{
this.Hide();
ShowMessage("Successfully Disconnected.");
if(main_form == NULL)
{
main_form = new MainForm(this);
}
main_form.Show();;
}
}