请问各位大侠,如何实现登录界面和主窗体一起出现,并且把用户名传送到主界面?

snksxsyy 2007-11-08 06:02:24
怎么才能实现登录界面和主窗体一起出现并且登录界面在主界面上方,
就好像是打开一个模式对话框似的,
当登录成功后把用户名传送到主界面
各位大哥帮帮忙,我是个新手
...全文
712 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
sww5219999 2007-11-20
  • 打赏
  • 举报
回复
大哥~这是一种模式吧~都弄两个小窗口去模态显示啊?
一般都是后面个大的前面个小的用来登录!~
snksxsyy 2007-11-12
  • 打赏
  • 举报
回复
不过一定要设置

WindowState属性必须设置为Maxinized

吗?有没有其它方法?
snksxsyy 2007-11-12
  • 打赏
  • 举报
回复
谢谢 LL223851

我找到原因了,就是 "主窗体WindowState属性必须设置为Maxinized"  这个问题


snksxsyy 2007-11-10
  • 打赏
  • 举报
回复
在Load中启动Timer

不懂呢,能不能说的具体点大哥?
Wind_Sword 2007-11-10
  • 打赏
  • 举报
回复
哦,确实不行。
那么这样,你用Timer就可以了。在Load中启动Timer,在Timer中先停止Timer。然后显示登陆窗体。
snksxsyy 2007-11-10
  • 打赏
  • 举报
回复
我试了,两个窗体不能同时出现
登录后主窗体才会出来
Wind_Sword 2007-11-10
  • 打赏
  • 举报
回复
登陆窗口用ShowDialog
先打开主窗体,在其Load事件中打开登陆窗体,
这样应该可以啊
snksxsyy 2007-11-10
  • 打赏
  • 举报
回复
我顶起来
snksxsyy 2007-11-10
  • 打赏
  • 举报
回复
登录窗口用ShowDialog() 还是不能把两个窗体一起显示

只能把登录窗体先显示出来,登录成功后,主窗体才会出现

我晕了
sww5219999 2007-11-10
  • 打赏
  • 举报
回复
我也想弄个你那样的窗体可以一直没成功,因为我也是菜鸟就先给你点建议!
首先主窗口启动的时候(有个LOAD事件),你把那个登录窗口用ShowDialog()方法显示,这里声明:登录窗口并不是主窗口的子窗体,否则编译不过去!
其次,你要想办法让登录窗口关闭后让主窗口也自动关闭,这个办法我还没试,你可以用Application.Exit();方法试下!
最后,登录成功后隐藏登录窗口就可以了!
以上是我的思路!!
因为我们也有这个作业哈哈就顺便告诉你下,我先启动的登录窗口,成功后才显示主窗口,这样麻烦就少了很多哈哈~如果需要我可以把工程发给你嘎嘎
snksxsyy 2007-11-10
  • 打赏
  • 举报
回复
大哥还是不行啊

我是想把登录界面放在主窗体上方,并不是它的子窗体,
登录窗体可以移动到任何地方,登录成功后主窗体才可以获取焦点,并且传值.
cate12 2007-11-10
  • 打赏
  • 举报
回复
因為不知道如何像2樓那樣貼代碼,就截取部份代碼.

主窗口關鍵代碼:
private void MainForm_Load(object sender, System.EventArgs e)
{
LoginForm lf = new LoginForm(this);
lf.MdiParent = this;
lf.StartPosition = FormStartPosition.CenterScreen;/登錄窗口居中
lf.Show();
}

子窗口重構構造函數:
private MainForm mf;
public LoginForm(MainForm MF)
{
this.mf = MF;
}
snksxsyy 2007-11-10
  • 打赏
  • 举报
回复
snksxsyy 2007-11-10
  • 打赏
  • 举报
回复
我顶起来
LL223851 2007-11-10
  • 打赏
  • 举报
回复
我再帖,呵呵,弄了几次发现原来很简单,非常简单
FormLogin.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SnKsxs
{
public partial class FormLogin : Form
{
public FormLogin()
{
InitializeComponent();
}

private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text=="aaa"&txtPassword.Text=="bbb")//举例的,没有使用数据库
{
ValueClass.LoginUserName = txtUserName.Text;
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("用户名或密码错误!", "出错啦~~", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtPassword.Focus();
return;
}
return;
}

private void btnCancel_Click(object sender, EventArgs e)
{
Application.ExitThread();
}
}
}



FormMain.cs//主窗体,注意WindowState属性必须设置为Maxinized,Lable1.Text设置为空,否则影响显示效果
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SnKsxs
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
FormLogin fr = new FormLogin();
fr.ShowDialog();
label1.Text = ValueClass.LoginUserName;
}
}
}


Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SnKsxs
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
}



ValueClass.cs//传值
using System;
using System.Collections.Generic;
using System.Text;

namespace SnKsxs
{
class ValueClass
{
public static string LoginUserName = string.Empty;
}
}



sww5219999 2007-11-10
  • 打赏
  • 举报
回复
你的Form1是启动窗体?别告诉我你的启动窗体是Login,看看设置对了吗?

我的就是一起显示的啊!你的QQ号,不行我发给你看看。我的是120797720 跑


snksxsyy 2007-11-10
  • 打赏
  • 举报
回复
我就是用主窗体为启动窗体的,这个我也写了

private void Form1_Load(object sender, EventArgs e)
{
Login obj = new Login();
obj.ShowDialog();
}


但是还是先显示登录窗体,关闭登录窗体后主窗体才会显示.

你的可以一起显示?我试了不行啊
sww5219999 2007-11-10
  • 打赏
  • 举报
回复
上面的代码把那个 Form1的激活事件 Form1_Activated改成LOAD吧,代码如下:
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
frmLogin aa = new frmLogin();
aa.ShowDialog();
}


再就是非要一块显示?这样漏洞很多的!一个一个显示多安全!那样的是我的作业哈哈,已经解决~
sww5219999 2007-11-10
  • 打赏
  • 举报
回复
我的意思是让主窗体为启动窗体!!然后再主窗体的LOAD事件里面打上
frmLogin aa = new frmLogin();// frmLogin是登陆窗体的名字!
aa.ShowDialog();
这样2个窗体就可以一块显示了!!!

我一直在这做!感觉基本可以实现!关键是以点登录框上的叉号竟然主窗体还在!这样不是就不用登陆了?
郁闷!以下为我的代码
主窗体form1:
private void Form1_Activated(object sender, EventArgs e)
{
frmLogin aa = new frmLogin();
aa.ShowDialog();
}
登录窗体frmLogin:
public frmLogin()
{
InitializeComponent();
}

private void btnQuit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void btnLogin_Click(object sender, EventArgs e)
{

if (txtUsername.Text.ToLower() == "niit" && txtPassword.Text == "123")
{
Close();

}

else
{
MessageBox.Show("输入的用户名或密码错误","数据错误",MessageBoxButtons.OK,
MessageBoxIcon.Warning);
txtPassword.Text = "";
txtPassword.Focus();
}

}
Program:
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());//这里主窗体时启动窗体!!!!!!!!!!
}
}
snksxsyy 2007-11-09
  • 打赏
  • 举报
回复
各位大哥帮忙啊
加载更多回复(14)

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧