我用from1 作为登录窗口 ,点击登录后打开from2 这时我想关闭from1, 该在什么地方使用close
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 丽控业务管理系统
{
public partial class frmlogin : Form
{
public static string struserid;
public static string strusermc;
public frmlogin()
{
InitializeComponent();
}
private void btnlogin_Click(object sender, EventArgs e)
{
string soure = "database=lkdata;server=127.0.0.1;user id=sa;password=";
string sqlqxdz = "select qxdm from tb_qxdz where yhdm='" + txtbyhdm.Text.ToString().Trim() + "'";
string sqlyh = "select * from tb_yh where yhdm='" + txtbyhdm.Text.ToString().Trim() + "' and yhkl='" + txtbyhkl.Text.ToString().Trim() + "'";
SqlConnection mycon = new SqlConnection(soure);
SqlDataAdapter da = new SqlDataAdapter(sqlyh, mycon);
SqlDataAdapter da1 = new SqlDataAdapter(sqlqxdz, mycon);
DataSet ds = new DataSet();
da.Fill(ds, "tb_yh");
da1.Fill(ds, "tb_qxdz");
if (txtbyhdm.Text.ToString().Trim() == null)
{ MessageBox.Show("用户代码为空"); }
if (txtbyhkl.Text.ToString().Trim() == null)
{ MessageBox.Show("用户口令为空"); }
if (ds.Tables[0].Rows.Count >= 0)
{
struserid = ds.Tables[0].Rows[0]["yhdm"].ToString().Trim();
strusermc = ds.Tables[0].Rows[0]["yhmc"].ToString().Trim();
frmmain frm_main = new frmmain();
frm_main.Show();
for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
{
switch (ds.Tables[1].Rows[i]["qxdm"].ToString().Trim())
{
case "01":
frm_main.tlmxtgl.Enabled = true;
frm_main.tlmxtgl.Visible = true;
break;
case "0101":
frm_main.tlmyhgl.Enabled = true;
frm_main.tlmyhgl.Visible = true;
break;
case "0102":
frm_main.tlmqxgl.Enabled = true;
frm_main.tlmqxgl.Visible = true;
break;
case "0103":
frm_main.tlmxgkl.Enabled = true;
frm_main.tlmxgkl.Visible = true;
break;
case "0104":
frm_main.tlmtc.Enabled = true;
frm_main.tlmtc.Visible = true;
break;
}
}
}
else
{ MessageBox.Show(" 用户代码或用户口令错误"); }
}
private void frmlogin_Load(object sender, EventArgs e)
{
}
private void btncancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
[解决办法]
this.Hide();
frmmain frm_main = new frmmain();
frm_main.Show();
this.Close();
这样用就行了
[解决办法]
作为第一个画面的Form1不能关闭(因为消息循环建立在此画面上),否则程序就会退出,Form1可以隐藏。
this.Hide();不能Close();
点击退出的时候可以关闭。
[解决办法]
如果你想关闭FORM2的时候,再回到FORM1, 可以这样
事件(){this.hide();Form2 form2 = new Form2();if(DialogResult.OK==form.ShowDialog()){ this.show();}}你要在FORM2里的FORMCLOSED事件加一个this.dialogresult = dialogresult.ok
[解决办法]
你可以在打开form1之前就先调用form2,form2关闭时调用form1
[解决办法]
一个简单的Login窗体,其它的如限制登录次数或用户信息传递等功能自己在frmLogin.cs中扩展
Program.cs
static void Main(){ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); frmLogin myLogin = new frmLogin(); if (myLogin.ShowDialog() == DialogResult.OK) { Application.Run(new frmMain()); } //else //{ // MessageBox.Show("登录失败!"); //}}