locked
How to handle message box result? RRS feed

  • Question

  • Hi All,

    I am working on a speech app having a msg box as shown. Need to do necessary actions based on user saying "Yes/No". But how to set/get the message box result and close the message box programmatically.

    suppose if (userspeech = "yes") {//do something; close msgbox}

                   if(userspeech="No") {//do something; close msgbox}

    I dont have access to mouse and can't get the messagebox result.

    or any suggestions for a better solution pls.

    string msgBoxText = "Do you want to add selected item to cart?";
                string msgBoxCaption = "Word Processor";
                MessageBoxButton msgBoxBtn = MessageBoxButton.YesNo;
                MessageBoxImage msgBoxIcon = MessageBoxImage.Question;
    MessageBox.Show(msgBoxText, msgBoxCaption, msgBoxBtn, msgBoxIcon);

    Thursday, March 15, 2012 2:48 PM

Answers

  • Create a new windows and design it similar to MessageBox with two button Yes and No. Then you can programmatically raise the Yes and No button click event when speech is detected.

    XAML Code

    <Window x:Class="CustomMessageWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CustomMessageWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <Button Name="btnYes" Content="Yes"/>
                <Button Name="btnNo" Content="No"/>
            </StackPanel>
        </Grid>
    </Window>

    Code Behind

    Class CustomMessageWindow
     
        Public Sub ListenToSpeech(ByVal speechText As String)
            If speechText.ToUpperInvariant = "YES" Then
                'Raise click event of btnYes programmatically
                btnYes.RaiseEvent(New RoutedEventArgs(Button.ClickEvent))
            ElseIf speechText.ToUpperInvariant = "NO" Then
                'Raise click event of btnYes programmatically
                btnNo.RaiseEvent(New RoutedEventArgs(Button.ClickEvent))
            End If
        End Sub
     
        Private Sub btnYes_Click(sender As Object, e As System.Windows.RoutedEventArgsHandles btnYes.Click
            ''Some code
            Me.DialogResult = True
        End Sub
     
        Private Sub btnNo_Click(sender As Object, e As System.Windows.RoutedEventArgsHandles btnNo.Click
            'Some code
            Me.DialogResult = False
        End Sub
    End Class


    Gaurav Khanna | Microsoft VB.NET MVP


    • Edited by Khanna Gaurav Thursday, March 15, 2012 6:13 PM
    • Marked as answer by ykbharat Friday, March 16, 2012 9:08 AM
    Thursday, March 15, 2012 6:12 PM

All replies

  • Hi,

    In order to simulate client click on MessageBox, you'll probably need to call some winAPI methods.

    This will become more complecated as the message box actually block your ui thread and does not supply window handle.

    why not create your own messageBox (simple window) and control what you want?

    I think it will be simpler.

    Asaf.


    MCPD
    MCTS - TFS 2010
    My Blog: http://benjaminet.wordpress.com

    Thursday, March 15, 2012 3:53 PM
  • If I had to do this, I would be thinking of the following:

       

    1. Inherit and extend the Window class, so that it can close itself and return Yes or No. The close/return action would best be triggered by an event from the "Mediator" pattern - one the best examples being MVVMLite's Messenger. With this you can register a listener for the speech Yes/No event, and act as if the buttons were clicked.

    2. Use Messenger's Send method to fire off a Yes/No event when the speech monitor triggers an input.

    3. Keep the if/else logic back in your business logic layer, so the new Spech enabled Yes/No dialog is kept clean and is reusable.

       

    Regards,
    Pete


    #PEJL

    Thursday, March 15, 2012 4:19 PM
  • Create a new windows and design it similar to MessageBox with two button Yes and No. Then you can programmatically raise the Yes and No button click event when speech is detected.

    XAML Code

    <Window x:Class="CustomMessageWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CustomMessageWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <Button Name="btnYes" Content="Yes"/>
                <Button Name="btnNo" Content="No"/>
            </StackPanel>
        </Grid>
    </Window>

    Code Behind

    Class CustomMessageWindow
     
        Public Sub ListenToSpeech(ByVal speechText As String)
            If speechText.ToUpperInvariant = "YES" Then
                'Raise click event of btnYes programmatically
                btnYes.RaiseEvent(New RoutedEventArgs(Button.ClickEvent))
            ElseIf speechText.ToUpperInvariant = "NO" Then
                'Raise click event of btnYes programmatically
                btnNo.RaiseEvent(New RoutedEventArgs(Button.ClickEvent))
            End If
        End Sub
     
        Private Sub btnYes_Click(sender As Object, e As System.Windows.RoutedEventArgsHandles btnYes.Click
            ''Some code
            Me.DialogResult = True
        End Sub
     
        Private Sub btnNo_Click(sender As Object, e As System.Windows.RoutedEventArgsHandles btnNo.Click
            'Some code
            Me.DialogResult = False
        End Sub
    End Class


    Gaurav Khanna | Microsoft VB.NET MVP


    • Edited by Khanna Gaurav Thursday, March 15, 2012 6:13 PM
    • Marked as answer by ykbharat Friday, March 16, 2012 9:08 AM
    Thursday, March 15, 2012 6:12 PM
  • What should be action if the user did not click Yes or No button for quite some time (assume 5 mins), then we need to raise another event (Logout).

    How can we handle in this scenario?

    Tuesday, March 27, 2012 9:46 PM
  • What should be action if the user did not click Yes or No button for quite some time (assume 5 mins), then we need to raise another event (Logout).

    How can we handle in this scenario?

    Add a DispatcherTimer with interval of 5 minutes and start it in Window Loaded event. And on Tick event of DispatcherTimer you can raise Logout event.

    Gaurav Khanna | Microsoft VB.NET MVP

    Wednesday, March 28, 2012 6:36 AM