请帮我看看,下面的代码为什么运行总报:“未将对象引用设置到对象的实例”
报错行: Pack1.OpenWindow(typeof(Pack).ToString());
程序代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;namespace login
{
public partial class Form2 : Form
{
Form2 Pack1;
public Form2()
{
InitializeComponent();
} private void 包装ToolStripMenuItem_Click(object sender, EventArgs e)
{
//Form2 Pack = new Form2();
//Pack1 = this.MdiParent as Form2;
Pack1 = new Form2();
Pack1 = this.MdiParent as Form2;
Pack1.OpenWindow(typeof(Pack).ToString());
} /// <summary> /// Open child window /// </summary> /// <param name="ChildTypeString"></param> private void OpenWindow( string ChildTypeString ) { Form myChild = null; if( !ContainMDIChild( ChildTypeString ) ) { // Get current process assembly Assembly assembly = Assembly.GetExecutingAssembly(); // Create data type using type string Type typForm = assembly.GetType( ChildTypeString ); // Create object using type's "InvokeMember" method Object obj = typForm.InvokeMember( null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null ); // Show child form if( obj != null ) { myChild = obj as Form; myChild.MdiParent = this; myChild.Show(); myChild.Focus(); } } } /// <summary> /// Search mdi child form by specific type string /// </summary> /// <param name="ChildTypeString"></param> /// <returns></returns> private bool ContainMDIChild( string ChildTypeString ) { Form myMDIChild = null; foreach(Form f in this.MdiChildren) { if( f.GetType().ToString() == ChildTypeString ) { // found it myMDIChild = f; break; } } // Show the exist form if( myMDIChild != null) { myMDIChild.TopMost = true; myMDIChild.Show(); myMDIChild.Focus(); return true; } else return false; } }
}
报错行: Pack1.OpenWindow(typeof(Pack).ToString());
程序代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;namespace login
{
public partial class Form2 : Form
{
Form2 Pack1;
public Form2()
{
InitializeComponent();
} private void 包装ToolStripMenuItem_Click(object sender, EventArgs e)
{
//Form2 Pack = new Form2();
//Pack1 = this.MdiParent as Form2;
Pack1 = new Form2();
Pack1 = this.MdiParent as Form2;
Pack1.OpenWindow(typeof(Pack).ToString());
} /// <summary> /// Open child window /// </summary> /// <param name="ChildTypeString"></param> private void OpenWindow( string ChildTypeString ) { Form myChild = null; if( !ContainMDIChild( ChildTypeString ) ) { // Get current process assembly Assembly assembly = Assembly.GetExecutingAssembly(); // Create data type using type string Type typForm = assembly.GetType( ChildTypeString ); // Create object using type's "InvokeMember" method Object obj = typForm.InvokeMember( null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null ); // Show child form if( obj != null ) { myChild = obj as Form; myChild.MdiParent = this; myChild.Show(); myChild.Focus(); } } } /// <summary> /// Search mdi child form by specific type string /// </summary> /// <param name="ChildTypeString"></param> /// <returns></returns> private bool ContainMDIChild( string ChildTypeString ) { Form myMDIChild = null; foreach(Form f in this.MdiChildren) { if( f.GetType().ToString() == ChildTypeString ) { // found it myMDIChild = f; break; } } // Show the exist form if( myMDIChild != null) { myMDIChild.TopMost = true; myMDIChild.Show(); myMDIChild.Focus(); return true; } else return false; } }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货