How change Mouse pointer in Outlook VSTO - c#

I am trying to upload a file to a remote server using a custom button in outlook ribbon. Following steps are happened
Users click the upload file button in the ribbon and the File picker dialog is seen.
File selection popup will be seen and user select one or more files
After click ok button, I just want to change the default cursor to wait for the cursor
Once file uploading is complete, I tried to restore default curosor
My code is like following
public void UploadFiles(Office.IRibbonControl control)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Cursor.Current = Cursors.WaitCursor
//some code which will call rest API to upload file
Cursor.Current = Cursors.default
}
}
But above code is not changing curosor. The button is present in compose window of outlook

You need to import Word.DLL and then use the following code
public void UploadFiles(Office.IRibbonControl control)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Outlook.Inspector currentInspector = Globals.ThisAddIn.Application.ActiveInspector();
Word.Document document = currentInspector.WordEditor;
Word.Application wordApp = document.Application;
wordApp.System.Cursor = Word.WdCursorType.wdCursorWait;
//perform your task
//Switch to default Cursor
wordApp.System.Cursor = Word.WdCursorType.wdCursorNormal;
}
}

Related

How to make UserControl add attachments

How to make user control in Windows application c#
I need make attachments files in form but I need use an user control when click
button browse and choose the files or image add user control in form ?
Add a button on the form and use OpenFileDialog, like that:
private void buttonGetFile_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Text files | *.txt"; // file types, that will be allowed to upload
dialog.Multiselect = false; // allow/deny user to upload more than one file at a time
if (dialog.ShowDialog() == DialogResult.OK) // if user clicked OK
{
String path = dialog.FileName; // get name of file
using (StreamReader reader = new StreamReader(new FileStream(path, FileMode.Open), new UTF8Encoding())) // do anything you want, e.g. read it
{
// ...
}
}
}

C# OpenFileDialog opening twice

I am having a problem with a windows form coded in Visual Studio using C# as part of a web development course. When I click on the menu item ('Import') the file dialogue box opens twice.
The first dialogue seems incorrect as it does not have the filter or title applied. Then the second dialogue box which immediately opens afterward has the correct filter and title applied.
Obviously it is only meant to open once to enable me to read the selected file and add it to the Listbox. This works, but only once I have selected the file twice.
Thanks in advance for your help.
private void importToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
openFileDialog1.Filter = "Text|*.txt";
openFileDialog1.Title = "Choose a file to import";
if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Oh. No file selected!");
}
else
{
string usersFile = openFileDialog1.FileName;
string[] lines = File.ReadAllLines(usersFile);
foreach (string line in lines)
{
groceryList.Items.Add(line);
}
}
Start of your code you have
private void importToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
this is unneccessary, the ShowDialog will show a dialog with the filters not correctly set up as your next lines have:
openFileDialog1.Filter = "Text|*.txt";
openFileDialog1.Title = "Choose a file to import";
Then you ShowDialog again.
Therefore, just remove the first
openFileDialog1.ShowDialog();
and you should be fine.

browse folder and get user input to create a new file

I want to create a file in a directory selected by the user and named it by the user input.
I tried FolderBrowserDialog but it didn't prompt me to give a file name:
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string path = fbd.SelectedPath;
//string FileName; then concatenate it with the path to create a new file
how can I do that?
You want to create a new file in a folder, so you should:
ask the user to select a folder (with FolderBrowserDialog)
offer the user a way to type a file name, with an input field (separate from the folder dialog)
Then you concat those 2 infos to get your full file name.
Or you can use SaveFileDialog and check if the file already exists when the user has selected a file (with a File.Exists...). There is a property for displaying an alert when the file does not exists, but not on the other side.
So when you got the DialogResult, use File.Exists and you can alert the user.
Sample for this solution:
In this sample (I hope without errors, cannot test right now):
- I open the saveFileDialog on a button called SaveButton with the SaveButton_Click click method
- I have a SaveFileDialog component on my form, called saveFileDialog1. On this component, the event FileOK is associated to my saveFileDialog1_FileOk method
private void SaveButton_Click(object sender, EventArgs e)
{
// Set your default directory
saveFileDialog1.InitialDirectory = #"C:\";
// Set the title of your dialog
saveFileDialog1.Title = "Save file";
// Do not display an alert when the user uses a non existing file
saveFileDialog1.CheckFileExists = false;
// Default extension, in this sample txt.
saveFileDialog1.DefaultExt = "txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// DO WHAT YOU WANT WHEN THE FILE AS BEEN CHOSEN
}
}
// This method handles the FileOK event. It checks if the file already exists
private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
if (File.Exists(saveFileDialog1.FileName))
{
// The file already exists, the user must select an other file
MessageBox.Show("Please select a new file, not an existing one");
e.Cancel = true;
}
}

Open File Dialog box on Clicking Link Label

I've created a log in panel in which I've used Transparent group box(with user name text box and password text box), and used a wallpaper on background, now I've Used a link label on this log in panel by clicking on which the user can change the background wallpaper of the log in panel.
Means when user clicks on the link label (lnklblChangeBackGround) with text "Click Here to Change Background" open dialogue box will open and user can select the Wallpaper from here and then by clicking on OK or Select the wallpaper will be assigned to background of the log in panel
Can any one help me out that
how can i open a open dialogue box by clicking on the link label
how can i assign a select wallpaper to the background of my log in panel
Note: I'm Creating this using VS 2010 using C#. and it's a desktop App and i'm using winform here.
At first you have to add an Event (LinkClicked) to your Link Label.
Just place this code here to open a file dialog.
private String getPicture()
{
string myPic = string.Empty;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "jpg (*.jpg)|*.jpg|png (*.png)|*.png";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
myPic = openFileDialog1.FileName;
return myPic;
}
You can edit the filter to avoid the user choosing images, which is not supported in your opinion.
With this code below you can set the Background Image of your pictureBox
private void setBackground(String picture)
{
pictureBox1.Image = null;
pictureBox1.Image = Image.FromFile(picture);
}
And the final version would look like this
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
String myFile = getPicture();
setBackground(myFile);
}
if this is too much code or too complicated for you, then you can just put it all in one function like this
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string myPic = string.Empty;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "jpg (*.jpg)|*.jpg|png (*.png)|*.png";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
myPic = openFileDialog1.FileName;
pictureBox1.Image = null;
pictureBox1.Image = Image.FromFile(myPic);
}

Why does showing a OpenFileDialog in WPF block my WPF Popup?

I have a WPF application where i open a popup mennu (popup control) using the escape key. In that popup menu I open a file dialog when pushing a button, and the when pushing the button closes the popup. When i next time pushes the esc button it doesnt pop up, not until i have focused another program, eg. reset focus. Does anyone know what could cause this?
Edit
//called when pushing esc
private void ShowSettingsMenu()
{
SettingsMenu.IsOpen = true;
}
//clicking my button, subsequent presses on my esc, doesnt pop it up (the code is run)
private void ImportLicenseButton_Click(object sender, RoutedEventArgs e)
{
SettingsMenu.IsOpen = false; //<- hiding it again
OpenFileDialog filedialog = new OpenFileDialog();
filedialog.Filter = "Xml Files|*.xml";
if ((bool)filedialog.ShowDialog())
{
string fileName = "license.xml";
string destinationFolder = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
if (!string.IsNullOrEmpty(filedialog.FileName))
{
File.Copy(filedialog.FileName, System.IO.Path.Combine(destinationFolder, fileName), true);
}
else
{
MessageBox.Show("Please select a file name");
}
}
this.Cursor = Cursors.None;
}
Fixed by redisplaying the popup.

Categories