C#里时关闭子窗口时base.Dispose(disposing)报错

tengjige 2010-05-28 09:30:16
C#里时关闭子窗口时base.Dispose(disposing)报错:
Value Dispose() cannot be called while doing CreateHandle().
求各高手帮助解决!!!!
...全文
1162 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tengjige 2010-05-28
  • 打赏
  • 举报
回复
谢了,给分。
tengjige 2010-05-28
  • 打赏
  • 举报
回复
谢谢,我先试一下
skep99 2010-05-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tengjige 的回复:]
我是在子窗口的_FormClosing里Dispose的
在显示前怎么做?我是点击一个BUTTON显示的
private void button1_Click_1(object sender, EventArgs e)
{
Program.buttonid = 1;
Form2 form2 = new Form2();
form2.ShowDialog(this);
……
[/Quote]

在FormClosing里还没执行完,Dispose不掉的.
不用Dispose就可以了,gc帮你回收掉
tengjige 2010-05-28
  • 打赏
  • 举报
回复
我是在子窗口的_FormClosing里Dispose的
在显示前怎么做?我是点击一个BUTTON显示的
private void button1_Click_1(object sender, EventArgs e)
{
Program.buttonid = 1;
Form2 form2 = new Form2();
form2.ShowDialog(this);
}
只在此山中 2010-05-28
  • 打赏
  • 举报
回复
看:
OnLoad或者Form的Constructor里面调用Close或者Dispose是不安全的。
更好的做法是,在窗口显示以前,当发现需要关闭窗口的时候,throw一个特殊的Exception,然后在创建窗口的代码忠try...catch...
skep99 2010-05-28
  • 打赏
  • 举报
回复
楼主在哪里调用关闭的的子窗口?外面try catch下,贴代码
C#.net实现学生成绩管理系统 namespace 学生成绩管理系统 { partial class Formlogin { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.labeluser = new System.Windows.Forms.Label(); this.textBoxuser = new System.Windows.Forms.TextBox(); this.labelcode = new System.Windows.Forms.Label(); this.textBoxcode = new System.Windows.Forms.TextBox(); this.buttonin = new System.Windows.Forms.Button(); this.buttonout = new System.Windows.Forms.Button(); this.SuspendLayout(); // // labeluser // this.labeluser.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.labeluser.Location = new System.Drawing.Point(31, 50); this.labeluser.Name = "labeluser"; this.labeluser.Size = new System.Drawing.Size(55, 26); this.labeluser.TabIndex = 0; this.labeluser.Text = "用户名"; this.labeluser.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // textBoxuser // this.textBoxuser.Location = new System.Drawing.Point(126, 55); this.textBoxuser.Name = "textBoxuser"; this.textBoxuser.Size = new System.Drawing.Size(112, 21); this.textBoxuser.TabIndex = 1; // // labelcode // this.labelcode.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.labelcode.Location = new System.Drawing.Point(31, 124); this.labelcode.Name = "labelcode"; this.labelcode.Size = new System.Drawing.Size(55, 23); this.labelcode.TabIndex = 2; this.labelcode.Text = "密码"; this.labelcode.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // textBoxcode // this.textBoxcode.Location = new System.Drawing.Point(126, 124); this.textBoxcode.Name = "textBoxcode"; this.textBoxcode.PasswordChar = '*'; this.textBoxcode.Size = new System.Drawing.Size(112, 21); this.textBoxcode.TabIndex = 3; // // buttonin // this.buttonin.Location = new System.Drawing.Point(34, 200); this.buttonin.Name = "buttonin"; this.buttonin.Size = new System.Drawing.Size(75, 23); this.buttonin.TabIndex = 4; this.buttonin.Text = "登陆"; this.buttonin.UseVisualStyleBackColor = true; this.buttonin.Click += new System.EventHandler(this.buttonin_Click); // // buttonout // this.buttonout.Location = new System.Drawing.Point(163, 200); this.buttonout.Name = "buttonout"; this.buttonout.Size = new
不使用水晶报表,实现列表式的打印。 刚学C# 1个月写的,如果污染了视听,请见谅。 struct PageProperty { public int PageLeft; public int PageTop; public int PageBottom; public int PageWidth; public int PageHeight; public int RowSpace; public int ColSpace; public int AllVisibleColSpace; public int AllColWidth; public int AllVisibleColWidth; public int AllRowHeight; public int TitleH; public int SubTitleH; public int FooterH; public int[] ColWidth; public int VisibleColWidth; public int PageTextHeight; public int PageTextWidth; } struct TRect { public int Left; public int Top; public int Right; public int Bottom; } class PanelImage : Panel { protected Panel panelShadow = null; public PictureBox ImgData = null; public PanelImage() { this.BackColor = Color.Gray; this.DoubleBuffered = true; ImgData = new PictureBox(); ImgData.Parent = this; ImgData.BackColor = Color.White; panelShadow = new Panel(); panelShadow.Parent = this; panelShadow.BackColor = Color.Black; ResizeControl(); } void ResizeControl() { panelShadow.Left = 8; panelShadow.Top = 8; panelShadow.Width = this.Width - 12; panelShadow.Height = this.Height - 12; ImgData.Left = 4; ImgData.Top = 4; ImgData.Width = this.Width - 12; ImgData.Height = this.Height - 12; } protected override void OnResize(EventArgs eventargs) { ResizeControl(); base.OnResize(eventargs); } protected override void Dispose(bool disposing) { panelShadow.Dispose(); ImgData.Dispose(); base.Dispose(disposing); } } public partial class PrintGrid { [DllImport("gdi32.dll", EntryPoint = "GetDeviceCaps", CallingConvention = CallingConvention.StdCall)] public static extern int GetDeviceCaps(int Handle, int nIndex); private float _leftMargin = 0; private float _topMargin = 0; private float _rightMargin = 0; private float _bottomMargin = 0; const short HORZSIZE = 4; const short VERTSIZE = 6; const short HORZRES = 8; const short VERTRES = 10; const short PHYSICALOFFSETX = 112; const short PHYSICALOFFSETY = 113; int ixLogPixPerInch; int iyLogPixPerInch; int xLogPixPerInch; int yLogPixPerInch; int FPageIndex; int FPageCount; string FLeftPageFooter; string FRightPageFooter; string FTitle; Font FFont; Font FTitleFont; Font FTextFont; Font FFooterFont; Font FSubTitleFont; int FSubTitleOff; int FLeftPageFooterOff; int FPrintCount; string FSubTitle; bool FHLine; bool FVLine; int FPageTop; int FPageLeft; int FPageRight; int FPageBottom; int FToPage; int FFromPage; bool FPrintPageCount; string FRightSubTitle; bool FExistPrinter; bool FCenterShow; Color FHeaderColor; bool FUseHeaderColor; private int iDX; //水平缩放比例 private int iDY; //垂直缩放比例 private int ColCount; //总列数 private int RowCount; //总行数 private int[] ColWidth; //记录每一列的宽度 private int AllColWidth; //所有列的宽度 private int AllVisibleColWidth; //所有可视列的宽度 private int RowSpace; //行间距 private int ColSpace; //所有可视列间距 private int AllVisibleColSpace; //所有行的间距 private int AllColSpace; //所有列的间距 private int AllRowSpace; //所有行的间距 private int RowsOfPage; //每页打的行数 private int PageWidth; //页的宽度 private int PageHeight; //页的高度 private int PageTextHeight; //页正文高度 private int PageTextWidth; //页正文宽度 private int TextTop; //正文上边距 private int TextLeft; //正文左边距 private int TitleH; //标题高度 private int SubTitleH; //标题高度 private int FooterH; //页脚高度 private int FooterTop; //页脚顶部 private int TextH; //正文高度 private int VisibleColWidth; private int DX; private int DY; private string StrTest; private ListView ListViewData = null; //数据 private PrintDocument PD = null;
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using opc_Library; namespace Files { /// /// Form1 的摘要说明。 /// public class Form1 : System.Windows.Forms.Form { write_log write_log = new write_log(); private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1; private System.Windows.Forms.Button button1; private System.Windows.Forms.RichTextBox richTextBox1; private System.IO.FileSystemWatcher fileSystemWatcher1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Label label2; /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.button1 = new System.Windows.Forms.Button(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.fileSystemWatcher1 = new System.IO.FileSystemWatcher(); this.button2 = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(8, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(120, 16); this.label1.TabIndex = 0; this.label1.Text = "文件夹全路径名: "; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(8, 40); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(320, 21); this.textBox1.TabIndex = 1; this.textBox1.Text = "C:\\"; // // button1 // this.button1.Location = new System.Drawing.Point(232, 8); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(96, 23); this.button1.TabIndex = 2; this.button1.Text = "浏览文件夹"; this.button1.Click += new System.EventHandler(this.button1_Click_1); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(8, 88); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(320, 136); this.richTextBox1.TabIndex = 3; this.richTextBox1.Text = ""; // // fileSystemWatcher1 // this.fileSystemWatcher1.EnableRaisingEvents = true; this.fileSystemWatcher1.SynchronizingObject = this; this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Changed); this.fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Created); this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Deleted); this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed); // // button2 // this.button2.Location = new System.Drawing.Point(232, 64); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(96, 23); this.button2.TabIndex = 5; this.button2.Text = "开始监视"; this.button2.Click += new System.EventHandler(this.button2_Click); // // label2 // this.label2.Location = new System.Drawing.Point(8, 72); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(120, 16); this.label2.TabIndex = 4; this.label2.Text = "文件系统变化情况: "; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(336, 230);
数据库的连接,基本的连接方法namespace data { partial class Form1 { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.button1 = new System.Windows.Forms.Button(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.aaDataSet = new data.aaDataSet(); this.aaDataSetBindingSource = new System.Windows.Forms.BindingSource(this.components); this.Column1 = new System.Windows.Forms.DataGridViewButtonColumn(); this.Column2 = new System.Windows.Forms.DataGridViewButtonColumn(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.aaDataSet)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.aaDataSetBindingSource)).BeginInit(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(110, 227); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1, this.Column2}); this.dataGridView1.Location = new System.Drawing.Point(32, 12); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.RowTemplate.Height = 23; this.dataGridView1.Size = new System.Drawing.Size(240, 150); this.dataGridView1.TabIndex = 1; // // aaDataSet // this.aaDataSet.DataSetName = "aaDataSet"; this.aaDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; // // aaDataSetBindingSource // this.aaDataSetBindingSource.DataSource = this.aaDataSet; this.aaDataSetBindingSource.Position = 0; // // Column1 // this.Column1.HeaderText = "Column1"; this.Column1.Name = "Column1"; // // Column2 // this.Column2.HeaderText = "Column2"; this.Column2.Name = "Column2"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 262); this.Controls.Add(this.dataGridView1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.aaDataSet)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.aaDataSetBindingSource)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.DataGridViewButtonColumn Column1; private System.Windows.Forms.DataGridViewButtonColumn Column2; private aaDataSet aaDataSet; private System.Windows.Forms.BindingSource aaDataSetBindingSource; } }

110,537

社区成员

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

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

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