How to pass a textbox value to another window and close the current window.

Apr 5, 2013 at 9:23 PM
Scenario:
The user will input a 'value' in a textbox, then click a button. This will close the current window, open another window and populate a textbox with the 'value'.

Any suggestions. Thanks.
Coordinator
Apr 6, 2013 at 11:30 AM
This is a general WPF question. Check this post on stackoverflow -> http://stackoverflow.com/questions/2796470/wpf-create-a-dialog-prompt/2796487#2796487.

I suggest you put your textbox in a ModernDialog derived control.
Jul 13, 2013 at 9:48 PM
Hi Kozw,

I've had a look at your suggested solution and it just doesn't work within Modern UI. Neither does a textbox in a moderndialog solve the problem.
I would like to use Delegates, which works fine in WPF, to pass data from a pop-up window back to the calling window.

Here is my current code, but I've had to revert to a standard WPF Window and this defeats the purpose of the look-n-feel of ModernUI.

Page 1, is a UserControl with a textbox and a button. This is a USERCONTROL.
Page 2, is a Window with a textbox and a button. This is a WINDOW and is where the problem lies, it should be a USERCONTROL in ModernUI format, but I digress.

Page 1 - codebehind
  private void btnGetCustName_Click(object sender, RoutedEventArgs e)
    {
        UserControl1 f = new UserControl1();
        f.AddressUpdated += new UserControl1.AddressUpdateHandler(AddressForm_ButtonClicked);
        f.ShowDialog();
    }
    private void AddressForm_ButtonClicked(object sender, AddressUpdateEventArgs e)
    {
        // update this form's value from the event args on dialog control
        txtName.Text = e.CustName;
    }
Page 2 - codebehind
   public delegate void AddressUpdateHandler(object sender, AddressUpdateEventArgs e);
    public event AddressUpdateHandler AddressUpdated;

    public UserControl1()
    {
        InitializeComponent();     
    }
    private void btnOK_Click(object sender, RoutedEventArgs e)
    {
        string sCustName = txtCustName.Text;
        AddressUpdateEventArgs args = new AddressUpdateEventArgs(sCustName);
        AddressUpdated(this, args);
        this.Close();
    }
}  
public class AddressUpdateEventArgs : System.EventArgs
{
    private string mCustName;
    public AddressUpdateEventArgs(string sCustName)
    {
        this.mCustName = sCustName;          
    }
    public string CustName
    {
        get
        {
            return mCustName;
        }
    }  
}  
As I said, this works fine if the Page 2 (UserControl1) is a WPF window - but I want it to be a ModernUI Dialog or at least look like it.

If, I change Page 2 (UserControl1) to be a USERCONTROL, then I cant pass this command -
f.AddressUpdated += new UserControl1.AddressUpdateHandler(AddressForm_ButtonClicked);
to the dialog as it gives an error message on f.showdialog() saying that UserControl1 (Page2) does not contain a definition for ShowDialog...

If, I change the load button action to be like a normal moderndialog load -
ModernDialog MyDiag = new ModernDialog();
        MyDiag.Title = "Name Search";
        MyDiag.Content = new UserControl1();
        MyDiag.ShowDialog();
then, how do I pass the delegate handler - AddressUpdated += new UserControl1.AddressUpdateHandler(AddressForm_ButtonClicked);
to the dialog?

Basically, I'm thinking that Delegates wont work with a ModernDialog - is this the case?

Thanks.
Jul 14, 2013 at 2:07 AM
Edited Jul 14, 2013 at 2:08 AM
Here's what I did for Window1 (your Page2). Window 1 was just a WPF Window I changed the class and namespace on it.
<mui:ModernDialog x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mui="http://firstfloorsoftware.com/ModernUI"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <!-- Whatever content here -->
        <TextBox Name="txtCustName" Height="33" TextWrapping="Wrap" Text="{Binding Value}" VerticalAlignment="Top" Margin="25,52,28,0"/>
        <Button Name="btnOK" Content="_OK" Margin="0,90,28,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="64"  />
    </Grid>
</mui:ModernDialog>

Public Class Window1
    Public Delegate Sub AddressUpdateHandler(sender As Object, e As AddressUpdateEventArgs)
    Public Event AddressUpdated As AddressUpdateHandler

    Private Sub btnOK_Click(sender As Object, e As RoutedEventArgs) Handles btnOK.Click
        Dim sCustName = txtCustName.Text
        Dim args As New AddressUpdateEventArgs(sCustName)
        RaiseEvent AddressUpdated(Me, args)
        DialogResult = True
        Close()
    End Sub
End Class
Here was the usercontrol that was loaded into the MainWindow, Page1)
<UserControl x:Class="Page1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Name="txtName" HorizontalAlignment="Left" Margin="22,27,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
        <Button Content="Get Customer Name" Name="btnGetCustName" HorizontalAlignment="Left" Margin="76,146,0,0" VerticalAlignment="Top" Width="146"/>
    </Grid>
</UserControl>

Public Class Page1
    Private Sub btnGetCustName_Click(sender As Object, e As RoutedEventArgs) Handles btnGetCustName.Click
        Dim f As New Window1
        f.Buttons = Nothing ' Clear default buttons
        AddHandler f.AddressUpdated, AddressOf AddressUpdateHandler
        f.ShowDialog()
        ' If you want to Show
        'f.Owner = Nothing
        'f.Show()
    End Sub
    Private Sub AddressUpdateHandler(sender As Object, e As AddressUpdateEventArgs)
        txtName.Text = e.CustName
    End Sub
End Class
Jul 14, 2013 at 4:53 AM
Finally....!
The penny has dropped......
Thank You Larry.....

My problem was that I was pushing a UserControl as content into a ModernDialog.

It wasn't until your example showed that I just should have been opening up a ModernDialog in the first place.......hours/days/months of coming back to this problem, testing, etc.....wow, that was frustrating, I think I just had the idea in my head that I just HAD to push a UserControl into the ModernDialog template because of how other ModernUI UserControls are dealt with....

Hanyway....

So my solution (with thanks to Larry) is:-
Page 1 - as my original example
Page 2 - make it a ModernDialog (you fool !!!!)

Have I said thanks Larry, well I will again, a thousand thanks and please have a beer on me (if you partake, that is).

Many, many thanks....
Theo... ;-)
Jul 14, 2013 at 9:12 AM
That's great but you can also have a usercontrol in the ModernDialog as content also.
Public Class Page1
    Private Sub btnGetCustName_Click(sender As Object, e As RoutedEventArgs) Handles btnGetCustName.Click
        Dim f As New Window1
        f.Buttons = Nothing ' Clear default buttons
        Dim uc = New Page2
        f.Content = uc
        AddHandler uc.AddressUpdated, AddressOf AddressUpdateHandler
        f.ShowDialog()
        ' If you want to Show
        'f.Owner = Nothing
        'f.Show()
    End Sub
    Private Sub AddressUpdateHandler(sender As Object, e As AddressUpdateEventArgs)
        txtName.Text = e.CustName
    End Sub
End Class
The code that would have gone in the window now goes in the UserControl. The only real difference is that you have to get the window the usercontrol is hosted in to set the dialog result and close.
Public Class Page2
    Public Delegate Sub AddressUpdateHandler(sender As Object, e As AddressUpdateEventArgs)
    Public Event AddressUpdated As AddressUpdateHandler

    Private Sub btnOK_Click(sender As Object, e As RoutedEventArgs) Handles btnOK.Click
        Dim sCustName = txtCustName.Text
        Dim args As New AddressUpdateEventArgs(sCustName)
        RaiseEvent AddressUpdated(Me, args)
        Dim parent = Window.GetWindow(Me)
        parent.DialogResult = True
        parent.Close()
    End Sub
End Class
hth.