コード例 #1
0
        private async void DeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DeleteBtn.IsEnabled  = false;
                DetailsBtn.IsEnabled = false;
                ExitBtn.IsEnabled    = false;

                object medicine = MedicinesListBox.SelectedItem;
                int?   id       = ((Medicine)medicine)?.Id;
                if (!id.HasValue)
                {
                    throw new ArgumentException($"{nameof(id)} is incorrect");
                }

                MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo);
                if (messageBoxResult == MessageBoxResult.No)
                {
                    DeleteBtn.IsEnabled  = true;
                    DetailsBtn.IsEnabled = true;
                    ExitBtn.IsEnabled    = true;
                    return;
                }

                string request = tcpService.SerializeDeleteMedicine(id.Value, SingletoneObj.User);
                byte[] data    = await tcpService.CodeStreamAsync(request);

                await SingletoneObj.Stream.WriteAsync(data, 0, data.Length);

                string response = await tcpService.DecodeStreamAsync(SingletoneObj.Stream);

                var responseArgs = response.Split(';');
                if (responseArgs.Length > 1 && responseArgs[0].Contains("500"))
                {
                    throw new ArgumentException(responseArgs[1]);
                }
                if (responseArgs.Length == 1 && responseArgs[0].Equals("200"))
                {
                    medicines.Remove((Medicine)medicine);
                }
            }
            catch (Exception ex)
            {
                StatusLabel.Content = "Status: " + ex.Message;
            }
            finally
            {
                DeleteBtn.IsEnabled  = true;
                DetailsBtn.IsEnabled = true;
                ExitBtn.IsEnabled    = true;
            }
        }