I'm creating an application to automate some processes. One of them is creating docx file from the dotx template.
Steps are quite easy: app opens MS Word with test.dotx file and SaveAs it to c:\temp as a test.docx. It should be as close to user's actions as possible. When the file is opened (from dotx so it is docx already) all I need is to open SaveAs dialog and push "save" (or just "enter", because focus is set on "save" button).
The problem is how to "hit" the save/enter. I tried SendKeys but I am in ShowDialog() which is waiting for the result and cannot perform SendKeys at the moment. Of course if I press enter from keyboard or cklick on "Save" all works perfectly, but this one "press" I'd like to do from the code. Could you please point me how to solve this (if it is possible at all)? Thank you.
Here is the part of the code I'm strugglig with:
SaveFileDialog saveFileDialog1 = new SaveFileDialog
{
InitialDirectory = #"C:\Temp\",
DefaultExt = "docx",
FileName = "test"
};
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
object FileName = saveFileDialog1.FileName;
doc.SaveAs(ref FileName);
}
If hit the save/enter is your requirement, the recommendation is use Spy++ (this Tool can be installed by Visual Studio Installer) to capture both: "the dialog with save button" and "save button" alias/class; then, programming with C# PInvoke (for example, using FindWindow and FindWindowEx) to send/post message to simulate this "save" button.
Related
On a click event of a button , I'm opening a file in RichTextBox of a WPF app as follows. I've another button for closing the file but need to figure out how to close that opened file after reading it but without exiting my entire WPF app.
Think of it as a scenario similar to what we have in Microsoft WORD application where you can open a WORD document by using File-->Open menu item, and then you can close it by using File-->Close menu item while keeping the WORD application still open with top Ribbon. In my case, I've a toolbar on top with open and close button that I need to perform similar Open/Close file operations on:
private void BtnOpenFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Rich Text Format (*.rtf)|*.rtf|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
FileStream fileStream = new FileStream(dlg.FileName, FileMode.Open);
TextRange range = new TextRange(mainRTB.Document.ContentStart, mainRTB.Document.ContentEnd);
range.Load(fileStream, DataFormats.Rtf);
}
}
If you want to "close" the file whose content is shown in a RichTextBox, I think it'd be enough to clear the RichTextBox and to dispose your resources.
By "disposing your resources", I mean closing your FileStream or wrap it in a using, for example.
I've been trying to follow tutorials and various Stack Overflow posts, etc. to implement an OpenFileDialog to select a file. The problem is, it seems I can't get my program to continue with the rest of the logic. Not entirely sure if it has something to do with the fact I'm trying to open the file dialog inside my main window or what. Consider the following snippet:
public MainWindow()
{
InitializeComponent();
string file = "";
// Displays an OpenFileDialog so the user can select a file.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Files|*.txt;*.out";
openFileDialog1.Title = "Select a File";
openFileDialog1.ShowHelp = true;
// Show the Dialog.
// If the user clicked OK in the dialog and
// a file was selected, open it.
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//file = openFileDialog1.FileName;
//file = openFileDialog1.OpenFile().ToString();
//openFileDialog1.Dispose();
}
openFileDialog1 = null;
Console.WriteLine("File path is: " + file);
As you can see, I've even tried setting the "Help" value to true before the dialog finishes. I've tried to select both the file name for the file string, etc. but to no avail - the program seems to simply wait after the file is selected from the dialog. Would anyone here have a suggestion for a solution?
Previously I had the same problem with WPF. When you are working with WPF, System.Windows.Form Namespace is not included to your Project references;
and in the other hand actually, there are two OpenFileDialog, the first is System.Windows.Forms.OpenFileDialog (this is what you have) and the second is Microsoft.Win32.OpenFileDialog. if you want to get your code to work you must add System.Windows.Forms into your references:
Solution Explorer -> YourProject -> References (Right Click and Add Reference...) -> Assembly -> Framework -> Find And Select System.Windows.Forms -> OK
and the Next Solution is to use Microsoft.Win32, it's pretty easy. just add this Namespace into your code file and Change your code like this:
string file = "";
// Displays an OpenFileDialog so the user can select a file.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Files|*.txt;*.out";
openFileDialog1.Title = "Select a File";
// Show the Dialog.
// If the user clicked OK in the dialog and
// a file was selected, open it.
if (openFileDialog1.ShowDialog() == true)
{
file = openFileDialog1.FileName;
//file = openFileDialog1.OpenFile().ToString();
//openFileDialog1.Dispose();
}
openFileDialog1 = null;
Console.WriteLine("File path is: " + file);
OpenFileDialog.ShowDialog() is a modal method:
FileDialog is a modal dialog box; therefore, when shown, it blocks the
rest of the application until the user has chosen a file. When a
dialog box is displayed modally, no input (keyboard or mouse click)
can occur except to objects on the dialog box. The program must hide
or close the dialog box (usually in response to some user action)
before input to the calling program can occur.
This means that invoking this method will block your main thread until the dialog box is closed. A few of the options you have are:
Invoke the FileDialog after the main window is initialized.
Invoke the FileDialog from a thread. In this case, be careful to synchronize.
I am trying to make a folder selection which would show an error if there isn't enough space on the selected drive. I have created a custom designed error and dialog form, but there is a problem with using the FolderBrowserDialog.
Here is my actual code:
frmDialog dialog = new frmDialog("Install software", "The software cannot be found. Please select the path of the executable or let the launcher install it for you.");
dialog.SetYesButtonText("Install software");
dialog.SetNoButtonText("Browse for executable...");
if (dialog.ShowDialog() == DialogResult.Yes)
{
fbd = new FolderBrowserDialog();
fbd.Description = "Please select where do you want to install the software!";
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK) // + space checking, but I deleted it for debugging now.
{
frmError error = new frmError("Not enough space", "Please select a folder with at lease 22 MB of free space.");
error.ShowDialog();
}
}
I will actually make a loop afterwards which will run until the user selects a folder with enough space or cancels the selection.
The problem is that the error dialog does not get any focus. So when the user selects the folder, the FolderBrowserDialog disappears, and the error dialog shows up in a new window, but the Visual Studio's window gets the focus instead of the error dialog. As I experienced, this issue is not existing with my own forms, so if I changed the fdb to frmDialog, all the three dialogs would appear with focus after each other.
Set the owner of the dialogs like this:
fbd.ShowDialog( dialog );
error.ShowDialog( dialog );
I recommend to set the owners of the other dialogs to set a parent child relationship. So when you close the parent form, the child forms closes to. And also put a using block around your forms if you're using the ShowDialog calls.
I am working on winforms application in C#. What I want to achieve is to get a file from user for which I am using the following code:
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string sFileName = dlg.FileName;
//my code goes here
}
Now, everything is working fine but I want to put 3 radio buttons in the same dialog box, meaning I would now get two things from this dialog box
string sFileName = dlg.FileName; //same as in case of traditional dialog box
//some thing like this which tells which radio button is selected:
dlg.rbTypes.Selected
How do I achieve this?
Yes, that's possible, I did the same kind of customization with SaveFileDialog successfully and it's pretty interesting.
Follow the following links:
http://www.codeproject.com/KB/dialog/OpenFileDialogEx.aspx
http://www.codeproject.com/KB/cs/getsavefilename.aspx
http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx
Also my own questions too will help you:
Change default arrangement of Save and Cancel buttons in SaveFileDialog
How to stop overwriteprompt when creating SaveFileDialog using GetSaveFileName
You have to use the WinAPI for this and you need to write the ShowDialog method in your own calling the GetOpenFileName windows function inside it, instead of calling .net's OpenFileDialog. The GetOpenFileName will create the windows OpenFileDialog. (Refer to http://msdn.microsoft.com/en-us/library/ms646927%28v=vs.85%29.aspx). This together with writing the HookProc procedure and catching events such as WM_INITDIALOG, CDN_INITDONE inside it will help you do what you want.
To add radio buttons etc., you have to call the windows functions such as CreateWindowEx and SendMessage....
The 2nd link has the exact direction to the customization...
Ask for any clarifications...
On XP you need to use the hook procedure method and the GetOpenFileName API. On Vista and later this will result in a horrid looking file dialog with limited utility, e.g. no search. On Vista you should use IFileDialog and to customise the dialog you need the IFileDialogCustomize interface. Because the new Vista dialogs are exposed as COM interfaces they are quite easy to consume in .net.
I feel really silly having to ask this question as I know I should not be having so much trouble with this simple task....but I am trying to launch my .msi when a user pushes a button of a form. I am certain this is a one liner but I cannot for the life of me figure this out. I have the .MSI file on my desktop so I want the button to also be able to have the user select where the msi file is. If anyone could help me that would be grand...
Look at Process.Start.
Process.Start("path to msi");
To get the path to the file, you can use the FileDialog class (assuming winforms).
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
Process.Start(openFileDialog1.FileName);
}
Look at using this to get the file:
FileDialog dialog = new FileDialog();