How to Add an Error Message before Deleting an item from a Listbox in Windows Phone 8.1

Asked

Viewed 57 times

2

Great masters, I am here again in search of knowledge. I have a small application on Windows Phone 8.1, with a Sqlite database, as shown below:

inserir a descrição da imagem aqui

I would like when selecting the item in the Listbox, and clicking on the Delete button, to launch a message asking if the user wants to delete this data. The code of the Delete button is as below:

    private async void btnExcluir_Click(object sender, RoutedEventArgs e)
    {
        var excluir = ltbExibir.SelectedItem as Moto;
        excluir.mot_nome = txtNomeMoto.Text;
        excluir.mot_placa = txtPlacaMoto.Text;
        await conexao.DeleteAsync(excluir);            
    }

And taking the opportunity, I would like to know how to insert title in the columns of Listbox. Waiting for your valuable information.

2 answers

0

To view a dialog box in Windows Phone 8.1 you must use the class Windows.UI.Popups.MessageDialog. You can add a title, a message and for Windows Phone 8.1 it allows you to add at most 2 buttons.

Below is an example of use:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    // Crie uma instância da classe MessageDialog passando a mensagem e o título da mensagem
    MessageDialog msg = new MessageDialog("C# melhor linguagem! Você concorda né?", "Oi oi oi!");

    // Adicione os botões desejados e configure os eventos para cada um
    msg.Commands.Add(new UICommand("Sim", new UICommandInvokedHandler(CommandHandlers)));
    msg.Commands.Add(new UICommand("Não", new UICommandInvokedHandler(CommandHandlers)));

    // Exibe a MessageDialog
    await msg.ShowAsync();
}

public void CommandHandlers(IUICommand commandLabel)
{
    // A verificação de qual botão foi pressionado pelo usuário só pode ser feita dessa forma
    var action = commandLabel.Label;
    switch (action)
    {
        case "Sim":
            // Faça alguma coisa pra alegrar o usuário, ele deu a resposta certa
            break;

        case "Não":
            // Reposta errada, faça alguma outra coisa
            break;
    }
}

About the multiple columns in the ListView, this will only be possible through a DataTemplate customized to the ListView.ItemTemplate and column titles would have to be done out of control. This is because the control ListView not designed to display data in a "table format".

-1

Solution for Silverlight

You should use Messagebox for this in the following way:

MessageBoxResult result = MessageBox.Show("Deseja continuar?", "Opção", MessageBoxButton.OKCancel);

if (result == MessageBoxResult.OK)
{
    //Logica
}

Solution for Winrt

MessageDialog msgDialog = new MessageDialog("Sua mensagem", "Titulo");

//OK Button
UICommand okBtn = new UICommand("OK");
okBtn.Invoked = OkBtnClick;
msgDialog.Commands.Add(okBtn);

//Cancel Button
UICommand cancelBtn = new UICommand("Cancel");
cancelBtn.Invoked = CancelBtnClick;
msgDialog.Commands.Add(cancelBtn);

//Show message
msgDialog.ShowAsync();

And create button calling methods:

private void CancelBtnClick(IUICommand command)
{
}

private void OkBtnClick(IUICommand command)
{
}
  • 1

    There is no Messagebox.Show for Winrt applications. Don’t go around writing any nonsense. As a matter of fact, it even exists, if the AP had specified that it was using Windows Phone Silverlight 8.1.

  • 1

    I did not write any "nonsense", really I was wrong showing the solution only to Silverlight without knowing if its app is in this format.

  • 1

    Okay okay, I JUST THINK, IF YOU REALLY knew what I was writing, would not have committed this "mistake".

  • Let’s respect the answers, even if it was wrong, the intention was to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.