两个窗口形式之间传递值 [英] Passing values between two windows forms

查看:109
本文介绍了两个窗口形式之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是这样的,我有同样的解决方案/项目的两种不同形式。我需要做的是提取表格A标签的值,并将其加载到表B.尽可能,我住使用此代码走,因为它只会发生冲突我的整个程序:

  FormB myForm会=新FromB(label.Text); 
myForm.ShowDialog();



我试图现在是获取和我想要的值设置其属性的class通过。但是,每当我从FormB访问get方法,它返回一个空值。



我希望有人能帮助我与此有关。任何其他的方法可以做到这是非常赞赏。 :)

 公共类杂项
{
字符串添加my_id;

公共无效SETID(字符串ID)
{
添加my_id = ID;
}

公共字符串的getId()
{
返回添加my_id;
}
}


解决方案

您可以做这样的事情:



儿童形式

 公共字符串YourText {搞定;组; } 

公共TESTFORM()
{
的InitializeComponent();
}

公共无效UpdateValues​​()
{
someLabel.Text = YourText;
}



启动它



  VAR孩子=新TESTFORM {YourText = someTextBox.Text}; 

child.UpdateValues​​();

child.ShowDialog();

通过这种方法,你不必改变构造函数,你也可以添加其他的构造。



原因他们是空的是,属性在构造函数设置后,你也可以做些事情像这样有点逻辑添加到您的getter和setter方法​​,但是!,我会考虑不影响对性能的UI

 私人字符串_yourText =的String.Empty; 
公共字符串YourText
{
得到
{
返回_yourText;
}

{
_yourText =价值;
UpdateValues​​();
}
}

在这种情况下,用户界面​​将全自动时更新您设置的属性。


The case is this, I have two different forms from the same solution/project. What I need to do is to extract the value of a label in Form A and load it into Form B. As much as possible, I am staying away from using this code since it will only conflict my whole program:

FormB myForm = new FromB(label.Text);
myForm.ShowDialog();

What I am trying right now is a class with a property of get and set for the value I wanted to pass. However, whenever I access the get method from FormB, it returns a blank value.

I hope somebody can help me with this. Any other ways to do this is extremely appreciated. :)

    public class Miscellaneous
    {
       string my_id;

       public void SetID(string id)
       {
           my_id = id;
       }

       public string GetID()
       {
           return my_id;
       }
     }

解决方案

You could do something like this:

Child form

public string YourText { get; set; }

public TestForm()
{
    InitializeComponent();
}

public void UpdateValues()
{
    someLabel.Text = YourText;
}

Initiate it

var child = new TestForm {YourText = someTextBox.Text};

child.UpdateValues();

child.ShowDialog();

With this approach you don't have to change the Constructor, you could also add another constructor.

The reason for them being empty is that the properties are set after the constructor, you could Also do someting like this to add a bit of logic to your getters and setters, However, I would consider not affecting UI on properties!

private string _yourText = string.Empty;
public string YourText
{
    get
    {
        return _yourText;
    }
    set 
    { 
        _yourText = value;
        UpdateValues(); 
    }
}

In this case, the UI will be updated automaticly when you set the property.

这篇关于两个窗口形式之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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