質問

I want to show a dialog when iTunes.OnQuittingEvent event is called

(I have a WPF Application that is connected to iTunes using COM)

This dialog i want to create will ask the user if they want to close iTunes, they will have to close the application too. so if they clicked that button, iTunes, the dialog and the application will close.

but. if they click the cancel button, it will just close the dialog and cancel quitting iTunes.

can anyone help me? sorry i don't have any codes or screenshots yet, i haven't figure out everything including the design.

this is all i got as of the moment:

IN THE MAIN WINDOW:

Private Sub iTunes_OnQuittingEvent() Handles iTunes.OnQuittingEvent
  *'i still don't know how to show a dialog sorry*
End Sub
役に立ちましたか?

解決

For your dialog you can use something like

Dim result As MessageBoxResult = MessageBox.Show("Message Test", "Caption", MessageBoxButtons.OKCancel)

        Select Case result
            Case MessageBoxResult.OK
            Case MessageBoxResult.Cancel
        End Select

This will give you a modal message box with ok/cancel options.

You can then decide what to do using the case statement.

To make a custom dialog you can use something like this.

Here is a quick example.

XAML

<Window x:Class="MessageWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DialogWindow" Height="150" MaxHeight="450" Width="400" MaxWidth="600" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
            <StackPanel Orientation="Horizontal">
                <Button Name="btnYes" Content="Yes" Click="btnYes_Click"></Button>
                <Button Name="btnNo" Content="No" Click="btnNo_Click"></Button>
                <Button Name="btnCancel" Content="Cancel" Click="btnCancel_Click"></Button>

            </StackPanel></Window>

VB

Public Class MessageWindow

    Private _messageResponse As MessageBoxResult

    Public Property MessageResponse As MessageBoxResult
        Get
            Return _messageResponse
        End Get
        Set(value As MessageBoxResult)
            _messageResponse = value
        End Set
    End Property

    Private Sub btnYes_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        DialogResult = True
        _messageResponse = MessageBoxResult.Yes
        Me.Close()
    End Sub

    Private Sub btnNo_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        DialogResult = False
        _messageResponse = MessageBoxResult.No
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        DialogResult = False
        _messageResponse = MessageBoxResult.Cancel
        Me.Close()
    End Sub
End Class

You can then use it like this

Dim messageWindow As New MessageWindow()

        messageWindow.ShowDialog()

        Dim result As MessageBoxResult = messageWindow.MessageResponse
        'handle response

Note if you only have two possible responses then you can use the dialogresult property of Window which is returned from the showdialog function of window. E.g. You can use dialogresult = true for OK and dialogresult = false for cancel.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top