如何在C#中获取表单文件名 [英] How do I get the form file name in C#

查看:71
本文介绍了如何在C#中获取表单文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在C#中找到活动表单文件名。



我尝试过:



Form form = Form.ActiveForm;

string FormName = form.Name.ToString();



结果:对象引用未设置为对象的实例。

解决方案

这可能取决于您何时执行此操作。

如果您在表单构造函数中执行此操作,然后您可能会遇到问题。

在Load事件中,您将获得一个空白名称。

在显示事件或更高版本中,你会得到正确的表格名称。



但是......我不能在任何一个中得到空引用异常!

因此,检查调试器中的代码,看看究竟发生了什么以及错误发生的位置 - =在触发异常时查找行中的空值。


如果您尝试要在当前(First Form)的Form load中调用该代码,您将获得Null Va lue。



我试图获得活动表格名称。

创建两个表单Form1和Form2,每个表单中都有一个按钮来详细说明功能。

Form1

  public   partial   class  Form1:Form 
{

private void Form1_Load( object sender,EventArgs e)
{
MessageBox.Show(Form.ActiveForm.Name); // 空,因为表格未加载

}

private void button1_Click( object sender,EventArgs e)
{
MessageBox.Show(Form.ActiveForm.Name); // // Form1,因为此表单已加载并处于活动状态(从此表单调用按钮)。
new Form2()。Show(); // 触发Form2
}


}





Form2



  public   partial   class  Form2:表格
{
私有 void Form2_Load( object sender,EventArgs e)
{
MessageBox.Show(Form.ActiveForm.Name); // Form1,因为它是从Form1调用的
}

private void button1_Click( object sender, EventArgs e)
{
MessageBox.Show(Form.ActiveForm.Name); // Form2,因为此表单已加载并处于活动状态(从此表单调用按钮)。
}
}


你接受了两个答案,但没有一个是正确的。



正确的事情很简单:你需要了解编程的基础知识,首先是生命周期。这太简单了:表单文件根本不存在。构建应用程序后,所有源文件都被遗忘;应用程序不知道它们。



当然,没有可能需要文件名的情况。你不需要知道不存在的东西。如果你认为你需要一些,你必须解释原因,但我可以提前回答:没有这种情况。



令人惊讶的是,很多你拥有的东西在您的源代码中保留:所有类和所有其他类型,所有成员,名称。这些信息可以通过反射访问。



此外,你永远不应该依赖属性 System.Windows.Forms .Control.Name



我建议你在对这个问题的评论中做些什么。你根本没有准备好开发UI,需要先学习一些编程,UI只会让你分心。



-SA

I like to find the active form file name in C#

What I have tried:

Form form = Form.ActiveForm;
string FormName = form.Name.ToString();

Result: Object reference not set to an instance of an object.

解决方案

It probably depends on when you are doing this.
If you are doing it in the form constructor, then you may have a problem.
In the Load event, you will get a blank name.
In the Shown event or later, you will get the correct form name.

But...I can't get a null reference exception in any of them!
So check the code in the debugger and see exactly what is going on and where exactly the error occurs -= look for the null value in the line when the exception is triggered.


IF you try to call that code in the Form load of the current (First Form) , you will get Null Value.

what i have tried to get the Active Form Name.
Create Two Forms Form1 and Form2 with a button in each to detail the functionality.
Form1

public partial class Form1 : Form
    { 

        private void Form1_Load(object sender, EventArgs e)
        { 
            MessageBox.Show(Form.ActiveForm.Name); // Null, because the Form is not loaded
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Form.ActiveForm.Name); // // Form1, because this Form is loaded and active (button invoked from this form ). 
             new Form2().Show(); // trigger the Form2
        }

       
    }



Form2

public partial class Form2 : Form
   {
       private void Form2_Load(object sender, EventArgs e)
       {
           MessageBox.Show(Form.ActiveForm.Name);  // Form1, since it is called from Form1
       }

       private void button1_Click(object sender, EventArgs e)
       {
           MessageBox.Show(Form.ActiveForm.Name); // Form2, because this Form is loaded and active (button invoked from this form ).
       }
   }


You accepted two answers, but none of them is correct.

The correct thing is very simple: you need to understand the very basics of programming, first of all, life cycle. This is too simple: the "form file" simply does not exist. After you build the application, all source files are "forgotten"; the applications "know" nothing about them.

And of course there are no the situations when you possibly need the file name. You don't need to know something which does not exist. If you think you need some, you have to explain why, but I can answer in advance: there are no such situations.

Amazingly, a lot of what you had in your source code is preserved: all your classes and all other types, all their members, their names. This information is accessible through reflection.

Also, you should never rely on the property System.Windows.Forms.Control.Name.

I advised what you should do in my comment to the question. You are not at all ready to develop UI, need to learn some more of programming first, and UI would only distract you from this.

—SA


这篇关于如何在C#中获取表单文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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