Search code examples
c#wpfshowdialog

Window.ShowDialog failes on return


I have a custom input dialog box that request an user's name and a reson (because reasons) for doing a certain action in my application. The user clicks on a button on the main window and the dialog box shows, as it should.

The user then enters his/her name and the reason and clicks ok. The dialog then closes but I ( the program) never receives an answer. Here is my XAML for the input dialog:

<Window x:Class="Sequence_Application_2.GUI.ForcedRackInput"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Forcera Rack" Height="300" Width="300"
    WindowStartupLocation="CenterScreen">


<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="21*"/>
        <ColumnDefinition Width="274*"/>
    </Grid.ColumnDefinitions>
    <TextBox Name="OperatorNameText"  HorizontalAlignment="Left" Height="23" Margin="15,36,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2"/>
    <Label x:Name="label" Content="Namn:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
    <Label x:Name="label1" Content="Orsak:" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
    <Border BorderThickness="1" BorderBrush="Black" Grid.ColumnSpan="2" Margin="0,0,0,0.5">
        <TextBox Name="ReasonText" Margin="15,98,15,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="116" />
    </Border>

    <Button Name="OkButton"    IsDefault="True" Content="OK" Click="OkButtonPressed"  HorizontalAlignment="Left" Margin="26.202,233,0,0" VerticalAlignment="Top" Width="75" Cursor="Arrow" Grid.Column="1"/>
    <Button Name="CancelButton" IsCancel="True" Content="Avbryt" Margin="152.202,233,47,0" VerticalAlignment="Top" Cursor="Arrow" Grid.Column="1"/>

</Grid>
</Window>

And here is the "behind code":

namespace Sequence_Application_2.GUI
{
using System.Windows;

public partial class ForcedRackInput : Window
{

    public string OperatorName { get { return OperatorNameText.Text; }  }
    public string ForcedRackReason { get { return ReasonText.Text; } }

    public ForcedRackInput()
    {
        InitializeComponent();
    }

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


}
}

and this is how I call the code (from a model, not a "window class")

public void ForceClosingRack(Flow forcedFlow)
    {
        var forcedRackWindow = new ForcedRackInput();

        string operatorName = "";
        string reasonForForced = "";

        if( forcedRackWindow.ShowDialog() == true)
        {
            operatorName = forcedRackWindow.OperatorName;
            reasonForForced = forcedRackWindow.ForcedRackReason;

        }

    } // code jumps from "if(forcedRackWindow.... to this line when ok is clicked in the dialog

Looked for the solution for some time now and I just about to change career

Thanks for your time


Solution

  • My guess is that the problem doesn't lie in the code, which seems to be fine, but in your if statement.

    When you run your program in Debug mode it should work as expected.

    My guess is that you are assigning the variables operatorName and reasonForForced inside your if statment but they are not used anywhere else in the program and hence the whole if statement is ignored by the compiler and not present when running in Release mode.

    A small modification in your code which embeds different behaviour depending on the variable values can prove my guess:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var forcedRackWindow = new ForcedWindow();
    
        string operatorName = "foo";
        string reasonForForced = "foo";
    
        if (forcedRackWindow.ShowDialog() == true)
        {
            operatorName = forcedRackWindow.OperatorName;
            reasonForForced = forcedRackWindow.ForcedRackReason;
        }
    
        if(!operatorName.Equals(reasonForForced))
        {
            MessageBox.Show("We are not the same");
        }
    }