private void accountsList_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0) { Account currAccount = ((ViewModel)accountsList.Rows[e.RowIndex].DataBoundItem).DataItem; currAccount.IsDefault = true; MQTTModel mqtt = new MQTTModel(); mqtt.Accounts.Attach(currAccount); var entry = mqtt.Entry(currAccount); entry.Property(a => a.IsDefault).IsModified = true; mqtt.SaveChanges(); this.DialogResult = DialogResult.OK; this.Close(); } else { Account currAccount = ((ViewModel)accountsList.Rows[e.RowIndex].DataBoundItem).DataItem; MQTTModel mqtt = new MQTTModel(); mqtt.Accounts.Attach(currAccount); mqtt.Accounts.Remove(currAccount); mqtt.SaveChanges(); this.accountsList.DataSource = mqtt.Accounts.Select(o => new ViewModel() { DataItem = o }).ToList(); } }
private void sendButton_Click(object sender, EventArgs e) { if (txtContent.Text.Length == 0) { MessageBox.Show("Content is required"); return; } if (txtTopic.Text.Length == 0) { MessageBox.Show("Topic is required"); return; } if (cmbQOS.SelectedIndex == -1) { MessageBox.Show("Please choose QOS"); return; } QOS messageQOS = QOS.AT_MOST_ONCE; switch (cmbQOS.SelectedIndex) { case 1: messageQOS = QOS.AT_LEAST_ONCE; break; case 2: messageQOS = QOS.EXACTLY_ONCE; break; } MQTTModel mqtt = new MQTTModel(); var accounts = from a in mqtt.Accounts where a.IsDefault == true select a; dal.Message newMessage = new dal.Message(); newMessage.Content = Encoding.UTF8.GetBytes(txtContent.Text); newMessage.Incoming = false; newMessage.QOS = messageQOS; newMessage.TopicName = txtTopic.Text; newMessage.Account = accounts.First(); _client.Publish(new mqtt.avps.Topic(newMessage.TopicName, newMessage.QOS), newMessage.Content, chkRetain.Checked, chkDuplicate.Checked); mqtt.Accounts.Attach(newMessage.Account); mqtt.Messages.Add(newMessage); mqtt.Entry(newMessage.Account).State = EntityState.Unchanged; mqtt.SaveChanges(); txtContent.Text = String.Empty; txtTopic.Text = String.Empty; cmbQOS.SelectedIndex = -1; chkDuplicate.Checked = false; chkRetain.Checked = false; MessageBox.Show("Message Sent Succesfully", "Message Sent", MessageBoxButtons.OK, MessageBoxIcon.Information); List <dal.Message> messages = (from m in mqtt.Messages.Include("Account") where m.Account.ID == _account.ID orderby m.ID descending select m).ToList(); this.Invoke((MethodInvoker) delegate { this.messagesGrid.DataSource = messages; }); mqtt.Dispose(); }