Winform问题

hahahoo 2003-01-30 09:17:22
两个winform,在一个form里有一个Main()里面用Application.Run(new Form2())首先显示另外一个form,编译后运行总是显示
An exception 'System.NullReferenceException' has occurred in ApplicaitonBrowser.exe.
为什么?
下面是两个form的代码,没有放任何其他的功能
Browser.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;

namespace ApplicationBrowser
{
/// <summary>
/// Summary description for Browser.
/// </summary>
public class Browser : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Browser()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

[STAThread]
static void Main()
{
Application.Run(new Login());
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Browser";
}
#endregion
}
}

Login.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace ApplicationBrowser
{
/// <summary>
/// Summary description for Login.
/// </summary>
public class Login : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panelBottom;
private System.Windows.Forms.Button buttonOk;
private System.Windows.Forms.Button buttonCancel;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Login()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panelBottom = new System.Windows.Forms.Panel();
this.buttonOk = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.panelBottom.SuspendLayout();
this.SuspendLayout();
//
// panelBottom
//
this.panelBottom.BackColor = System.Drawing.Color.Gray;
this.panelBottom.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonCancel,
this.buttonOk});
this.panelBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panelBottom.Location = new System.Drawing.Point(0, 226);
this.panelBottom.Name = "panelBottom";
this.panelBottom.Size = new System.Drawing.Size(408, 40);
this.panelBottom.TabIndex = 0;
//
// buttonOk
//
this.buttonOk.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonOk.Location = new System.Drawing.Point(240, 8);
this.buttonOk.Name = "buttonOk";
this.buttonOk.TabIndex = 0;
this.buttonOk.Text = "确定";
this.buttonOk.Click += new System.EventHandler(this.buttonOk_Click);
//
// buttonCancel
//
this.buttonCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonCancel.Location = new System.Drawing.Point(320, 8);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.TabIndex = 1;
this.buttonCancel.Text = "取消";
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// Login
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(408, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panelBottom});
this.Name = "Login";
this.Text = "Login";
this.panelBottom.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

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

private void buttonOk_Click(object sender, System.EventArgs e)
{
}
}
}
...全文
22 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hahahoo 2003-02-17
  • 打赏
  • 举报
回复
问题找到了,是我的机器有问题,重装后就好了。framework好像不太稳定,经常有这些那些的问题,以前用asp.net的时候也遇到过framework的问题
hahahoo 2003-02-02
  • 打赏
  • 举报
回复
楼上的你说的是什么?
甴曱 2003-02-02
  • 打赏
  • 举报
回复
在你的Login.cs中你可以把namespace ApplicationBrowser
改为namespace ApplicationBrowser:ApplicationBrowser
另一个是你在产生login.cs时,可以在ApplicationBrowser中点击“项目”--“添加Windows窗体”并改名为login.cs
runrunrun 2003-02-02
  • 打赏
  • 举报
回复
那可以把login窗口作为主窗口,但验证完后不能退出,而要隐藏自己。用 login.hide()
hahahoo 2003-02-02
  • 打赏
  • 举报
回复
问题不在这里,我的browser是要开多个的,就像internet explorer一样,不可能开一个browser就login一下的
anggogo 2003-02-01
  • 打赏
  • 举报
回复
不错

只有 main 里面的东西是主窗口,你创建了它后,先把自己隐藏起来,然后 frmLogin.ShowDialog 出来,如果通过验证,关掉 login, 再把自己显示出来;否则就直接关掉整个程序
runrunrun 2003-02-01
  • 打赏
  • 举报
回复
同意 kals(Big big world), 最好用模态窗体。
Application.Run(new Form2())后, Form2即为主窗体,一但该窗口退出,
整个程序即出。
poetc 2003-02-01
  • 打赏
  • 举报
回复
gz

up
hahahoo 2003-01-31
  • 打赏
  • 举报
回复
http://www.redfocus.net/forums/dispbbs.asp?boardID=6&ID=424
你这贴子不能读了,问一下如何在load事件中配置,谢谢?
kals 2003-01-31
  • 打赏
  • 举报
回复
无论是在VS.NET的Debug中还是直接双击编译后的文件,我试过都没有问题。
不过我做登陆窗体一般都不是这样做,而是把校验窗体作为一个模态窗体弹出
yarshray 2003-01-30
  • 打赏
  • 举报
回复
你的启动窗体,是Login,在程序一开始new,分配了运行空间

可是,你的主窗体,没有,因此你主窗体的load事件中,没有配置

主窗体,因此抛出异常,没有将对象引用,引用到对象实例.

关于,登陆窗体,搜索,论坛,以前的帖子,我记得以前答过.

titicaca 2003-01-30
  • 打赏
  • 举报
回复
debug后运行没有问题?
hahahoo 2003-01-30
  • 打赏
  • 举报
回复
还有在VS.NET中debug好像没有问题,直接双击运行编译后的exe文件就会有这个问题

110,533

社区成员

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

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

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