访问表单成员从另一个类 [英] Accessing form members from another class

查看:93
本文介绍了访问表单成员从另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点是在接收/在TCP客户端发送的分组使用备忘录通知用户。
的旧代码是多余的哑巴,我使用了用于在备忘录中添加文本由于计时器访问的形式成员定时器,哈哈。

The point is to notify the user using the memo when a packet is received/sent in a TCP Client. The old code was extra dumb,I used a Timer that used to add text in the memo since the Timer has access to the form members,lol.

旧的代码:

//Memo.Text += txt + "\n";



今天我发挥它,这就是我所做的。

I played with it today,this is what I've done

在Form1的类

public string TextValue
{
    get
    {
        return Memo.Text;
    }

    set
    {
        this.Memo.Text += value + "\n";
    }
}



我所说的这样的代码是:

I call the code like that:

Form1 myForm = new Form1();
myForm.TextValue = "test asdasd";



该备忘录修饰符的私人,但是这不是问题。

的问题是,没有文字显示在我的呼唤代码备忘录。

The problem is that no text is displayed on the memo when i call the code.

推荐答案

通过输入这样的:

Form1 myForm = new Form1();

在创建窗体(Form)的新实例,而是我猜你应该使用现有的实例。其中最有可能已被初始化

you create a new instance of your form (Form1), but instead I guess you should use existing instance which most likely has been initialized already.

一个做到这一点的方法:

One of the ways to do it:

var form = Form.ActiveForm as Form1;

if (form != null)
{
     form.TextValue = "test asdasd";
}



虽然这不是很好的设计。尝试使用自定义的 活动 来代替。

这篇关于访问表单成员从另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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