views:

79

answers:

1

Basically, I'm trying to take information entered by the user on one page and print it out to another page via a "printer friendly" version, or report, of something. I have a MainPage.xaml that is, as the name suggests, my main page, but in a window there is the subpage AdCalculator.xaml where the user enters the information and PrintEstimate.xaml that is navigated to via a button on AdCalculator.

I would like to be able to transfer the information entered in the textboxes from AdCalculator and print it out via text blocks in PrintEstimate. So in order to do that I have the following code:

        Views.AdCalculator AdCalc = new Views.AdCalculator();
        string PrintCompanyName = AdCalc.CompanyName;
        string PrintContactName = AdCalc.txt_CustomerName.Text;
        string PrintBillingAddress1 = AdCalc.txt_BillingAddress.Text;
        string PrintBillingAddress2 = AdCalc.txt_BillingAddressLine2.Text;
        string PrintPhoneNumber = AdCalc.txt_PhoneNumber.Text;
        string PrintNumOfAds = AdCalc.txt_NumofAds.Text;
        string PrintRateOfPlay = AdCalc.Cmb_Rate.SelectedValue.ToString();
        string PrintNumOfMonths = AdCalc.txt_NumofMonths.Text;
        string PrintTotalDue = AdCalc.txt_InvoiceSummary_TotalDue.Text;

        PrintEstimate PrintEstimatePage = new PrintEstimate();
        PrintEstimatePage.txt_CompanyName.Text = PrintCompanyName;
        PrintEstimatePage.txt_CustomerName.Text = PrintContactName;
        PrintEstimatePage.txt_BillingAddress.Text = PrintBillingAddress1;
        PrintEstimatePage.txt_BillingAddressLine2.Text = PrintBillingAddress2;
        PrintEstimatePage.txt_PhoneNumber.Text = PrintPhoneNumber;
        PrintEstimatePage.txt_InvoiceSummary_NumofAds.Text = PrintNumOfAds;
        PrintEstimatePage.txt_InvoiceSummary_RateofPlay.Text = PrintRateOfPlay;
        PrintEstimatePage.txt_InvoiceSummary_NumOfMonths.Text = PrintNumOfMonths;
        PrintEstimatePage.txt_EstimateTotal.Text = PrintTotalDue;

Only problem is, when I instantiate the new AdCalculator page, it clears the values, so nothing is actually retained as far as user-input goes. Following a lead from a colleague, I believe all I need to do is change the line

        Views.AdCalculator AdCalc = new Views.AdCalculator();

to

        Views.AdCalculator AdCalc = (AdCalculator)Application.OpenForms["AdCalculator"]; 

except the "Apllication.OpenForms" doesn't register. I know there are a lot of differences in the way C# code-behind is laid out for silverlight applications, so I didn't know if there was an equivalent that anyone knew about to "Application.OpenForms" that would help solve my issue or if there was any other way to go about getting my task done.

Thank you very much!

+2  A: 

If I understand your question correctly you simply want to get some user input and display it.

I suggest you start by defining a class that will represent the data you are inputting, for example:

public class Customer
{
    public string ContectName { get; set; }
    public string BillingAddress1 { get; set; }
    public string BillingAddress2 { get; set; }
    public string PhoneNumber { get; set; }
    public int NumOfAds { get; set; }
    public double RateOfPlay { get; set; }
    public int NumOfMonths { get; set; }
    public double TotalDue { get; set; }
}

On the page where the user inputs data you then create an instance of this class, either by manually creating an instance and setting its properties when the user submits (similar to what you are doing in your code) or use databinding to your advantage (which is what I prefer).

Let's say for example you are inputting data on your MainPage

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    this.DataContext = new Customer();
}

Now you can bind the controls. Let's say you're using a grid:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <sdk:Label Content="Billing Address 1:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="billingAddress1TextBox" Text="{Binding Path=BillingAddress1, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Billing Address 2:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="3" Name="billingAddress2TextBox" Text="{Binding Path=BillingAddress2, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Contect Name:" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="3" Name="contectNameTextBox" Text="{Binding Path=ContectName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Num Of Ads:" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="3" Name="numOfAdsTextBox" Text="{Binding Path=NumOfAds, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Num Of Months:" Grid.Column="0" Grid.Row="4" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="4" Height="23" HorizontalAlignment="Left" Margin="3" Name="numOfMonthsTextBox" Text="{Binding Path=NumOfMonths, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Phone Number:" Grid.Column="0" Grid.Row="5" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="5" Height="23" HorizontalAlignment="Left" Margin="3" Name="phoneNumberTextBox" Text="{Binding Path=PhoneNumber, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Rate Of Play:" Grid.Column="0" Grid.Row="6" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="6" Height="23" HorizontalAlignment="Left" Margin="3" Name="rateOfPlayTextBox" Text="{Binding Path=RateOfPlay, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Total Due:" Grid.Column="0" Grid.Row="7" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="7" Height="23" HorizontalAlignment="Left" Margin="3" Name="totalDueTextBox" Text="{Binding Path=TotalDue, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
    </Grid>
</Grid>

When the user clicks the submit button, you can use something like this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var currentCustomer = this.DataContext as Customer;
    var previewWindow = new PrintPreviewWindow(currentCustomer);
    previewWindow.Show();
}

For this to work you'll need to have a Silverlight ChildWindow like this:

public partial class PrintPreviewWindow : ChildWindow
{
    public PrintPreviewWindow(Customer customer)
    {
        InitializeComponent();
        this.DataContext = customer;
    }

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = false;
    }
}

So your MainPage creates a new instance of the PrintPreviewChildWindow (could be a page as well if you prefer) and passes along the customer instance. The ChildWindow can then do whatever it wants with it. When the ChildWindow closes, you'll probably want to empty the input page, you can do this by simply setting the data context again:

this.DataContext = new Customer();

I'm guessing this is what you are looking for.

Try to get into the whole data binding stuff, it will save you lots and lots of lines of code. And let us know if this answers your question or if you have more :-)

TimothyP
Yes that is absolutely what I was asking. Let me try this out in my code and I'll let you know if I have any more questions, but this definitely looks to be everything I was needing and more! Thank you so much!
AmbiguousX
TimothyP,I am having trouble with the printing functionality. I have tried entering the code you provided (with adjustments to fit my variables, etc.) and objects such as "PrintPreviewWindow" are not registering: the compiler is throwing up an error suggesting a missing assembly reference/using directive. I have made sure to add the "System.Windows.Printing;" directive in my Using statements, but still nothing. I have looked on MSDN, as well as various other forums to see if there is something I am missing, but I'm just not seeing it.
AmbiguousX
(cont'd) I know I'm a rookie so it very well may be something minor, a "silly beginner's mistake" kind of thing, but I was just wondering if you knew of anything that might be missing? or anything I'm doing wrong? If you need any more code snippets or any more detailed information please let me know. Thank you!
AmbiguousX
Hi, I'm sorry to confuse you. The PrintPreviewWindow is simply a new ChildWindow I created. (Project add new item --> Silverlight --> ChildWindow, name it PrintPreviewWindow). On that ChildWindow you can add controls that will allow you to print. More info on printing here: http://wildermuth.com/2009/11/27/Silverlight_4_s_Printing_Support (Haven't used it myself)
TimothyP
Oh ok, sorry for the mix-up, I figured it was something I had misunderstood, I just didn't know what, thank you so much for your help!
AmbiguousX