Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
959 views
in Technique[技术] by (71.8m points)

wpf - Passing data between C# forms

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.

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"


       }

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

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;
                }
            }

And the code for Form2:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

public class Dto
{
    public string Text;
}

The code in MainWindow would then look like this:

    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;
    }

And the code in window2 would look like this:

    public Dto Dto;

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...