简体   繁体   English

如何将价值从一种形式转移到另一种形式?

[英]How to transfer value from one Form to another?

Ok Here is what I want. 好的这就是我想要的。 I have created a Class 我创建了一个类

public class docname
    {
        private string doctname;

        public string Doctname
        {
            get { return doctname; }
            set { doctname = value; }
        }
    }

and I have used it in a form 而且我在表格中使用过它

public string name;
docname dc = new docname();
dc.Doctname = name;

and when I check the value in another form I get a null value. 当我以另一种形式检查值时,我得到一个空值。 Whats the reason behind this? 这背后的原因是什么?

I am a beginner at C#. 我是C#的初学者。

Well, in your code sample, you're not actually assigning anything to the public string name variable, so it will be null until you assign a value to it. 好吧,在您的代码示例中,您实际上并没有为public string name变量分配任何内容,因此在为其分配值之前它将为null。 Assuming that's just a typo, you need to make sure that both of your forms are referring to the same instance of your DocName class (only create a new DocName() once in your code, and then pass that reference to both forms). 假设这只是一个拼写错误,您需要确保两个表单都引用了DocName类的同一个实例(只在代码中创建一个new DocName() ,然后将该引用传递给两个表单)。

Form myForm1    = new Form();
Form myForm2    = new Form();
DocName dn      = new DocName();
myForm1.docName = dn;
myForm2.docName = dn;
dn.DoctName     = "SomeDocumentName.txt";

MessageBox.Show(myForm1.docName.DoctName);  // "SomeDocumentName.txt"
MessageBox.Show(myForm2.docName.DoctName);  // "SomeDocumentName.txt"

Because there is only one instance of your DoctName class, the property of that class will persist regardless of which form is calling it. 因为只有一个DoctName类的实例,所以该类的属性将保持不管哪个表单调用它。

public string name; 公共字符串名称; <--- name = "blah"; <--- name =“blah”; <--- extra code docname dc = new docname(); <---额外代码docname dc = new docname(); dc.Doctname = name; dc.Doctname = name;

Well, according to your code, the variable called "name" is null. 好吧,根据您的代码,名为“name”的变量为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将comboBox中选定值的数据从一种形式传输到另一种形式 - How to transfer data of selected value in comboBox from one form to another 如何在WinForms C#中将数据从一种形式传输到另一种形式? - How to transfer data from one form to another in winforms c#? 如何将值从一种形式转移到另一种形式? - How do I transfer values from one form to another? 将会话数据从一种形式传输到另一种形式 - Transfer Session data from one form to another 将数据从一个表单 dataGridView 传输到另一个表单 TextBox - transfer data from one form dataGridView to another form TextBox 如何将文本从一个表单中的 DataGridView 传输到另一个表单中的私有 TextBox? - How can I transfer text from a DataGridView in one Form to a private TextBox in another Form? 如何将图像从一个场景转移到另一个场景 - How to Transfer an image from one scene to another 如何将花车从一个场景转移到另一个场景 - how to transfer a float from one scene to another 如何使用文本框的值从一种形式到另一种形式? - How to use value of textbox from one form to another form? 如何将值从一个窗体传递到另一个窗体的私有TextBox? - How to pass a value from one Form to a private TextBox in another Form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM