Beispiel #1
0
        private async void DeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DeleteBtn.IsEnabled = false;
                ExitBtn.IsEnabled   = false;

                object ward = WardsListBox.SelectedItem;
                int?   id   = ((Ward)ward)?.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;
                    ExitBtn.IsEnabled   = true;
                    return;
                }

                string request = tcpService.SerializeDeleteWard(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"))
                {
                    wards.Remove((Ward)ward);
                }
            }
            catch (Exception ex)
            {
                StatusLabel.Content = "Status: " + ex.Message;
            }
            finally
            {
                DeleteBtn.IsEnabled = true;
                ExitBtn.IsEnabled   = true;
            }
        }