如何将windows窗体作为参数传递给任何其他类函数?使用c# [英] How to pass windows form as parameter to any another class function? using c#

查看:80
本文介绍了如何将windows窗体作为参数传递给任何其他类函数?使用c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为toolbar的用户控件和一个名为infocore的类文件。用户控件名称的工具栏包含添加,保存和退出按钮。在infocore类中编写的添加,保存和退出按钮的代码。用save(Form f,EventArgs e)函数写的保存按钮代码。



然后在用户控件中创建infocore类的对象并访问像objInfocore这样的save()函数。 save(this.FindForm(),e);



当我在表单中添加这些用户控件并运行应用程序时,我在program.cs Application.Run中得到了异常(new Form1())对象引用未设置对象的实例。那么我应该在Application.Run中传递参数(新Form1(这里参数是必需的?))

I had created one user control named as toolbar and one class file named as infocore. The toolbar which is name of user control contain add, save and exit button. The code of add,save and exit button written in infocore class. the save button code written in save(Form f,EventArgs e) function.

Then create object of infocore class in user control and access save() function like objInfocore.save(this.FindForm(),e);

When i add these user control in form and run appliction then i got the exception in program.cs Application.Run(new Form1()) Object reference not set an instance of an object. so what should i pass parameter in Application.Run(new Form1("here Parameter is required?"))

推荐答案

这不是问题所以请假好吧,如果你的Form1类构造函数需要一个参数,那么编译器就会抱怨。 对象引用未设置对象的实例是运行时错误,而不是编译时间,因此不是Application.Run就是问题。



究竟是什么问题不是我可以准确的 - 我无法访问您的代码。所以,你必须开始寻找自己。



首先要做的是找出问题发生的地方。既然你在谈论Application.Run,​​我认为当你执行那一行时会出现异常 - 这意味着错误是在构造函数的某个地方,并且调试器无法准确地解决问题。因此,首先捕获所有异常:在Debug,Exceptions ...下的VS菜单栏上查看,并在Thrown和User-unhandled下的每个复选框中打勾。按OK



现在在调试器中运行你的应用程序,它应该在它遇到异常时停止,即使某些代码正在捕获并重新抛出它。

查看抛出异常的行中的变量应该告诉你哪一个是null,这至少会为你提供更多信息。
That isn''t the problem, so leave that well alone - if your Form1 class constructor required a parameter then the compiler would have complained. "Object reference not set an instance of an object" is a run time error, not compile time, so it is not the Application.Run that is the problem.

Exactly what is the problem is not something I can be precise about - I don''t have access to your code. So, you will have to start looking for yourself.

The first thing to do is to find out where the problem is occurring. Since you are talking about Application.Run, I assume that the exception comes when you execute that line - which means that the error is in the constructor somewhere, and the debugger can''t work out exactly where. So start off by trapping all exceptions: Look on your VS menu bar under "Debug", "Exceptions..." and put a tick en every check box under "Thrown" and "User-unhandled". Press OK

Now run your app in the debugger and it should stop when it hits the exception, even if some code is catching and re-throwing it.
Looking at the variables in the line throwing the exception should tell you which one is null, and that will at least give you more info as to why it occurred.


这篇关于如何将windows窗体作为参数传递给任何其他类函数?使用c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆