如何使用Set和Get属性将值插入文本框 [英] How Do I Insert Value To Textbox Using Set And Get Properties

查看:135
本文介绍了如何使用Set和Get属性将值插入文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.net的新手,现在我在Windows窗体应用程序中做一个项目。所以我想使用属性(在diff类中声明)将值分配给文本框... plz帮助我

i am new to .net, now im doing one project in windows form application. so i want to use properties(which are declared in diff class) to assign values in to textbox... plz help me

推荐答案

就目前而言,你的问题没有似乎很有道理:它是在不同类中声明的一点混乱。



使用From属性设置文本框值很容易:

As it stands, your question doesn't seem to make a lot of sense: it's the "declared in a different class" bit that confuses.

Setting a textbox value using a From property is easy:
public string SetTextBox
    {
    get { return myTextBox.Text; }
    set { myTextBox.Text = value; }
    }





但是如果您将该属性作为与该表单无关的其他类的一部分,那么它是一个更复杂:你必须使用活动。



在你的课堂上创建一个活动:



But if you have the property as part of a different class which is not related to the form it's a bit more complex: you have to use Events.

Create a event in your class:

public class MyClass
    {
    /// <summary>
    /// Event to indicate Text Changed
    /// </summary>
    public event EventHandler TextChanged;
    /// <summary>
    /// Called to signal to subscribers that the Text Changed
    /// </summary>
    /// <param name="e"></param>
    protected virtual void OnTextChanged(EventArgs e)
        {
        EventHandler eh = TextChanged;
        if (eh != null)
            {
            eh(this, e);
            }
        }
    }

您的Form类将添加一个事件处理程序方法来更新实际的文本框。

将您的属性添加到您的class,在setter中,你发出事件的信号:

Your Form class will add an event handler method to update the actual textbox.
Add your Property to your class, and in the setter, you signal the event:

private string _SetTextBox;
public string SetTextBox
    {
    get { return _SetTextBox; }
    set
        {
        _SetTextBox = value;
        OnTextChanged(new EventArgs());
        }
    }

这使得表单可以更新自己的文本框:

This makes the form do the work of updating it's own Textbox:

    ...
    myClassInstance.TextChanged += new EventHandler(myClassInstance_TextChanged);
    }
private MyClass myClassInstance = new MyClass();

void myClassInstance_TextChanged(object sender, EventArgs e)
    {
    myTextBox.Text = myClassInstance.TextBoxText;
    }


这篇关于如何使用Set和Get属性将值插入文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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