Cancelling sending information upon clicking on "Cancel" from VSTO Add in - c#

I have a custom add in for Outlook, there is a ribbon and it has a button "Send Info", when i click it, it opens a WPF form which has a save and cancel buttons, when the save is clicked it grabs a list of phone numbers and sends it via Http post to an application that saved them. The problem is that, if the user click on "Cancel", it should cancel the whole process at any point.. What is happening is that when you click Save, it will send whatever is there and if cancel is clicked, it would still save some... Is there a way to cancel the whole process by clicking on the cancel button on the form?.
I just need some guidance as to how others have done this or were able to do this.

See if it helps:
private Thread td;
void Save()
{
//initialize the thread here
//...
td.Start();
//...
}
void Cancel()
{
if (td != null && td.IsAlive)
{
//warning canceling
//...
td.Abort();
}
}

Related

Detecting which button was clicked when closing a winform?

I have this event handler in my winform:
private void SaveToDXF_FormClosing(object sender, FormClosingEventArgs e)
{
// Make sure the user has selected at-least one layer
if (listBoxLayers.SelectedItems.Count == 0)
{
_AcAp.Application.ShowAlertDialog("Please select one or more layers.");
e.Cancel = true;
}
}
Originally I was using the OK button click handler but I quickly found out that there did not seem to be a way to cancel out of actually closing the form. Then I read on SO a suggestion to use FormClosing. This works fine but ...
If the user closing the form by pressing the Cancel button this event still fires. That makes sense. But I only want to perform this validation check and cancel closing the form if they click the OK button.
How do we do this?
Sounds like you can check the this.DialogResult in the closing handler: it will be different depending which button was clicked (whatever you set as the DialogResult in the properties grid).
e.g. "don't do the check if they are cancelling" could be like
if (this.DialogResult != DialogResult.Cancel && listBoxLayers.SelectedItems.Count == 0)
If you have more checks to do it might be simple to just "if cancelling then return" as the event handler first line

Click button after webBrowser1_DocumentCompleted event

I have a C# 4.0 WinForms application, which has a WebBrowser control and 2-buttons.
Clicking the first button sends a URL to the browser to navigate to a specified webSite.
Clicking the second button parses the OuterHtml of the webBrowser1.Document, looking for an "https://..." link for File Download.
The code then uses a webClient.DownloadFileAsync to pull down a file for further use in the application.
The above code successfully works, if I manually click those buttons.
In an effort to automate this for the end-user, I place the first button's click event, i.e. btnDisplayWeb.PerformClick(); in the form's Form1_Load event. This also works, allowing the webBrowser1 to populate its Document with the desired webSite.
However, I am unable to programatically click the 2nd button to acquire the web link for file download.
I have tried to place the 2nd buttons click event within the browser's DocumentCompleted event, as shown below.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
btnMyUrl.PerformClick();
}
However, from what I've read on StackOverFlow and other sites, it's possible that this particular event gets called more than once, and hence it fails.
I've also attempted to loop for a number of seconds, or even use a Thread.Sleep(xxxx), but the browser window fails to populate until the sleep or timer stops.
I attempted to use the suggestions found on the following StackOverFlow site shown below.
How to use WebBrowser control DocumentCompleted event in C#?
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string url = e.Url.ToString();
if (!(url.StartsWith("http://") || url.StartsWith("https://")))
{
// in AJAX
}
if (e.Url.AbsolutePath != this.webBrowser.Url.AbsolutePath)
{
// IFRAME
}
else
{
// REAL DOCUMENT COMPLETE
}
}
However, in parsing the OuterHtml, nothing is returned in the first two sections, and in the third section, other elements are returned instead of the desired "https://..." link for File Download.
Interestingly, if I use a webBrowser1.ReadyState event, as shown below, and place a MessageBox inside DocumentCompleted, this seems to allow the browser document to complete, because after clicking the OK button, the parsing is successful.
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
MessageBox.Show("waiting", "CHECKING");
btnMyUrl.PerformClick();
}
However, I then have the difficulty of finding a way to click the OK button of the MessageBox.
Is there another event that occurs after the DocumentCompleted event.
OR, can someone suggest how to programmatically close the MessageBox?
If this can be done in code, then I can perform the buttonClick() of the 2nd button in that section of code.
After finding that the addition of a MessageBox allows the webBrowser1.Document to complete, and using webBrowser1.ReadyState event within the webBrowser_DocumentCompleted event, all I needed to do, was to find a way to programmatically close the MessageBox.
Further searching on StackOverFlow revealed the following solution on the site below.
Close a MessageBox after several seconds
Implementing the AutoClosingMessageBox, and setting a time interval, closed the MessageBox and allowed my button click, i.e. btnMyUrl.PerformClick(); to successfully parse the OuterHtml and now the code works properly.
Hopefully, if someone else discovers that placing a MessageBox within the webBrowser_DocumentCompleted event allows the document to complete; the aforementioned AutoClosingMessageBox will assist them as well.

Cancel dropdown behavior on click

This may seem like a strange request, but I want to cancel the normal behavior of a dropdown control regarding opening or closing the list. I have several dropdowns on a form and it seemed tedious to have to hit the arrow exactly to open the list. I added code in the click event handler to open or close the list depending on its current state:
private void customerCodeComboBox_Click(object sender, EventArgs e)
{
if (customerCodeComboBox.DroppedDown == false)
customerCodeComboBox.DroppedDown = true;
else
customerCodeComboBox.DroppedDown = false;
}
This works just fine unless you actually click on the dropdown arrow. Then it opens and closes immediately since it fires the normal open and then my code closes it! :(
Is there a way to cancel the normal behavior? e.Cancel was not available in the EventArgs. Is there a better way to open the list when a mouse clicks anywhere in the field?

Outlook AddIn: showing popup while dragging mail in Outlook

I have an outlook addin which shows a popup after sending mail which asks the user whether he/she wants to save the mail.
The problem is that this popup is shown when a mail is added to the sent folder.
So when you send a mail and immediately start dragging a mail to another folder because, e.g. you want to clean up mails, then the popup shows when you're in the middle of dragging which stops the dragging and makes the popup with the question unresponsive to the mouse.
Is there any way around this?
Thanks in advance!
The event for an mail being added to sent folder is set in the Ribbon Load method:
private void CodexRibbon_Load(object sender, RibbonUIEventArgs e)
{
if (!Globals.ThisAddIn.FirstLoadComplete)
{
Messenger.Default.Register<object>(this, ItemSend);
Globals.ThisAddIn.FirstLoadComplete = true;
}
}
The question is asked as follows:
private void ItemSend(object item)
{
if (MessageBox.Show(#" Wilt u de zojuist verzonden e-mail opslaan?", #"Opslaan in Codex", MessageBoxButtons.YesNo) == DialogResult.Yes)
ShowSaveWindow(item);
}
I don't think there is a way to find out if the control is inside the DoDragDrop Windows API function (you can patch it and have the patched version set some kind of a global flag before entering the original function, but I don't think that will be a good idea in .Net).
Try to display the message box in the Application.ItemSendevent handler, mark the message somehow (user property?), then do the processing in Items.ItemAdd without a message box.

Cancel button to cancel SpeakAsync method doesn't respond

I have a cancel button to cancel speakAsync method of a speech syntehsizer. It is called over a PromptBuilder and the output is set to a wavefile, as
reader.SetOutputToWaveFile(CurAudioFile, fmt);
reader.SpeakAsync(PB);
And cancel it as following:
private buttonCancel_Click(...)
{
if (Utility.Warn("Are you sure you want to cancel reading the document aloud?") == System.Windows.Forms.DialogResult.Yes)
{
reader.SpeakAsyncCancelAll();
}
}
But the problem is that, when I click on the cancel button, the code is never executed! actually it become unresponsive to the button click and it is not executed!
I tried to use Application.DoEvents() in speak_progress but it also doesn't help!
Since it is a different thread when playing asynchronously, you are probably loosing reference to your reader object.
I suggest you to declare reader as a static property especially if you cancel it in a different event.
I found that in the Speak_progress event which is called after saying each word, I call Focus method for a control. then it shifts focus from the cancel button frequently and the click couldn't be performed!
Hope this answer be useful for similar problems!

Categories