0

I need help transferring text from one textbox in one page to another textbox in another page since I'm new to WPF. I was able to change pages, but I can't get the textbox from the first page into the textbox in the second page. It just changes pages but doesn't add in the value from the 1st textbox. I've been looking for help for hours but can't make it work out.

Page1.xaml:

<TextBox x:Name = "ATextBox" KeyDown="AppealNumberTextBoxEnter"/>

Page1.cs:

            private void AppealNumberTextBoxEnter(object sender, KeyEventArgs e)
    {
        //When Enter Key is pushed in textbox
        if (e.Key == Key.Return)
        {NavigationService ns = NavigationService.GetNavigationService(this);
            ns.Navigate(new Uri("Page2.xaml", UriKind.Relative));}

            Page2 p2 = new Page2();
            p2.BTextBox.Text = ATextBox.Text;

Page2.xmal:

<TextBox x:Name = "BTextBox" KeyDown="BillingDateTextBoxEnter"/>

Page2.cs:

private void BillingDateTextBoxEnter(object sender, KeyEventArgs e)
    {
        //When Enter Key is pushed in textbox
        if (e.Key == Key.Return)
        {
            MessageBox.Show("Find Billing date for: " + BTextBox.Text);
        }
    }
  • 1
    I guess that the NavigationService is creating a new ``Page2`` internally and navigating to that page. So the one that you are creating is totally a different one. This might be why you can't see the text in the ``Page2``. It's hard to see the whole picture but you might wanna implement this in MVVM style and use binding to set the text. – Andrew KeepCoding Sep 07 '22 at 08:07
  • I tried learning MVVM a while ago, but I couldn't get a good grasp. I'll probably try learning it again though. With Binding, can you get data from multiple sources, like a database and textboxes from different pages? Also, can you to make the ENTER Key in Page2 also be pressed after the text is entered? – Oscar Rodriguez Sep 08 '22 at 00:01
  • There is a feature called [MultiBinding](https://stackoverflow.com/a/2552911/2411960), but I'm not sure if this is what you want. – Andrew KeepCoding Sep 08 '22 at 00:16
  • Sorry, can you be more specific on you 2nd question? I couldn't get it. – Andrew KeepCoding Sep 08 '22 at 00:18
  • Yup, MultiBinding is exactly what I was looking for! And for the 2nd question, I was referencing my original post - After successfully getting the new page (Page2) to load with the text from the old textbox (ATextBox from Page1), is it possible to have the ENTER Key pressed as code from the old page (Page1). Can Page 2.cs BillingDateTextBoxEnter event get set off in the Page1.cs after Page2 is loaded with the text from ATextBox (Page1 textbox)? – Oscar Rodriguez Sep 08 '22 at 00:26
  • Still can't get the picture. 1) You click enter in Page1. 2) AppealNumberTextBoxEnter() is called and loads Page2 with the text from Page1. and what then? – Andrew KeepCoding Sep 08 '22 at 01:44
  • I was thinking: 1) You enter text into ATextBox - Good. 2) You push enter in Page1 - Good. 3) Navigate to Page2 - Good. 4) Page2 BTextBox is filled with text from Page1 ATextBox - Good. **5)** Have BillingDateTextBoxEnter (Page 2.cs) be activated on Page2 that was just created. But, to activate it, I'd need to push the enter key. Could it be activated without pushing the the enter key from the Page1.cs after navigating to Page2 and filling in the textbox? Hopefully this makes more sense, but if not don't worry about it, you've already been very helpful – Oscar Rodriguez Sep 08 '22 at 18:29

2 Answers2

1

Your code doesn't work because you modify the Text of a TextBox of a page you just created, but that page isn't the same as the one that was created with the call to Navigate().
What you can do is create the Page2, then navigate to it, such as :

if (e.Key == Key.Return)
{
    Page2 p2 = new Page2();
    p2.BTextBox.Text = ATextBox.Text;

    // Note: if you are in Page.xaml.cs, I think you can simplify
    // 'NavigationService.GetNavigationService(this)' to just 'NavigationService'
    NavigationService.Navigate(p2);
}

I think it is also possible to keep your current code with a small change :

if (e.Key == Key.Return)
{
    NavigationService ns = NavigationService.GetNavigationService(this);
    ns.Navigate(new Uri("Page2.xaml", UriKind.Relative));

    Page2 p2 = (Page2)ns.Content;
    p2.BTextBox.Text = ATextBox.Text;
}

Note that I moved the text change inside the if condition, as there is no need to update the text of the TextBox in page 2 if it is not displayed.

Arkane
  • 352
  • 4
  • 10
  • Andrew's suggestion of using MVVM is an even better solution. – Arkane Sep 07 '22 at 08:17
  • Thank you!! The top code worked for me. I doubt this is possible, but would it be possible to make the ENTER Key in Page2 also be pressed after the text is entered? – Oscar Rodriguez Sep 07 '22 at 23:49
  • I just noticed the edit in your post above. There is a way to send the Enter key, but it will be a lot simpler to create a new public method in Page2.cs that you can call from AppealNumberTextBoxEnter() in Page1. – Arkane Sep 12 '22 at 09:14
1

Since you have just started learning WPF, I advise you to immediately learn the correct implementation. The concept of WPF is built around UI elements getting the data they need on their own. To do this, you need to create a data source object to the properties of which the properties of UI elements will be bound. In this implementation, all data is stored in this source object, and updating its properties with any element updates all other bound properties as well.

In the simplest case, it's like this:

    public class MyProxy
    {
        public string SomeText {get; set;}
    }

In App Resources:

    <Application.Resources>
        <vms:MyProxy x:Key="proxy"/>
    </Application.Resources>

vms: is the namespace prefix of the MyProxy class.

In Pages:

    <TextBox Text="{Binding SomeText, Source={StaticResource proxy}}"/>

When implementing MVVM (and this is very typical for WPF), such a data source is called a ViewModel and is passed to the DataContext of the Window and/or Page. In this case, bindings are simplified to:

    <TextBox Text="{Binding SomeText}"/>

If the data source properties are supposed to be changed from the Sharp code, then the INotifyPropertyChanged interface must be implemented in the data source. Here is my simple implementation: BaseInpc. And the data source based on it:

    public class MyViewModel : BaseInpc
    {
        private string _someText;
        public string SomeText 
        {
            get => _someText;
            set => Set(ref _someText, value);
        }
    }
EldHasp
  • 6,079
  • 2
  • 9
  • 24
  • I've tried learning MVVM, but it seemed really confusing so I backed away from it, but if its part of best practices I'll probably give it another shot. Also, do you know if its possible to have Binding from more than one source? Is that what MVVM also allows? And lastly, would it be possible to make the ENTER Key in Page2 also be pressed after the text is entered? – Oscar Rodriguez Sep 07 '22 at 23:59
  • Most often, one source (ViewModel) for one view (View) is used. This is because WPF elements have a DataContext property that is used by Bindings as the default source. With it, you can simplify bindings to `Text="{Binding SomeText}"`. But you can use many sources, if you then explicitly specify them in the bindings. I showed this at the beginning of my example `Text="{Binding SomeText, Source={StaticResource proxy}}"`. – EldHasp Sep 08 '22 at 07:05
  • Very often the Locator or MainViewModel is used, which have other ViewModels in their properties, which can be used for different Views or simultaneously in one of some Views. It is difficult to give a general answer, since MVVM sets the rules for the interaction of app layers, but does not determine the requirements for the implementation of these layers. Implementations are different and there are a lot of them. The optimal implementations for the Solution are selected depending on the conditions of a particular task. – EldHasp Sep 08 '22 at 07:09
  • "> And lastly, would it be possible to make the ENTER Key in Page2 also be pressed after the text is entered?" I didn't understand this question. Maybe you need `Button.IsDefault=true`? – EldHasp Sep 08 '22 at 07:12
  • Thanks for the bindings and MVVM rundown! As for the ENTER Key part, I was trying to activate the Page2.cs method BillingDateTextBoxEnter from the Page1.cs. The BillingDateTextBoxEnter method is activated if (e.Key == Key.Return). So, I was trying to see if I could activate it from the Page1.cs by making the ENTER Key go off after having already navigated to Page2 and filled in the textbox. If this still doesn't make sense, no worries, you already helped me out a lot, so thank you – Oscar Rodriguez Sep 08 '22 at 18:43