在 C# 表单之间传递数据 [英] Passing data between C# forms

查看:44
本文介绍了在 C# 表单之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在两个表单之间传递数据(我想要做的就是在 Form1 中有一个文本框,并在位于 Form2 的 textbox1 中显示该文本框的值).我将如何使用 WPF 解决这个问题?已经看了很多解决方案,但似乎根本无法让其中任何一个起作用.

I am struggling to pass data between two forms (all I want to do is have a textbox in Form1, and show that textbox value in textbox1, which is located in Form2). How would I go about this, using WPF? Have looked at quite a few solutions, but cannot seem to get any of them at all to work.

对于我想要显示值的表单(在 tbd.Text 中),代码如下:

For the form in which I'm wanting to display the values (in tbd.Text), here is the code:

namespace test
{
    /// <summary>
    /// Interaction logic for OptionDisplayWindow.xaml
    /// </summary>
    public partial class OptionDisplayWindow : Window
    {
        public OptionDisplayWindow()
        {

            InitializeComponent();
            tbd.Text = "k"; //want to change this value based on "s" in the other form

        }

文字转自的形式(想显示字符串):

The form in which the text is transferred from (want to display the string):

public void Button1_Click(object sender, RoutedEventArgs e)
        {
            string s = "testText"


       }

我已经尝试了所有其他关于 SO 的答案(过去 6 个小时都在尝试)并且完全没有运气.

I have tried every single other answer on SO (spent the past 6 hours trying) and have had absolutely no luck.

编辑 2:使用此处列为最佳答案的方法 发送从一种形式到另一种形式的值我为 Form1 编写了以下代码:

EDIT 2: Using the method listed as the best answer here Send values from one form to another form I've come up with this code for Form1:

private void ttbtn_Click(object sender, RoutedEventArgs e)
        {
            using (Form2 form2 = new Form2())
            { 
                tbd.Text = form2.TheValue;
                }
            }

以及 Form2 的代码:

And the code for Form2:

        public string TheValue
        {
            get { return arrayTest.Text; }
        }

但是,我收到错误Form 2":using 语句中使用的类型必须可隐式转换为System.IDisposable".

However, I'm getting the error 'Form 2': type used in a using statement must be implicitly convertible to 'System.IDisposable'.

推荐答案

您放在示例项目中的代码(您在评论中作为链接提供)应该在您的问题中.鉴于它变得更容易理解您正在尝试做什么并为您提供可行的解决方案.

The code that you put in the sample project (that you provided as a link in the comments) should be in your question. Given that it becomes much easier to understand what you're trying to do and to give you a workable solution.

我建议创建一个DataTransferObject"并在每个表单之间传递它.

I would suggest creating a "DataTransferObject" and pass that between each form.

public class Dto
{
    public string Text;
}

MainWindow 中的代码如下所示:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        var dto = new Dto();
        window2 win2 = new window2();
        win2.Dto = dto;
        win2.ShowDialog();
        textBox1.Text = dto.Text;
    }

window2 中的代码如下所示:

    public Dto Dto;

    private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (this.Dto != null)
        {
            this.Dto.Text = textBox2.Text;
        }
    }

这是在表单之间传输数据的一种方式 - 大约一百万种.使用数据传输对象的一个​​优势是它可以让您开始将数据与 UI 分离,这通常是一件非常好的事情.

That is one way - out of about a million - of transferring data between forms. An advantage of using a data transfer object is that it begins you on the road of separating your data from your UI, and that is generally a very good thing to do.

这篇关于在 C# 表单之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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