比如说当前窗口是Form1,上面设置一个按钮button1
怎么写click事件跳转到以建立的form2
(新手,刚起步,请大家指点)
我这样写的 为什么不起作用
Form2 frm2=new Form2();
frm2.Show();
试试这个
Form Frm = new Form();
Frm.showDialog(this);
/*
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace feng
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuCut;
private System.Windows.Forms.MenuItem menuHelp;
private System.Windows.Forms.MenuItem menuFile;
private System.Windows.Forms.MenuItem menuExit;
private System.Windows.Forms.MenuItem menuLoad;
private System.Windows.Forms.MenuItem menuSave;
private System.Windows.Forms.MenuItem menuPast;
private System.Windows.Forms.MenuItem menuCopy;
private System.Windows.Forms.MenuItem menuAbout;
private System.Windows.Forms.MenuItem menuEditor;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.menuEditor = new System.Windows.Forms.MenuItem();
this.menuAbout = new System.Windows.Forms.MenuItem();
this.menuCopy = new System.Windows.Forms.MenuItem();
this.menuPast = new System.Windows.Forms.MenuItem();
this.menuSave = new System.Windows.Forms.MenuItem();
this.menuLoad = new System.Windows.Forms.MenuItem();
this.menuExit = new System.Windows.Forms.MenuItem();
this.menuFile = new System.Windows.Forms.MenuItem();
this.menuHelp = new System.Windows.Forms.MenuItem();
this.menuCut = new System.Windows.Forms.MenuItem();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
//
// menuEditor
//
this.menuEditor.Index = 2;
this.menuEditor.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuCut,
this.menuCopy,
this.menuPast});
this.menuEditor.Text = "Editor(E)";
//
// menuAbout
//
this.menuAbout.Index = 0;
this.menuAbout.Text = "About(A)";
this.menuAbout.Click += new System.EventHandler(this.MenuAboutClick);
//
// menuCopy
//
this.menuCopy.Index = 1;
this.menuCopy.Text = "Copy(O)";
//
// menuPast
//
this.menuPast.Index = 2;
this.menuPast.Text = "Past(P)";
//
// menuSave
//
this.menuSave.Index = 2;
this.menuSave.Text = "Save(S)";
//
// menuLoad
//
this.menuLoad.Index = 0;
this.menuLoad.Text = "Load(L)";
this.menuLoad.Click += new System.EventHandler(this.MenuLoadClick);
//
// menuExit
//
this.menuExit.Index = 1;
this.menuExit.Text = "Exit(E)";
this.menuExit.Click += new System.EventHandler(this.MenuExitClick);
//
// menuFile
//
this.menuFile.Index = 0;
this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuLoad,
this.menuExit,
this.menuSave});
this.menuFile.Text = "File(F)";
//
// menuHelp
//
this.menuHelp.Index = 1;
this.menuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuAbout});
this.menuHelp.Text = "Help(H)";
//
// menuCut
//
this.menuCut.Index = 0;
this.menuCut.Text = "Cut(C)";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFile,
this.menuHelp,
this.menuEditor});
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(456, 365);
this.Menu = this.mainMenu1;
this.Name = "MainForm";
this.Text = "MainForm";
}
#endregion
void MenuExitClick(object sender, System.EventArgs e)
{
Application.Exit();
}
void MenuLoadClick(object sender, System.EventArgs e)
{
OpenFileDialog dlgOpenFile=new OpenFileDialog();
dlgOpenFile.Title="open";
dlgOpenFile.Filter="open(*.txt)|*.txt|All Files(*.*)|*.*";
if(dlgOpenFile.ShowDialog()==DialogResult.OK)
{
string fileName=dlgOpenFile.FileName;
FileStream aFile=new FileStream(fileName,FileMode.Open);
StreamReader sr=new StreamReader(aFile);
this.Text=fileName;
sr.Close();
this.menuSave.Enabled=false;
}
}
void MenuAboutClick(object sender, System.EventArgs e)
{
Form1 frm2=new Form1();
frm2.Show();
}
}
}
/*
*/
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DefaultNamespace
{
/// <summary>
/// Description of Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
public Form1()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 96);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "hello!! xuefeng";
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 224);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "确定";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "About";
this.Load += new System.EventHandler(this.Form1Load);
this.ResumeLayout(false);
}
#endregion
void Form1Load(object sender, System.EventArgs e)
{
}
}
}
你必须保证这个窗体及代码同时在这个项目文件夹下,
Form2 fm=new Form();
fm.Owner=this;
fm.Showdialog();