简体   繁体   English

将Windows窗体中的文本框值传递给类

[英]Pass Textbox Value From Windows Form To Class

I am learning C# and have run into an interesting issue to me. 我正在学习C#,遇到了一个有趣的问题。 I have a class variable defined as public and I instantiate a new instance of my form in my class and access the value of the public variable it is always null. 我有一个定义为public的类变量,并且在我的类中实例化了表单的新实例,并访问它始终为null的public变量的值。

To further explain my issue - this syntax prints the appropriate value 为了进一步解释我的问题,此语法会输出适当的值

System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());

However, this syntax is always outputting a 0 但是,此语法始终输出0

System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());

How do I need to alter my syntax so that the correct value is passed to the class Functions ? 我该如何更改语法,以便将正确的值传递给Functions类?

public partial class Form1 : Form
{

    public double tboxvalue;
    private string exportdata;

    public Form1()
    {
        InitializeComponent();
    }

    private void btnClicker_Click(object sender, EventArgs e)
    {
        Functions.EE();
    }
    private void txtData_CheckedChanged(object sender, EventArgs e)
    {
        bool @checked = ((CheckBox)sender).Checked;

        if (@checked.ToString() == "True")
        {
            exportdata = "Yes";
            tboxvalue = Convert.ToDouble(this.txtData.Text);
            System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());
        }
        else
            exportdata = "No";
    }
}
class Functions
{
    public static void EE()
    {
        Form1 f1 = new Form1();
        System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
    }
}

To access properties of the form, you need to change two Things. 要访问表单的属性,您需要更改两个内容。 First you have to pass the form to the 'EE' method, then you can access the form's properties. 首先,您必须将form传递给'EE'方法,然后才能访问表单的属性。 Second, don't create a new form in 'EE' method. 其次,不要在“ EE”方法中创建新表格。

public partial class Form1 : Form
{

    public double tboxvalue;
    private string exportdata;

    public Form1()
    {
        InitializeComponent();
    }

    private void btnClicker_Click(object sender, EventArgs e)
    {
        Functions.EE(this);
    }
    private void txtData_CheckedChanged(object sender, EventArgs e)
    {
        bool @checked = ((CheckBox)sender).Checked;

        if (@checked.ToString() == "True")
        {
            exportdata = "Yes";
            tboxvalue = Convert.ToDouble(this.txtData.Text);
            System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());
        }
        else
            exportdata = "No";
    }
}
class Functions
{
    public static void EE(Form1 f1)
    {
        System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
    }
}

If i understood your question i guess you are recreated Form1 with own textbox or labels when you click btnClicker button. 如果我理解您的问题,我想您在单击btnClicker按钮时会重新创建带有自己的文本框或标签的Form1。 You can reassign your form objects where you created it. 您可以在创建表单对象的地方重新分配它。

You might add static Form1 object and Setter routine to Functions class: 您可以将静态Form1对象和Setter例程添加到Functions类:

    private static Form1 _form;

    public static void SetForm(Form1 form)
    {
        _form = form;
    }

and pass the form to the class in Form_Load event-click on the form twice: 并将表单传递给Form_Load事件中的类,然后在表单上单击两次:

private void Form1_Load(object sender, EventArgs e)
    {
        Functions.SetForm(this);
    }

Then you can play with the form in Functions class using the object _form 然后,您可以使用_form对象在Functions类中使用表单
good luck! 祝好运!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM