如何从MainForm获取数据到类 [英] How to get data from MainForm to class

查看:63
本文介绍了如何从MainForm获取数据到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我在我的班级访问数据时遇到问题。从Class到Main Form的数据我没有问题。我尝试了更多的方法但没有为我工作(我不知道为什么)。我需要在Main表单中设置几个字符串,这个字符串可以从我的类中访问。



例如主表单:

Hello,

I have problem to acces data in my class.With data from Class to Main Form i have no problem. I try more ways but nothing working for me (i do not know why). I need in Main form set few strings and this strings have accesable from my Class.

For example Main form:

private void button1_Click(object sender, EventArgs e)
{
    TestClass tc = new TestClass();

    if (tc.Lunch() == true)
        MessageBox.Show("Data passed");
    else
        MessageBox.Show("Error");
}

private string myprop;
public string MyProperty
{
get{return  myprop ;}
set{myprop = "Hello" ;}
}



和类:


and Class:

class TestClass
    {
        public bool Lunch()
        {
            Form1 f1 = new Form1();
            string newStr = f1.MyProperty;

            if (newStr == "Hello")
                return true;
            else
                return false;
        }
    }





Actualy使用用户属性将数据从主表单传递到类,但是这很麻烦



Actualy for passing data from Main Form to class using user property but this is much slover

Properties.Settings.Default.UserString = "Hello";
Properties.Settings.Default.Save();



谢谢。


Thank you.

推荐答案

问题是你仍然不知道一般编程。在这种情况下,您的问题是理解类型与类型实例,实例化,范围,实例与静态成员。这些是OOP系统中pre-OOP编程的基础知识,包括.NET。如果没有这方面的知识,建议您不要使用任何UI。您可以根据仅限控制台的应用程序进行最简单的练习。



有关解决不同类别的表单实例之间的通信问题的详细信息(在您的情况下,与一些非形式类,这将不会产生实际差异),请参阅我的文章: 一次回答的许多问题 - Windows窗体或WPF Windows之间的协作



但如果这样文章并非100%清楚,您还没有准备好做UI或其他高级主题;在这种情况下,回到基础,以获得更多的信心。稍后你会回到更复杂的东西,也许很快,不用担心。



-SA
The problem is that you still have no clue on general programming. In this case, your problem is understanding types vs instances of types, instantiation, scope, instance vs static member. These are bare basics of "pre-OOP" programming in OOP systems, including .NET. Without this knowledge, you are not recommended to do any UI at all. You can train yourself on simplest exercises based on console-only applications.

For the detail of solving the problem of communication between form instances of different classes (in your case, with some non-form class, which won't make and practical difference), please see my article: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

But if this article is not 100% clear for you, you are not yet ready to do the UI or other advanced topics; in this case, get back to basics, to gain some more confidence. You will come back to more complex stuff a bit later, maybe pretty soon, don't worry.

—SA


这篇关于如何从MainForm获取数据到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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