0

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'.

Community
  • 1
  • 1
user3879060
  • 25
  • 1
  • 8
  • This will get closed unless you post some code. – Evan Trimboli Aug 28 '16 at 06:53
  • Sorry, new to SO, give me a minute! – user3879060 Aug 28 '16 at 06:54
  • 1
    @user3879060 actually there're already lots of tutorials regarding this: http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form first result from google – User2012384 Aug 28 '16 at 07:01
  • Tried that solution but it's giving me two errors: ..."must be implicitly convertible to System.IDisposable", and "bool does not have a definition for 'OK'". – user3879060 Aug 28 '16 at 07:13
  • Give us some context... are you trying to open `Form2` from `Form1`? what's the flow of stuff? – Giora Guttsait Aug 28 '16 at 07:43
  • As shown in the code snips (my explanation before that is trash), the first snip is from Form2, and the second from Form1. I want to display "string s = "testText" " in tbd.Text of Form2, so the "tbd" textbox will show "testText" if that makes sense. – user3879060 Aug 28 '16 at 07:49
  • @user3879060 - How are you getting ""must be implicitly convertible to System.IDisposable"? Can you show us the code that you've tried? – Enigmativity Aug 28 '16 at 08:18
  • Just added another edit with the code I tried. – user3879060 Aug 28 '16 at 08:28
  • @user3879060 - Windows Forms forms are disposable. WPF forms are not. That's why that solution doesn't work. – Enigmativity Aug 28 '16 at 08:32
  • @user3879060 - Also the solution you linked to only works because of the `form2.ShowDialog()`. That pauses the code execution while that dialog is shown. You don't have that in your code so it'll appear as if nothing has happened. – Enigmativity Aug 28 '16 at 08:34
  • Thanks, saved me a lot of time troubleshooting. Might just switch to Windows Forms. – user3879060 Aug 28 '16 at 08:36
  • @user3879060 - Can you show the code that creates both of your forms? Particularly the current interaction between the two? – Enigmativity Aug 28 '16 at 08:37
  • @user3879060 - Can you also use the `@` notification system please? – Enigmativity Aug 28 '16 at 08:38
  • @Enigmativity I put together a basic project (the actual is quite large and has a fair few resources) here to demonstrate the basics of what is going on. The window structure is the same http://wikisend.com/download/810600/wpftest.zip – user3879060 Aug 28 '16 at 08:50

2 Answers2

2

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.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Thanks. I was actually trying to pass data the other way (didn't make this clear, sorry), and have attempted to do so, but I only get an empty box on the 2nd window (I suspect because Dto is always null, and I can't seem to change this). Here's the updated code in my horrible attempt to try and change it :/. http://s000.tinyupload.com/index.php?file_id=45688752685779824697 – user3879060 Aug 28 '16 at 11:08
  • @user3879060 - The file you uploaded comes up with "This file contains a virus or malware" when I download it. In any case, the `Dto` can work both ways with no trouble. – Enigmativity Aug 28 '16 at 12:28
  • Sorry about that, here's a screenshot of what I've done http://imgur.com/a/qAmJL The dto ends up remaining null and therefore doesn't transfer to the window2 textbox. – user3879060 Aug 28 '16 at 13:09
  • You're creating a new instance of `MainWindow` in `window2` - that's not going to do anything useful. In `MainWindow` you need to instantiate and populate the `Dto`, but you don't need a field for `Dto` here. In `window2` you need the field. Once you set the field in `window2` then you can populate the text box. – Enigmativity Aug 28 '16 at 13:39
1

Another simple way to pass data between forms is using your application's settings.

Step 1: Create a setting, open the "Project" menu and pick "test Properties..."

this will take you to the settings page, create a setting name it however you want, I named mine "PassString" and make sure it's a string type and the Scope is set to user.

Step 2. Lets set the string setting to your textbox.text property, add these changes to the code:

private void button1_Click(object sender, RoutedEventArgs e)
{
    Properties.Settings.Default.PassString = textBox1.Text;
    window2 win2 = new window2();
    win2.ShowDialog();
}

Step 3. Update the text on your second window Initialization Process.

    public OptionDisplayWindow()
    {
        InitializeComponent();
        tbd.Text = Properties.Settings.Default.PassString;
    }

P.S. you may have to add a reference to reach your application settings.

    using test.Properties;                 
Dark Templar
  • 1,130
  • 8
  • 10
  • Thanks :D. Is there any way to use that method with multiple variables, or do I need to create different settings for each? E.g. if a form had textBox1 and textBox2 and I wanted to pass each to a variable in the second window. – user3879060 Aug 28 '16 at 13:33
  • there are many types of settings, this example simply passes a string which you set or get whenever you like, what exactly do you mean? – Dark Templar Aug 28 '16 at 14:11